2 music.cc -- implement Music
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "dispatcher.hh"
13 #include "duration.hh"
15 #include "international.hh"
16 #include "ly-smobs.icc"
18 #include "music-sequence.hh"
23 Music is anything that has (possibly zero) duration and supports
24 both time compression and transposition.
26 In Lily, everything that can be thought to have a length and a pitch
27 (which has a duration which can be transposed) is considered "music".
30 Music::internal_is_music_type (SCM k) const
32 SCM ifs = get_property ("types");
34 return scm_c_memq (k, ifs) != SCM_BOOL_F;
37 Music::Music (SCM init)
38 : Prob (ly_symbol2scm ("Music"), init)
40 length_callback_ = SCM_EOL;
41 start_callback_ = SCM_EOL;
43 length_callback_ = get_property ("length-callback");
44 if (!ly_is_procedure (length_callback_))
45 length_callback_ = duration_length_callback_proc;
47 start_callback_ = get_property ("start-callback");
51 Music::derived_mark () const
53 scm_gc_mark (length_callback_);
54 scm_gc_mark (start_callback_);
58 Music::copy_mutable_properties () const
60 return ly_music_deep_copy (mutable_property_alist_);
64 Music::type_check_assignment (SCM s, SCM v) const
66 if (!::type_check_assignment (s, v, ly_symbol2scm ("music-type?")))
70 Music::Music (Music const &m)
73 length_callback_ = m.length_callback_;
74 start_callback_ = m.start_callback_;
77 set_spot (*m.origin ());
81 Music::get_length () const
83 SCM lst = get_property ("length");
84 if (unsmob_moment (lst))
85 return *unsmob_moment (lst);
87 if (ly_is_procedure (length_callback_))
89 SCM res = scm_call_1 (length_callback_, self_scm ());
90 return *unsmob_moment (res);
97 Music::start_mom () const
99 SCM lst = start_callback_;
100 if (ly_is_procedure (lst))
102 SCM res = scm_call_1 (lst, self_scm ());
103 return *unsmob_moment (res);
111 print_alist (SCM a, SCM port)
113 /* SCM_EOL -> catch malformed lists. */
114 for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
116 scm_display (scm_caar (s), port);
117 scm_puts (" = ", port);
118 scm_write (scm_cdar (s), port);
119 scm_puts ("\n", port);
125 Music::generic_to_relative_octave (Pitch last)
127 SCM elt = get_property ("element");
128 Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
131 Pitch new_pit = *old_pit;
132 new_pit = new_pit.to_relative_octave (last);
134 SCM check = get_property ("absolute-octave");
135 if (scm_is_number (check)
136 && new_pit.get_octave () != scm_to_int (check))
138 Pitch expected_pit (scm_to_int (check),
139 new_pit.get_notename (),
140 new_pit.get_alteration ());
141 origin ()->warning (_f ("octave check failed; expected \"%s\", found: \"%s\"",
142 expected_pit.to_string (),
143 new_pit.to_string ()));
144 new_pit = expected_pit;
147 set_property ("pitch", new_pit.smobbed_copy ());
152 if (Music *m = unsmob_music (elt))
153 last = m->to_relative_octave (last);
155 last = music_list_to_relative (get_property ("elements"), last, false);
160 Music::to_relative_octave (Pitch last)
162 SCM callback = get_property ("to-relative-callback");
163 if (ly_is_procedure (callback))
165 Pitch *p = unsmob_pitch (scm_call_2 (callback, self_scm (), last.smobbed_copy ()));
169 return generic_to_relative_octave (last);
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 This mutates alist. Hence, make sure that it is not shared
190 transpose_mutable (SCM alist, Pitch delta)
192 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
194 SCM entry = scm_car (s);
195 SCM prop = scm_car (entry);
196 SCM val = scm_cdr (entry);
199 if (Pitch *p = unsmob_pitch (val))
201 Pitch transposed = p->transposed (delta);
202 if (transposed.get_alteration ().abs () > Rational (1,1))
204 warning (_f ("transposition by %s makes alteration larger than double",
205 delta.to_string ()));
208 new_val = transposed.smobbed_copy ();
210 else if (prop == ly_symbol2scm ("element"))
212 if (Music *m = unsmob_music (val))
213 m->transpose (delta);
215 else if (prop == ly_symbol2scm ("elements"))
216 transpose_music_list (val, delta);
217 else if (prop == ly_symbol2scm ("pitch-alist") &&
219 new_val = ly_transpose_key_alist (val, delta.smobbed_copy ());
222 scm_set_cdr_x (entry , new_val);
227 Music::transpose (Pitch delta)
229 if (to_boolean (get_property ("untransposable")))
232 transpose_mutable (mutable_property_alist_, delta);
236 Music::set_spot (Input ip)
238 set_property ("origin", make_input (ip));
242 Music::origin () const
244 Input *ip = unsmob_input (get_property ("origin"));
245 return ip ? ip : &dummy_input_global;
249 ES TODO: This method should probably be reworked or junked.
252 Music::to_event () const
254 SCM class_name = ly_camel_case_to_lisp_identifier (get_property ("name"));
256 // catch programming mistakes.
257 if (!internal_is_music_type (class_name))
259 programming_error ("Not a music type");
262 Stream_event *e = new Stream_event (class_name, mutable_property_alist_);
263 Moment length = get_length ();
264 if (length.to_bool ())
265 e->set_property ("length", length.smobbed_copy ());
267 // articulations as events.
268 SCM art_mus = e->get_property ("articulations");
269 if (scm_is_pair (art_mus))
271 SCM art_ev = SCM_EOL;
272 for (; scm_is_pair (art_mus); art_mus = scm_cdr (art_mus))
274 Music *m = unsmob_music (scm_car (art_mus));
275 SCM ev = m ? m->to_event ()->unprotect () : scm_car (art_mus);
276 art_ev = scm_cons (ev, art_ev);
278 e->set_property ("articulations", scm_reverse_x (art_ev, SCM_EOL));
282 ES TODO: This is a temporary fix. Stream_events should not be
285 e->set_property ("music-cause", self_scm ());
291 Music::send_to_context (Context *c)
293 Stream_event *ev = to_event ();
294 c->event_source ()->broadcast (ev);
299 make_music_by_name (SCM sym)
301 SCM make_music_proc = ly_lily_module_constant ("make-music");
302 SCM rv = scm_call_1 (make_music_proc, sym);
305 Music *m = unsmob_music (rv);
310 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
312 Music::duration_length_callback (SCM m)
314 Music *me = unsmob_music (m);
315 Duration *d = unsmob_duration (me->get_property ("duration"));
319 mom = d->get_length ();
320 return mom.smobbed_copy ();
326 return dynamic_cast<Music*> (unsmob_prob (m));