2 music.cc -- implement Music
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 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"
24 Music is anything that has (possibly zero) duration and supports
25 both time compression and transposition.
27 In Lily, everything that can be thought to have a length and a pitch
28 (which has a duration which can be transposed) is considered "music".
31 Music::internal_is_music_type (SCM k) const
33 SCM ifs = get_property ("types");
35 return scm_c_memq (k, ifs) != SCM_BOOL_F;
38 Music::Music (SCM init)
39 : Prob (ly_symbol2scm ("Music"), init)
41 length_callback_ = SCM_EOL;
42 start_callback_ = SCM_EOL;
44 length_callback_ = get_property ("length-callback");
45 if (!ly_is_procedure (length_callback_))
46 length_callback_ = duration_length_callback_proc;
48 start_callback_ = get_property ("start-callback");
52 Music::derived_mark () const
54 scm_gc_mark (length_callback_);
55 scm_gc_mark (start_callback_);
59 Music::copy_mutable_properties () const
61 return ly_music_deep_copy (mutable_property_alist_);
65 Music::type_check_assignment (SCM s, SCM v) const
67 if (!::type_check_assignment (s, v, ly_symbol2scm ("music-type?")))
71 Music::Music (Music const &m)
74 length_callback_ = m.length_callback_;
75 start_callback_ = m.start_callback_;
78 set_spot (*m.origin ());
82 Music::get_length () const
84 SCM lst = get_property ("length");
85 if (unsmob_moment (lst))
86 return *unsmob_moment (lst);
88 if (ly_is_procedure (length_callback_))
90 SCM res = scm_call_1 (length_callback_, self_scm ());
91 return *unsmob_moment (res);
98 Music::start_mom () const
100 SCM lst = start_callback_;
101 if (ly_is_procedure (lst))
103 SCM res = scm_call_1 (lst, self_scm ());
104 return *unsmob_moment (res);
112 print_alist (SCM a, SCM port)
114 /* SCM_EOL -> catch malformed lists. */
115 for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
117 scm_display (scm_caar (s), port);
118 scm_puts (" = ", port);
119 scm_write (scm_cdar (s), port);
120 scm_puts ("\n", port);
126 Music::generic_to_relative_octave (Pitch last)
128 SCM elt = get_property ("element");
129 Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
132 Pitch new_pit = *old_pit;
133 new_pit = new_pit.to_relative_octave (last);
135 SCM check = get_property ("absolute-octave");
136 if (scm_is_number (check)
137 && new_pit.get_octave () != scm_to_int (check))
139 Pitch expected_pit (scm_to_int (check),
140 new_pit.get_notename (),
141 new_pit.get_alteration ());
142 origin ()->warning (_f ("octave check failed; expected \"%s\", found: %s",
143 expected_pit.to_string (),
144 new_pit.to_string ()));
145 new_pit = expected_pit;
148 set_property ("pitch", new_pit.smobbed_copy ());
153 if (Music *m = unsmob_music (elt))
154 last = m->to_relative_octave (last);
156 last = music_list_to_relative (get_property ("elements"), last, false);
161 Music::to_relative_octave (Pitch last)
163 SCM callback = get_property ("to-relative-callback");
164 if (ly_is_procedure (callback))
166 Pitch *p = unsmob_pitch (scm_call_2 (callback, self_scm (), last.smobbed_copy ()));
170 return generic_to_relative_octave (last);
174 Music::compress (Moment factor)
176 SCM elt = get_property ("element");
178 if (Music *m = unsmob_music (elt))
179 m->compress (factor);
181 compress_music_list (get_property ("elements"), factor);
182 Duration *d = unsmob_duration (get_property ("duration"));
184 set_property ("duration", d->compressed (factor.main_part_).smobbed_copy ());
188 TODO: make transposition non-destructive
191 transpose_mutable (SCM alist, Pitch delta)
193 SCM retval = SCM_EOL;
195 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
197 SCM entry = scm_car (s);
198 SCM prop = scm_car (entry);
199 SCM val = scm_cdr (entry);
201 if (Pitch *p = unsmob_pitch (val))
203 Pitch transposed = p->transposed (delta);
204 scm_set_cdr_x (entry, transposed.smobbed_copy ());
206 if (abs (transposed.get_alteration ()) > DOUBLE_SHARP)
208 warning (_f ("transposition by %s makes alteration larger than double",
209 delta.to_string ()));
212 else if (prop == ly_symbol2scm ("element"))
214 if (Music *m = unsmob_music (val))
215 m->transpose (delta);
217 else if (prop == ly_symbol2scm ("elements"))
218 transpose_music_list (val, delta);
219 else if (prop == ly_symbol2scm ("pitch-alist") &&
221 entry = scm_cons (prop, ly_transpose_key_alist (val, delta.smobbed_copy ()));
222 retval = scm_cons (entry, retval);
225 return scm_reverse_x (retval, SCM_EOL);
229 Music::transpose (Pitch delta)
231 if (to_boolean (get_property ("untransposable")))
234 mutable_property_alist_ = transpose_mutable (mutable_property_alist_, delta);
238 Music::set_spot (Input ip)
240 set_property ("origin", make_input (ip));
244 Music::origin () const
246 Input *ip = unsmob_input (get_property ("origin"));
247 return ip ? ip : &dummy_input_global;
251 ES TODO: This method should probably be reworked or junked.
254 Music::to_event () const
256 SCM class_name = ly_camel_case_to_lisp_identifier (get_property ("name"));
258 // catch programming mistakes.
259 if (!internal_is_music_type (class_name))
261 programming_error ("Not a music type");
264 Stream_event *e = new Stream_event (class_name, mutable_property_alist_);
265 Moment length = get_length ();
266 if (length.to_bool ())
267 e->set_property ("length", length.smobbed_copy ());
269 // articulations as events.
270 SCM art_mus = e->get_property ("articulations");
271 if (scm_is_pair (art_mus))
273 SCM art_ev = SCM_EOL;
274 for (; scm_is_pair (art_mus); art_mus = scm_cdr (art_mus))
276 Music *m = unsmob_music (scm_car (art_mus));
277 SCM ev = m ? m->to_event ()->unprotect () : scm_car (art_mus);
278 art_ev = scm_cons (ev, art_ev);
280 e->set_property ("articulations", scm_reverse_x (art_ev, SCM_EOL));
284 ES TODO: This is a temporary fix. Stream_events should not be
287 e->set_property ("music-cause", self_scm ());
293 Music::send_to_context (Context *c)
295 Stream_event *ev = to_event ();
296 c->event_source ()->broadcast (ev);
301 make_music_by_name (SCM sym)
303 SCM make_music_proc = ly_lily_module_constant ("make-music");
304 SCM rv = scm_call_1 (make_music_proc, sym);
307 Music *m = unsmob_music (rv);
312 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
314 Music::duration_length_callback (SCM m)
316 Music *me = unsmob_music (m);
317 Duration *d = unsmob_duration (me->get_property ("duration"));
321 mom = d->get_length ();
322 return mom.smobbed_copy ();
328 return dynamic_cast<Music*> (unsmob_prob (m));