2 music.cc -- implement Music
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "music-sequence.hh"
11 #include "duration.hh"
12 #include "input-smob.hh"
13 #include "ly-smobs.icc"
20 Music is anything that has duration and supports both time compression
23 In Lily, everything that can be thought to have a length and a pitch
24 (which has a duration which can be transposed) is considered "music",
27 Music::internal_is_music_type (SCM k) const
29 SCM ifs = get_property ("types");
31 return scm_c_memq (k, ifs) != SCM_BOOL_F;
37 SCM nm = get_property ("name");
38 if (scm_is_symbol (nm))
40 return ly_symbol2string (nm);
44 return classname (this);
48 Music::Music (SCM init)
51 immutable_property_alist_ = init;
52 mutable_property_alist_ = SCM_EOL;
55 length_callback_ = get_property ("length-callback");
56 if (!ly_is_procedure (length_callback_))
58 length_callback_ = duration_length_callback_proc;
61 start_callback_ = get_property ("start-callback");
64 Music::Music (Music const &m)
66 immutable_property_alist_ = m.immutable_property_alist_;
67 mutable_property_alist_ = SCM_EOL;
70 /* First we smobify_self, then we copy over the stuff. If we don't,
71 stack vars that hold the copy might be optimized away, meaning
72 that they won't be protected from GC. */
74 mutable_property_alist_ = ly_music_deep_copy (m.mutable_property_alist_);
75 length_callback_ = m.length_callback_;
76 start_callback_ = m.start_callback_;
77 set_spot (*m.origin ());
86 Music::get_property_alist (bool m) const
88 return (m) ? mutable_property_alist_ : immutable_property_alist_;
92 Music::mark_smob (SCM m)
94 Music *mus = (Music *) SCM_CELL_WORD_1 (m);
95 scm_gc_mark (mus->immutable_property_alist_);
96 scm_gc_mark (mus->mutable_property_alist_);
101 Music::get_length () const
103 SCM lst = get_property ("length");
104 if (unsmob_moment (lst))
105 return *unsmob_moment (lst);
107 if (ly_is_procedure (length_callback_))
109 SCM res = scm_call_1 (length_callback_, self_scm ());
110 return *unsmob_moment (res);
117 Music::start_mom () const
119 SCM lst = start_callback_;
120 if (ly_is_procedure (lst))
122 SCM res = scm_call_1 (lst, self_scm ());
123 return *unsmob_moment (res);
131 print_alist (SCM a, SCM port)
133 /* SCM_EOL -> catch malformed lists. */
134 for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
136 scm_display (scm_caar (s), port);
137 scm_puts (" = ", port);
138 scm_write (scm_cdar (s), port);
139 scm_puts ("\n", port);
144 Music::print_smob (SCM s, SCM p, scm_print_state*)
146 scm_puts ("#<Music ", p);
147 Music *m = unsmob_music (s);
149 SCM nm = m->get_property ("name");
150 if (scm_is_symbol (nm) || scm_is_string (nm))
153 scm_puts (classname (m), p);
155 /* Printing properties takes a lot of time, especially during backtraces.
156 For inspecting, it is better to explicitly use an inspection
164 Music::generic_to_relative_octave (Pitch last)
166 SCM elt = get_property ("element");
167 Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
170 Pitch new_pit = *old_pit;
171 new_pit = new_pit.to_relative_octave (last);
173 SCM check = get_property ("absolute-octave");
174 if (scm_is_number (check)
175 && new_pit.get_octave () != scm_to_int (check))
177 Pitch expected_pit (scm_to_int (check),
178 new_pit.get_notename (),
179 new_pit.get_alteration ());
180 origin ()->warning (_f ("octave check failed; expected %s, found: %s",
181 expected_pit.to_string (),
182 new_pit.to_string ()));
183 new_pit = expected_pit;
186 set_property ("pitch", new_pit.smobbed_copy ());
191 if (Music *m = unsmob_music (elt))
192 last = m->to_relative_octave (last);
194 last = music_list_to_relative (get_property ("elements"), last, false);
199 Music::to_relative_octave (Pitch last)
201 SCM callback = get_property ("to-relative-callback");
202 if (ly_is_procedure (callback))
204 Pitch *p = unsmob_pitch (scm_call_2 (callback, self_scm (), last.smobbed_copy ()));
208 return generic_to_relative_octave (last);
212 Music::compress (Moment factor)
214 SCM elt = get_property ("element");
216 if (Music *m = unsmob_music (elt))
217 m->compress (factor);
219 compress_music_list (get_property ("elements"), factor);
220 Duration *d = unsmob_duration (get_property ("duration"));
222 set_property ("duration", d->compressed (factor.main_part_).smobbed_copy ());
226 Music::transpose (Pitch delta)
228 if (to_boolean (get_property ("untransposable")))
231 for (SCM s = this->get_property_alist (true); scm_is_pair (s); s = scm_cdr (s))
233 SCM entry = scm_car (s);
234 SCM val = scm_cdr (entry);
236 if (Pitch *p = unsmob_pitch (val))
238 Pitch transposed = p->transposed (delta);
239 scm_set_cdr_x (entry, transposed.smobbed_copy ());
241 if (abs (transposed.get_alteration ()) > DOUBLE_SHARP)
243 warning (_f ("transposition by %s makes alteration larger than double",
244 delta.to_string ()));
249 SCM elt = get_property ("element");
251 if (Music *m = unsmob_music (elt))
252 m->transpose (delta);
254 transpose_music_list (get_property ("elements"), delta);
257 UGH - how do this more generically?
259 SCM pa = get_property ("pitch-alist");
260 if (scm_is_pair (pa))
262 set_property ("pitch-alist", ly_transpose_key_alist (pa, delta.smobbed_copy ()));
266 IMPLEMENT_TYPE_P (Music, "ly:music?");
267 IMPLEMENT_SMOBS (Music);
268 IMPLEMENT_DEFAULT_EQUAL_P (Music);
271 Music::internal_get_property (SCM sym) const
273 SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
277 s = scm_sloppy_assq (sym, immutable_property_alist_);
278 return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
282 Music::internal_set_property (SCM s, SCM v)
284 if (do_internal_type_checking_global)
285 if (!type_check_assignment (s, v, ly_symbol2scm ("music-type?")))
288 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
292 Music::set_spot (Input ip)
294 set_property ("origin", make_input (ip));
298 Music::origin () const
300 Input *ip = unsmob_input (get_property ("origin"));
301 return ip ? ip : &dummy_input_global;
305 make_music_by_name (SCM sym)
307 SCM make_music_proc = ly_lily_module_constant ("make-music");
308 SCM rv = scm_call_1 (make_music_proc, sym);
311 scm_gc_protect_object (rv);
312 return unsmob_music (rv);
316 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
318 Music::duration_length_callback (SCM m)
320 Music *me = unsmob_music (m);
321 Duration *d = unsmob_duration (me->get_property ("duration"));
326 mom = d->get_length ();
328 return mom.smobbed_copy ();