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));
48 Preinit_Music::Preinit_Music ()
50 length_callback_ = SCM_EOL;
51 start_callback_ = SCM_EOL;
54 Music::Music (SCM init)
55 : Prob (ly_symbol2scm ("Music"), init)
57 length_callback_ = get_property ("length-callback");
58 if (!ly_is_procedure (length_callback_))
59 length_callback_ = duration_length_callback_proc;
61 start_callback_ = get_property ("start-callback");
65 Music::derived_mark () const
67 scm_gc_mark (length_callback_);
68 scm_gc_mark (start_callback_);
72 Music::copy_mutable_properties () const
74 return music_deep_copy (mutable_property_alist_);
78 Music::type_check_assignment (SCM s, SCM v) const
80 ::type_check_assignment (s, v, ly_symbol2scm ("music-type?"));
83 Music::Music (Music const &m)
86 length_callback_ = m.length_callback_;
87 start_callback_ = m.start_callback_;
90 set_spot (*m.origin ());
94 Music::get_length () const
96 SCM lst = get_property ("length");
97 if (unsmob<Moment> (lst))
98 return *unsmob<Moment> (lst);
100 if (ly_is_procedure (length_callback_))
102 SCM res = scm_call_1 (length_callback_, self_scm ());
103 return *unsmob<Moment> (res);
110 Music::start_mom () const
112 SCM lst = start_callback_;
113 if (ly_is_procedure (lst))
115 SCM res = scm_call_1 (lst, self_scm ());
116 return *unsmob<Moment> (res);
124 print_alist (SCM a, SCM port)
126 /* SCM_EOL -> catch malformed lists. */
127 for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
129 scm_display (scm_caar (s), port);
130 scm_puts (" = ", port);
131 scm_write (scm_cdar (s), port);
132 scm_puts ("\n", port);
137 Music::generic_to_relative_octave (Pitch last)
139 SCM elt = get_property ("element");
140 Pitch *old_pit = unsmob<Pitch> (get_property ("pitch"));
143 Pitch new_pit = *old_pit;
144 new_pit = new_pit.to_relative_octave (last);
146 SCM check = get_property ("absolute-octave");
147 if (scm_is_number (check)
148 && new_pit.get_octave () != scm_to_int (check))
150 Pitch expected_pit (scm_to_int (check),
151 new_pit.get_notename (),
152 new_pit.get_alteration ());
153 origin ()->warning (_f ("octave check failed; expected \"%s\", found: \"%s\"",
154 expected_pit.to_string (),
155 new_pit.to_string ()));
156 new_pit = expected_pit;
159 set_property ("pitch", new_pit.smobbed_copy ());
164 if (Music *m = unsmob<Music> (elt))
165 last = m->to_relative_octave (last);
167 (void) music_list_to_relative (get_property ("articulations"), last, true);
168 last = music_list_to_relative (get_property ("elements"), last, false);
173 Music::to_relative_octave (Pitch last)
175 SCM callback = get_property ("to-relative-callback");
176 if (ly_is_procedure (callback))
178 Pitch *p = unsmob<Pitch> (scm_call_2 (callback, self_scm (),
179 last.smobbed_copy ()));
183 return generic_to_relative_octave (last);
187 Music::compress (Moment factor)
189 SCM elt = get_property ("element");
191 if (Music *m = unsmob<Music> (elt))
192 m->compress (factor);
194 compress_music_list (get_property ("elements"), factor);
195 Duration *d = unsmob<Duration> (get_property ("duration"));
197 set_property ("duration",
198 d->compressed (factor.main_part_).smobbed_copy ());
202 This mutates alist. Hence, make sure that it is not shared
206 Prob::transpose (Pitch delta)
208 if (to_boolean (get_property ("untransposable")))
211 for (SCM s = mutable_property_alist_; scm_is_pair (s); s = scm_cdr (s))
213 SCM entry = scm_car (s);
214 SCM prop = scm_car (entry);
215 SCM val = scm_cdr (entry);
218 if (Pitch *p = unsmob<Pitch> (val))
220 Pitch transposed = p->transposed (delta);
222 if (scm_is_eq (prop, ly_symbol2scm ("tonic")))
223 transposed = Pitch (-1, transposed.get_notename (),
224 transposed.get_alteration ());
226 new_val = transposed.smobbed_copy ();
228 else if (scm_is_eq (prop, ly_symbol2scm ("element")))
230 if (Prob *m = unsmob<Prob> (val))
231 m->transpose (delta);
233 else if (scm_is_eq (prop, ly_symbol2scm ("elements"))
234 || scm_is_eq (prop, ly_symbol2scm ("articulations")))
235 transpose_music_list (val, delta);
236 else if (scm_is_eq (prop, ly_symbol2scm ("pitch-alist"))
237 && scm_is_pair (val))
238 new_val = ly_transpose_key_alist (val, delta.smobbed_copy ());
240 if (!scm_is_eq (val, new_val))
241 scm_set_cdr_x (entry, new_val);
246 Music::set_spot (Input ip)
248 set_property ("origin", ip.smobbed_copy ());
252 Music::origin () const
254 Input *ip = unsmob<Input> (get_property ("origin"));
255 return ip ? ip : &dummy_input_global;
259 ES TODO: This method should probably be reworked or junked.
262 Music::to_event () const
264 SCM class_name = ly_camel_case_2_lisp_identifier (get_property ("name"));
266 // catch programming mistakes.
267 if (!internal_is_music_type (class_name))
268 programming_error ("Not a music type");
270 Stream_event *e = new Stream_event
271 (Lily::ly_make_event_class (class_name),
272 mutable_property_alist_);
273 Moment length = get_length ();
274 if (length.to_bool ())
275 e->set_property ("length", length.smobbed_copy ());
277 // articulations as events.
278 SCM art_mus = e->get_property ("articulations");
279 if (scm_is_pair (art_mus))
281 SCM art_ev = SCM_EOL;
282 for (; scm_is_pair (art_mus); art_mus = scm_cdr (art_mus))
284 Music *m = unsmob<Music> (scm_car (art_mus));
285 art_ev = scm_cons (m->to_event ()->unprotect (), art_ev);
287 e->set_property ("articulations", scm_reverse_x (art_ev, SCM_EOL));
291 ES TODO: This is a temporary fix. Stream_events should not be
294 e->set_property ("music-cause", self_scm ());
300 Music::send_to_context (Context *c)
302 Stream_event *ev = to_event ();
303 c->event_source ()->broadcast (ev);
308 make_music_by_name (SCM sym)
310 SCM rv = Lily::make_music (sym);
313 Music *m = unsmob<Music> (rv);
318 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
320 Music::duration_length_callback (SCM m)
322 Music *me = unsmob<Music> (m);
323 Duration *d = unsmob<Duration> (me->get_property ("duration"));
327 mom = d->get_length ();
328 return mom.smobbed_copy ();
332 music_deep_copy (SCM m)
334 if (Music *mus = unsmob<Music> (m))
335 return mus->clone ()->unprotect ();
341 copy = scm_cons (music_deep_copy (scm_car (m)), copy);
344 while (scm_is_pair (m));
345 // Oh, come on, GUILE. Why do you require the second argument
346 // of scm_reverse_x to be a proper list? That makes no sense.
347 // return scm_reverse_x (copy, music_deep_copy (m));
348 SCM last_cons = copy;
349 copy = scm_reverse_x (copy, SCM_EOL);
350 scm_set_cdr_x (last_cons, music_deep_copy (m));
357 set_origin (SCM m, SCM origin)
359 while (scm_is_pair (m))
361 set_origin (scm_car (m), origin);
364 if (Music *mus = unsmob<Music> (m))
365 mus->set_property ("origin", origin);