2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
23 #include "dispatcher.hh"
24 #include "duration.hh"
26 #include "international.hh"
28 #include "music-sequence.hh"
31 #include "lily-imports.hh"
34 Music is anything that has (possibly zero) duration and supports
35 both time compression and transposition.
37 In Lily, everything that can be thought to have a length and a pitch
38 (which has a duration which can be transposed) is considered "music".
41 Music::internal_is_music_type (SCM k) const
43 SCM ifs = get_property ("types");
45 return scm_is_true (scm_c_memq (k, ifs));
51 length_callback_ = SCM_EOL;
52 start_callback_ = SCM_EOL;
55 Music::Music (SCM init)
56 : Prob (ly_symbol2scm ("Music"), init)
58 length_callback_ = get_property ("length-callback");
59 if (!ly_is_procedure (length_callback_))
60 length_callback_ = duration_length_callback_proc;
62 start_callback_ = get_property ("start-callback");
66 Music::derived_mark () const
68 scm_gc_mark (length_callback_);
69 scm_gc_mark (start_callback_);
73 Music::copy_mutable_properties () const
75 return music_deep_copy (mutable_property_alist_);
79 Music::type_check_assignment (SCM s, SCM v) const
81 ::type_check_assignment (s, v, ly_symbol2scm ("music-type?"));
84 Music::Music (Music const &m)
87 length_callback_ = m.length_callback_;
88 start_callback_ = m.start_callback_;
91 set_spot (*m.origin ());
95 Music::get_length () const
97 SCM lst = get_property ("length");
98 if (unsmob<Moment> (lst))
99 return *unsmob<Moment> (lst);
101 if (ly_is_procedure (length_callback_))
103 SCM res = scm_call_1 (length_callback_, self_scm ());
104 return *unsmob<Moment> (res);
111 Music::start_mom () const
113 SCM lst = start_callback_;
114 if (ly_is_procedure (lst))
116 SCM res = scm_call_1 (lst, self_scm ());
117 return *unsmob<Moment> (res);
125 print_alist (SCM a, SCM port)
127 /* SCM_EOL -> catch malformed lists. */
128 for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
130 scm_display (scm_caar (s), port);
131 scm_puts (" = ", port);
132 scm_write (scm_cdar (s), port);
133 scm_puts ("\n", port);
138 Music::generic_to_relative_octave (Pitch last)
140 SCM elt = get_property ("element");
141 Pitch *old_pit = unsmob<Pitch> (get_property ("pitch"));
144 Pitch new_pit = *old_pit;
145 new_pit = new_pit.to_relative_octave (last);
147 SCM check = get_property ("absolute-octave");
148 if (scm_is_number (check)
149 && new_pit.get_octave () != scm_to_int (check))
151 Pitch expected_pit (scm_to_int (check),
152 new_pit.get_notename (),
153 new_pit.get_alteration ());
154 origin ()->warning (_f ("octave check failed; expected \"%s\", found: \"%s\"",
155 expected_pit.to_string (),
156 new_pit.to_string ()));
157 new_pit = expected_pit;
160 set_property ("pitch", new_pit.smobbed_copy ());
165 if (Music *m = unsmob<Music> (elt))
166 last = m->to_relative_octave (last);
168 (void) music_list_to_relative (get_property ("articulations"), last, true);
169 last = music_list_to_relative (get_property ("elements"), last, false);
174 Music::to_relative_octave (Pitch last)
176 SCM callback = get_property ("to-relative-callback");
177 if (ly_is_procedure (callback))
179 Pitch *p = unsmob<Pitch> (scm_call_2 (callback, self_scm (),
180 last.smobbed_copy ()));
184 return generic_to_relative_octave (last);
188 Music::compress (Moment factor)
190 SCM elt = get_property ("element");
192 if (Music *m = unsmob<Music> (elt))
193 m->compress (factor);
195 compress_music_list (get_property ("elements"), factor);
196 Duration *d = unsmob<Duration> (get_property ("duration"));
198 set_property ("duration",
199 d->compressed (factor.main_part_).smobbed_copy ());
203 This mutates alist. Hence, make sure that it is not shared
207 Prob::transpose (Pitch delta)
209 if (to_boolean (get_property ("untransposable")))
212 for (SCM s = mutable_property_alist_; scm_is_pair (s); s = scm_cdr (s))
214 SCM entry = scm_car (s);
215 SCM prop = scm_car (entry);
216 SCM val = scm_cdr (entry);
219 if (Pitch *p = unsmob<Pitch> (val))
221 Pitch transposed = p->transposed (delta);
223 if (scm_is_eq (prop, ly_symbol2scm ("tonic")))
224 transposed = Pitch (-1, transposed.get_notename (),
225 transposed.get_alteration ());
227 new_val = transposed.smobbed_copy ();
229 else if (scm_is_eq (prop, ly_symbol2scm ("element")))
231 if (Prob *m = unsmob<Prob> (val))
232 m->transpose (delta);
234 else if (scm_is_eq (prop, ly_symbol2scm ("elements"))
235 || scm_is_eq (prop, ly_symbol2scm ("articulations")))
236 transpose_music_list (val, delta);
237 else if (scm_is_eq (prop, ly_symbol2scm ("pitch-alist"))
238 && scm_is_pair (val))
239 new_val = ly_transpose_key_alist (val, delta.smobbed_copy ());
242 scm_set_cdr_x (entry, new_val);
247 Music::set_spot (Input ip)
249 set_property ("origin", ip.smobbed_copy ());
253 Music::origin () const
255 Input *ip = unsmob<Input> (get_property ("origin"));
256 return ip ? ip : &dummy_input_global;
260 ES TODO: This method should probably be reworked or junked.
263 Music::to_event () const
265 SCM class_name = ly_camel_case_2_lisp_identifier (get_property ("name"));
267 // catch programming mistakes.
268 if (!internal_is_music_type (class_name))
269 programming_error ("Not a music type");
271 Stream_event *e = new Stream_event
272 (Lily::ly_make_event_class (class_name),
273 mutable_property_alist_);
274 Moment length = get_length ();
275 if (length.to_bool ())
276 e->set_property ("length", length.smobbed_copy ());
278 // articulations as events.
279 SCM art_mus = e->get_property ("articulations");
280 if (scm_is_pair (art_mus))
282 SCM art_ev = SCM_EOL;
283 for (; scm_is_pair (art_mus); art_mus = scm_cdr (art_mus))
285 Music *m = unsmob<Music> (scm_car (art_mus));
286 art_ev = scm_cons (m->to_event ()->unprotect (), art_ev);
288 e->set_property ("articulations", scm_reverse_x (art_ev, SCM_EOL));
292 ES TODO: This is a temporary fix. Stream_events should not be
295 e->set_property ("music-cause", self_scm ());
301 Music::send_to_context (Context *c)
303 Stream_event *ev = to_event ();
304 c->event_source ()->broadcast (ev);
309 make_music_by_name (SCM sym)
311 SCM rv = Lily::make_music (sym);
314 Music *m = unsmob<Music> (rv);
319 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
321 Music::duration_length_callback (SCM m)
323 Music *me = unsmob<Music> (m);
324 Duration *d = unsmob<Duration> (me->get_property ("duration"));
328 mom = d->get_length ();
329 return mom.smobbed_copy ();
333 music_deep_copy (SCM m)
335 if (Music *mus = unsmob<Music> (m))
336 return mus->clone ()->unprotect ();
342 copy = scm_cons (music_deep_copy (scm_car (m)), copy);
345 while (scm_is_pair (m));
346 // Oh, come on, GUILE. Why do you require the second argument
347 // of scm_reverse_x to be a proper list? That makes no sense.
348 // return scm_reverse_x (copy, music_deep_copy (m));
349 SCM last_cons = copy;
350 copy = scm_reverse_x (copy, SCM_EOL);
351 scm_set_cdr_x (last_cons, music_deep_copy (m));
358 set_origin (SCM m, SCM origin)
360 while (scm_is_pair (m))
362 set_origin (scm_car (m), origin);
365 if (Music *mus = unsmob<Music> (m))
366 mus->set_property ("origin", origin);