]> git.donarmstrong.com Git - lilypond.git/blob - lily/music.cc
Issue 4997/3: Use Preinit in Music
[lilypond.git] / lily / music.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "music.hh"
21
22 #include "context.hh"
23 #include "dispatcher.hh"
24 #include "duration.hh"
25 #include "input.hh"
26 #include "international.hh"
27 #include "main.hh"
28 #include "music-sequence.hh"
29 #include "score.hh"
30 #include "warn.hh"
31 #include "lily-imports.hh"
32
33 /*
34   Music is anything that has (possibly zero) duration and supports
35   both time compression and transposition.
36
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".
39 */
40 bool
41 Music::internal_is_music_type (SCM k) const
42 {
43   SCM ifs = get_property ("types");
44
45   return scm_is_true (scm_c_memq (k, ifs));
46 }
47
48 void
49 Music::pre_init ()
50 {
51   length_callback_ = SCM_EOL;
52   start_callback_ = SCM_EOL;
53 }
54
55 Music::Music (SCM init)
56   : Prob (ly_symbol2scm ("Music"), init)
57 {
58   length_callback_ = get_property ("length-callback");
59   if (!ly_is_procedure (length_callback_))
60     length_callback_ = duration_length_callback_proc;
61
62   start_callback_ = get_property ("start-callback");
63 }
64
65 void
66 Music::derived_mark () const
67 {
68   scm_gc_mark (length_callback_);
69   scm_gc_mark (start_callback_);
70 }
71
72 SCM
73 Music::copy_mutable_properties () const
74 {
75   return music_deep_copy (mutable_property_alist_);
76 }
77
78 void
79 Music::type_check_assignment (SCM s, SCM v) const
80 {
81   ::type_check_assignment (s, v, ly_symbol2scm ("music-type?"));
82 }
83
84 Music::Music (Music const &m)
85   : Prob (m)
86 {
87   length_callback_ = m.length_callback_;
88   start_callback_ = m.start_callback_;
89
90   /// why?
91   set_spot (*m.origin ());
92 }
93
94 Moment
95 Music::get_length () const
96 {
97   SCM lst = get_property ("length");
98   if (unsmob<Moment> (lst))
99     return *unsmob<Moment> (lst);
100
101   if (ly_is_procedure (length_callback_))
102     {
103       SCM res = scm_call_1 (length_callback_, self_scm ());
104       return *unsmob<Moment> (res);
105     }
106
107   return Moment (0);
108 }
109
110 Moment
111 Music::start_mom () const
112 {
113   SCM lst = start_callback_;
114   if (ly_is_procedure (lst))
115     {
116       SCM res = scm_call_1 (lst, self_scm ());
117       return *unsmob<Moment> (res);
118     }
119
120   Moment m;
121   return m;
122 }
123
124 void
125 print_alist (SCM a, SCM port)
126 {
127   /* SCM_EOL  -> catch malformed lists.  */
128   for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
129     {
130       scm_display (scm_caar (s), port);
131       scm_puts (" = ", port);
132       scm_write (scm_cdar (s), port);
133       scm_puts ("\n", port);
134     }
135 }
136
137 Pitch
138 Music::generic_to_relative_octave (Pitch last)
139 {
140   SCM elt = get_property ("element");
141   Pitch *old_pit = unsmob<Pitch> (get_property ("pitch"));
142   if (old_pit)
143     {
144       Pitch new_pit = *old_pit;
145       new_pit = new_pit.to_relative_octave (last);
146
147       SCM check = get_property ("absolute-octave");
148       if (scm_is_number (check)
149           && new_pit.get_octave () != scm_to_int (check))
150         {
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;
158         }
159
160       set_property ("pitch", new_pit.smobbed_copy ());
161
162       last = new_pit;
163     }
164
165   if (Music *m = unsmob<Music> (elt))
166     last = m->to_relative_octave (last);
167
168   (void) music_list_to_relative (get_property ("articulations"), last, true);
169   last = music_list_to_relative (get_property ("elements"), last, false);
170   return last;
171 }
172
173 Pitch
174 Music::to_relative_octave (Pitch last)
175 {
176   SCM callback = get_property ("to-relative-callback");
177   if (ly_is_procedure (callback))
178     {
179       Pitch *p = unsmob<Pitch> (scm_call_2 (callback, self_scm (),
180                                            last.smobbed_copy ()));
181       return *p;
182     }
183
184   return generic_to_relative_octave (last);
185 }
186
187 void
188 Music::compress (Moment factor)
189 {
190   SCM elt = get_property ("element");
191
192   if (Music *m = unsmob<Music> (elt))
193     m->compress (factor);
194
195   compress_music_list (get_property ("elements"), factor);
196   Duration *d = unsmob<Duration> (get_property ("duration"));
197   if (d)
198     set_property ("duration",
199                   d->compressed (factor.main_part_).smobbed_copy ());
200 }
201
202 /*
203   This mutates alist.  Hence, make sure that it is not shared
204 */
205
206 void
207 Prob::transpose (Pitch delta)
208 {
209   if (to_boolean (get_property ("untransposable")))
210     return;
211
212   for (SCM s = mutable_property_alist_; scm_is_pair (s); s = scm_cdr (s))
213     {
214       SCM entry = scm_car (s);
215       SCM prop = scm_car (entry);
216       SCM val = scm_cdr (entry);
217       SCM new_val = val;
218
219       if (Pitch *p = unsmob<Pitch> (val))
220         {
221           Pitch transposed = p->transposed (delta);
222
223           if (scm_is_eq (prop, ly_symbol2scm ("tonic")))
224             transposed = Pitch (-1, transposed.get_notename (),
225                                 transposed.get_alteration ());
226
227           new_val = transposed.smobbed_copy ();
228         }
229       else if (scm_is_eq (prop, ly_symbol2scm ("element")))
230         {
231           if (Prob *m = unsmob<Prob> (val))
232             m->transpose (delta);
233         }
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 ());
240
241       if (val != new_val)
242         scm_set_cdr_x (entry, new_val);
243     }
244 }
245
246 void
247 Music::set_spot (Input ip)
248 {
249   set_property ("origin", ip.smobbed_copy ());
250 }
251
252 Input *
253 Music::origin () const
254 {
255   Input *ip = unsmob<Input> (get_property ("origin"));
256   return ip ? ip : &dummy_input_global;
257 }
258
259 /*
260   ES TODO: This method should probably be reworked or junked.
261 */
262 Stream_event *
263 Music::to_event () const
264 {
265   SCM class_name = ly_camel_case_2_lisp_identifier (get_property ("name"));
266
267   // catch programming mistakes.
268   if (!internal_is_music_type (class_name))
269     programming_error ("Not a music type");
270
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 ());
277
278   // articulations as events.
279   SCM art_mus = e->get_property ("articulations");
280   if (scm_is_pair (art_mus))
281     {
282       SCM art_ev = SCM_EOL;
283       for (; scm_is_pair (art_mus); art_mus = scm_cdr (art_mus))
284         {
285           Music *m = unsmob<Music> (scm_car (art_mus));
286           art_ev = scm_cons (m->to_event ()->unprotect (), art_ev);
287         }
288       e->set_property ("articulations", scm_reverse_x (art_ev, SCM_EOL));
289     }
290
291   /*
292     ES TODO: This is a temporary fix. Stream_events should not be
293     aware of music.
294   */
295   e->set_property ("music-cause", self_scm ());
296
297   return e;
298 }
299
300 void
301 Music::send_to_context (Context *c)
302 {
303   Stream_event *ev = to_event ();
304   c->event_source ()->broadcast (ev);
305   ev->unprotect ();
306 }
307
308 Music *
309 make_music_by_name (SCM sym)
310 {
311   SCM rv = Lily::make_music (sym);
312
313   /* UGH. */
314   Music *m = unsmob<Music> (rv);
315   m->protect ();
316   return m;
317 }
318
319 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
320 SCM
321 Music::duration_length_callback (SCM m)
322 {
323   Music *me = unsmob<Music> (m);
324   Duration *d = unsmob<Duration> (me->get_property ("duration"));
325
326   Moment mom;
327   if (d)
328     mom = d->get_length ();
329   return mom.smobbed_copy ();
330 }
331
332 SCM
333 music_deep_copy (SCM m)
334 {
335   if (Music *mus = unsmob<Music> (m))
336       return mus->clone ()->unprotect ();
337   if (scm_is_pair (m))
338     {
339       SCM copy = SCM_EOL;
340       do
341         {
342           copy = scm_cons (music_deep_copy (scm_car (m)), copy);
343           m = scm_cdr (m);
344         }
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));
352       return copy;
353     }
354   return m;
355 }
356
357 void
358 set_origin (SCM m, SCM origin)
359 {
360   while (scm_is_pair (m))
361     {
362       set_origin (scm_car (m), origin);
363       m = scm_cdr (m);
364     }
365   if (Music *mus = unsmob<Music> (m))
366     mus->set_property ("origin", origin);
367 }