]> git.donarmstrong.com Git - lilypond.git/blob - lily/music.cc
46376ee5ed48c3ce97df4c694cb691d3f7512c15
[lilypond.git] / lily / music.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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
32 /*
33   Music is anything that has (possibly zero) duration and supports
34   both time compression and transposition.
35
36   In Lily, everything that can be thought to have a length and a pitch
37   (which has a duration which can be transposed) is considered "music".
38 */
39 bool
40 Music::internal_is_music_type (SCM k) const
41 {
42   SCM ifs = get_property ("types");
43
44   return scm_c_memq (k, ifs) != SCM_BOOL_F;
45 }
46
47 Music::Music (SCM init)
48   : Prob (ly_symbol2scm ("Music"), init)
49 {
50   length_callback_ = SCM_EOL;
51   start_callback_ = SCM_EOL;
52
53   length_callback_ = get_property ("length-callback");
54   if (!ly_is_procedure (length_callback_))
55     length_callback_ = duration_length_callback_proc;
56
57   start_callback_ = get_property ("start-callback");
58 }
59
60 void
61 Music::derived_mark () const
62 {
63   scm_gc_mark (length_callback_);
64   scm_gc_mark (start_callback_);
65 }
66
67 SCM
68 Music::copy_mutable_properties () const
69 {
70   return ly_music_deep_copy (mutable_property_alist_);
71 }
72
73 void
74 Music::type_check_assignment (SCM s, SCM v) const
75 {
76   ::type_check_assignment (s, v, ly_symbol2scm ("music-type?"));
77 }
78
79 Music::Music (Music const &m)
80   : Prob (m)
81 {
82   length_callback_ = m.length_callback_;
83   start_callback_ = m.start_callback_;
84
85   /// why?
86   set_spot (*m.origin ());
87 }
88
89 Moment
90 Music::get_length () const
91 {
92   SCM lst = get_property ("length");
93   if (Moment::is_smob (lst))
94     return *Moment::unsmob (lst);
95
96   if (ly_is_procedure (length_callback_))
97     {
98       SCM res = scm_call_1 (length_callback_, self_scm ());
99       return *Moment::unsmob (res);
100     }
101
102   return Moment (0);
103 }
104
105 Moment
106 Music::start_mom () const
107 {
108   SCM lst = start_callback_;
109   if (ly_is_procedure (lst))
110     {
111       SCM res = scm_call_1 (lst, self_scm ());
112       return *Moment::unsmob (res);
113     }
114
115   Moment m;
116   return m;
117 }
118
119 void
120 print_alist (SCM a, SCM port)
121 {
122   /* SCM_EOL  -> catch malformed lists.  */
123   for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
124     {
125       scm_display (scm_caar (s), port);
126       scm_puts (" = ", port);
127       scm_write (scm_cdar (s), port);
128       scm_puts ("\n", port);
129     }
130 }
131
132 Pitch
133 Music::generic_to_relative_octave (Pitch last)
134 {
135   SCM elt = get_property ("element");
136   Pitch *old_pit = Pitch::unsmob (get_property ("pitch"));
137   if (old_pit)
138     {
139       Pitch new_pit = *old_pit;
140       new_pit = new_pit.to_relative_octave (last);
141
142       SCM check = get_property ("absolute-octave");
143       if (scm_is_number (check)
144           && new_pit.get_octave () != scm_to_int (check))
145         {
146           Pitch expected_pit (scm_to_int (check),
147                               new_pit.get_notename (),
148                               new_pit.get_alteration ());
149           origin ()->warning (_f ("octave check failed; expected \"%s\", found: \"%s\"",
150                                   expected_pit.to_string (),
151                                   new_pit.to_string ()));
152           new_pit = expected_pit;
153         }
154
155       set_property ("pitch", new_pit.smobbed_copy ());
156
157       last = new_pit;
158     }
159
160   if (Music *m = Music::unsmob (elt))
161     last = m->to_relative_octave (last);
162
163   (void) music_list_to_relative (get_property ("articulations"), last, true);
164   last = music_list_to_relative (get_property ("elements"), last, false);
165   return last;
166 }
167
168 Pitch
169 Music::to_relative_octave (Pitch last)
170 {
171   SCM callback = get_property ("to-relative-callback");
172   if (ly_is_procedure (callback))
173     {
174       Pitch *p = Pitch::unsmob (scm_call_2 (callback, self_scm (),
175                                            last.smobbed_copy ()));
176       return *p;
177     }
178
179   return generic_to_relative_octave (last);
180 }
181
182 void
183 Music::compress (Moment factor)
184 {
185   SCM elt = get_property ("element");
186
187   if (Music *m = Music::unsmob (elt))
188     m->compress (factor);
189
190   compress_music_list (get_property ("elements"), factor);
191   Duration *d = Duration::unsmob (get_property ("duration"));
192   if (d)
193     set_property ("duration",
194                   d->compressed (factor.main_part_).smobbed_copy ());
195 }
196
197 /*
198   This mutates alist.  Hence, make sure that it is not shared
199 */
200 void
201 transpose_mutable (SCM alist, Pitch delta)
202 {
203   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
204     {
205       SCM entry = scm_car (s);
206       SCM prop = scm_car (entry);
207       SCM val = scm_cdr (entry);
208       SCM new_val = val;
209
210       if (Pitch *p = Pitch::unsmob (val))
211         {
212           Pitch transposed = p->transposed (delta);
213
214           if (prop == ly_symbol2scm ("tonic"))
215             transposed = Pitch (-1, transposed.get_notename (),
216                                 transposed.get_alteration ());
217
218           new_val = transposed.smobbed_copy ();
219         }
220       else if (prop == ly_symbol2scm ("element"))
221         {
222           if (Music *m = Music::unsmob (val))
223             m->transpose (delta);
224         }
225       else if (prop == ly_symbol2scm ("elements")
226                || prop == ly_symbol2scm ("articulations"))
227         transpose_music_list (val, delta);
228       else if (prop == ly_symbol2scm ("pitch-alist")
229                && scm_is_pair (val))
230         new_val = ly_transpose_key_alist (val, delta.smobbed_copy ());
231
232       if (val != new_val)
233         scm_set_cdr_x (entry, new_val);
234     }
235 }
236
237 void
238 Music::transpose (Pitch delta)
239 {
240   if (to_boolean (get_property ("untransposable")))
241     return;
242
243   transpose_mutable (mutable_property_alist_, delta);
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 = Input::unsmob (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     (scm_call_1 (ly_lily_module_constant ("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 = Music::unsmob (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 make_music_proc = ly_lily_module_constant ("make-music");
312   SCM rv = scm_call_1 (make_music_proc, sym);
313
314   /* UGH. */
315   Music *m = Music::unsmob (rv);
316   m->protect ();
317   return m;
318 }
319
320 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
321 SCM
322 Music::duration_length_callback (SCM m)
323 {
324   Music *me = Music::unsmob (m);
325   Duration *d = Duration::unsmob (me->get_property ("duration"));
326
327   Moment mom;
328   if (d)
329     mom = d->get_length ();
330   return mom.smobbed_copy ();
331 }