2 music.cc -- implement Music
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "duration.hh"
12 #include "input-smob.hh"
13 #include "ly-smobs.icc"
15 #include "music-list.hh"
21 Music is anything that has duration and supports both time compression
24 In Lily, everything that can be thought to have a length and a pitch
25 (which has a duration which can be transposed) is considered "music",
28 Music::internal_is_music_type (SCM k) const
30 SCM ifs = get_property ("types");
32 return scm_c_memq (k, ifs) != SCM_BOOL_F;
38 SCM nm = get_property ("name");
39 if (scm_is_symbol (nm))
41 return ly_symbol2string (nm);
45 return classname (this);
49 Music::Music (SCM init)
52 immutable_property_alist_ = init;
53 mutable_property_alist_ = SCM_EOL;
56 length_callback_ = get_property ("length-callback");
57 start_callback_ = get_property ("start-callback");
60 Music::Music (Music const &m)
62 immutable_property_alist_ = m.immutable_property_alist_;
63 mutable_property_alist_ = SCM_EOL;
66 /* First we smobify_self, then we copy over the stuff. If we don't,
67 stack vars that hold the copy might be optimized away, meaning
68 that they won't be protected from GC. */
70 mutable_property_alist_ = ly_music_deep_copy (m.mutable_property_alist_);
71 length_callback_ = m.length_callback_;
72 start_callback_ = m.start_callback_;
73 set_spot (*m.origin ());
83 Music::get_property_alist (bool m) const
85 return (m) ? mutable_property_alist_ : immutable_property_alist_;
89 Music::mark_smob (SCM m)
91 Music *mus = (Music*) SCM_CELL_WORD_1 (m);
92 scm_gc_mark (mus->immutable_property_alist_);
93 scm_gc_mark (mus->mutable_property_alist_);
98 Music::get_length () const
100 SCM lst = get_property ("length");
101 if (unsmob_moment (lst))
102 return *unsmob_moment (lst);
104 if (ly_c_procedure_p (length_callback_))
106 SCM res = scm_call_1 (length_callback_, self_scm ());
107 return *unsmob_moment (res);
114 Music::start_mom () const
116 SCM lst = get_property ("start-callback");
117 if (ly_c_procedure_p (lst))
119 SCM res = scm_call_1 (lst, self_scm ());
120 return *unsmob_moment (res);
128 print_alist (SCM a, SCM port)
130 /* SCM_EOL -> catch malformed lists. */
131 for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
133 scm_display (scm_caar (s), port);
134 scm_puts (" = ", port);
135 scm_write (scm_cdar (s), port);
136 scm_puts ("\n", port);
141 Music::print_smob (SCM s, SCM p, scm_print_state*)
143 scm_puts ("#<Music ", p);
144 Music* m = unsmob_music (s);
146 SCM nm = m->get_property ("name");
147 if (scm_is_symbol (nm) || scm_is_string (nm))
150 scm_puts (classname (m),p);
152 /* Printing properties takes a lot of time, especially during backtraces.
153 For inspecting, it is better to explicitly use an inspection
161 Music::to_relative_octave (Pitch p)
163 SCM elt = get_property ("element");
165 if (Music *m = unsmob_music (elt))
166 p = m->to_relative_octave (p);
168 p = music_list_to_relative (get_property ("elements"), p, false);
173 Music::compress (Moment factor)
175 SCM elt = get_property ("element");
177 if (Music *m = unsmob_music (elt))
178 m->compress (factor);
180 compress_music_list (get_property ("elements"), factor);
181 Duration *d = unsmob_duration (get_property ("duration"));
183 set_property ("duration", d ->compressed (factor.main_part_).smobbed_copy ());
187 Music::transpose (Pitch delta)
189 if (to_boolean (get_property ("untransposable")))
192 for (SCM s = this->get_property_alist (true); scm_is_pair (s); s = scm_cdr (s))
194 SCM entry = scm_car (s);
195 SCM val = scm_cdr (entry);
197 if (Pitch * p = unsmob_pitch (val))
199 Pitch transposed = p->transposed (delta);
200 scm_set_cdr_x (entry, transposed.smobbed_copy ());
202 if (abs (transposed.get_alteration ()) > DOUBLE_SHARP)
204 warning (_f ("Transposition by %s makes alteration larger than double",
205 delta.to_string ()));
210 SCM elt = get_property ("element");
212 if (Music* m = unsmob_music (elt))
213 m->transpose (delta);
215 transpose_music_list (get_property ("elements"), delta);
218 IMPLEMENT_TYPE_P (Music, "ly:music?");
219 IMPLEMENT_SMOBS (Music);
220 IMPLEMENT_DEFAULT_EQUAL_P (Music);
223 Music::internal_get_property (SCM sym) const
225 SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
229 s = scm_sloppy_assq (sym, immutable_property_alist_);
230 return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
234 Music::internal_set_property (SCM s, SCM v)
236 if (do_internal_type_checking_global)
237 if (!type_check_assignment (s, v, ly_symbol2scm ("music-type?")))
240 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
244 Music::set_spot (Input ip)
246 set_property ("origin", make_input (ip));
250 Music::origin () const
252 Input *ip = unsmob_input (get_property ("origin"));
253 return ip ? ip : & dummy_input_global;
257 Music::duration_log () const
259 if (is_mus_type ("rhythmic-event"))
260 return unsmob_duration (get_property ("duration"))->duration_log ();
265 make_music_by_name (SCM sym)
267 SCM make_music_proc = ly_lily_module_constant ("make-music");
268 SCM rv = scm_call_1 (make_music_proc, sym);
271 scm_gc_protect_object (rv);
272 return unsmob_music (rv);