]> git.donarmstrong.com Git - lilypond.git/blob - lily/music.cc
Merge branch 'master' of /home/jcharles/GIT/Lily/. into translation
[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 Music::Music (SCM init)
49   : Prob (ly_symbol2scm ("Music"), init)
50 {
51   length_callback_ = SCM_EOL;
52   start_callback_ = SCM_EOL;
53
54   length_callback_ = get_property ("length-callback");
55   if (!ly_is_procedure (length_callback_))
56     length_callback_ = duration_length_callback_proc;
57
58   start_callback_ = get_property ("start-callback");
59 }
60
61 void
62 Music::derived_mark () const
63 {
64   scm_gc_mark (length_callback_);
65   scm_gc_mark (start_callback_);
66 }
67
68 SCM
69 Music::copy_mutable_properties () const
70 {
71   return music_deep_copy (mutable_property_alist_);
72 }
73
74 void
75 Music::type_check_assignment (SCM s, SCM v) const
76 {
77   ::type_check_assignment (s, v, ly_symbol2scm ("music-type?"));
78 }
79
80 Music::Music (Music const &m)
81   : Prob (m)
82 {
83   length_callback_ = m.length_callback_;
84   start_callback_ = m.start_callback_;
85
86   /// why?
87   set_spot (*m.origin ());
88 }
89
90 Moment
91 Music::get_length () const
92 {
93   SCM lst = get_property ("length");
94   if (unsmob<Moment> (lst))
95     return *unsmob<Moment> (lst);
96
97   if (ly_is_procedure (length_callback_))
98     {
99       SCM res = scm_call_1 (length_callback_, self_scm ());
100       return *unsmob<Moment> (res);
101     }
102
103   return Moment (0);
104 }
105
106 Moment
107 Music::start_mom () const
108 {
109   SCM lst = start_callback_;
110   if (ly_is_procedure (lst))
111     {
112       SCM res = scm_call_1 (lst, self_scm ());
113       return *unsmob<Moment> (res);
114     }
115
116   Moment m;
117   return m;
118 }
119
120 void
121 print_alist (SCM a, SCM port)
122 {
123   /* SCM_EOL  -> catch malformed lists.  */
124   for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
125     {
126       scm_display (scm_caar (s), port);
127       scm_puts (" = ", port);
128       scm_write (scm_cdar (s), port);
129       scm_puts ("\n", port);
130     }
131 }
132
133 Pitch
134 Music::generic_to_relative_octave (Pitch last)
135 {
136   SCM elt = get_property ("element");
137   Pitch *old_pit = unsmob<Pitch> (get_property ("pitch"));
138   if (old_pit)
139     {
140       Pitch new_pit = *old_pit;
141       new_pit = new_pit.to_relative_octave (last);
142
143       SCM check = get_property ("absolute-octave");
144       if (scm_is_number (check)
145           && new_pit.get_octave () != scm_to_int (check))
146         {
147           Pitch expected_pit (scm_to_int (check),
148                               new_pit.get_notename (),
149                               new_pit.get_alteration ());
150           origin ()->warning (_f ("octave check failed; expected \"%s\", found: \"%s\"",
151                                   expected_pit.to_string (),
152                                   new_pit.to_string ()));
153           new_pit = expected_pit;
154         }
155
156       set_property ("pitch", new_pit.smobbed_copy ());
157
158       last = new_pit;
159     }
160
161   if (Music *m = unsmob<Music> (elt))
162     last = m->to_relative_octave (last);
163
164   (void) music_list_to_relative (get_property ("articulations"), last, true);
165   last = music_list_to_relative (get_property ("elements"), last, false);
166   return last;
167 }
168
169 Pitch
170 Music::to_relative_octave (Pitch last)
171 {
172   SCM callback = get_property ("to-relative-callback");
173   if (ly_is_procedure (callback))
174     {
175       Pitch *p = unsmob<Pitch> (scm_call_2 (callback, self_scm (),
176                                            last.smobbed_copy ()));
177       return *p;
178     }
179
180   return generic_to_relative_octave (last);
181 }
182
183 void
184 Music::compress (Moment factor)
185 {
186   SCM elt = get_property ("element");
187
188   if (Music *m = unsmob<Music> (elt))
189     m->compress (factor);
190
191   compress_music_list (get_property ("elements"), factor);
192   Duration *d = unsmob<Duration> (get_property ("duration"));
193   if (d)
194     set_property ("duration",
195                   d->compressed (factor.main_part_).smobbed_copy ());
196 }
197
198 /*
199   This mutates alist.  Hence, make sure that it is not shared
200 */
201
202 void
203 Prob::transpose (Pitch delta)
204 {
205   if (to_boolean (get_property ("untransposable")))
206     return;
207
208   for (SCM s = mutable_property_alist_; scm_is_pair (s); s = scm_cdr (s))
209     {
210       SCM entry = scm_car (s);
211       SCM prop = scm_car (entry);
212       SCM val = scm_cdr (entry);
213       SCM new_val = val;
214
215       if (Pitch *p = unsmob<Pitch> (val))
216         {
217           Pitch transposed = p->transposed (delta);
218
219           if (scm_is_eq (prop, ly_symbol2scm ("tonic")))
220             transposed = Pitch (-1, transposed.get_notename (),
221                                 transposed.get_alteration ());
222
223           new_val = transposed.smobbed_copy ();
224         }
225       else if (scm_is_eq (prop, ly_symbol2scm ("element")))
226         {
227           if (Prob *m = unsmob<Prob> (val))
228             m->transpose (delta);
229         }
230       else if (scm_is_eq (prop, ly_symbol2scm ("elements"))
231                || scm_is_eq (prop, ly_symbol2scm ("articulations")))
232         transpose_music_list (val, delta);
233       else if (scm_is_eq (prop, ly_symbol2scm ("pitch-alist"))
234                && scm_is_pair (val))
235         new_val = ly_transpose_key_alist (val, delta.smobbed_copy ());
236
237       if (val != new_val)
238         scm_set_cdr_x (entry, new_val);
239     }
240 }
241
242 void
243 Music::set_spot (Input ip)
244 {
245   set_property ("origin", ip.smobbed_copy ());
246 }
247
248 Input *
249 Music::origin () const
250 {
251   Input *ip = unsmob<Input> (get_property ("origin"));
252   return ip ? ip : &dummy_input_global;
253 }
254
255 /*
256   ES TODO: This method should probably be reworked or junked.
257 */
258 Stream_event *
259 Music::to_event () const
260 {
261   SCM class_name = ly_camel_case_2_lisp_identifier (get_property ("name"));
262
263   // catch programming mistakes.
264   if (!internal_is_music_type (class_name))
265     programming_error ("Not a music type");
266
267   Stream_event *e = new Stream_event
268     (Lily::ly_make_event_class (class_name),
269      mutable_property_alist_);
270   Moment length = get_length ();
271   if (length.to_bool ())
272     e->set_property ("length", length.smobbed_copy ());
273
274   // articulations as events.
275   SCM art_mus = e->get_property ("articulations");
276   if (scm_is_pair (art_mus))
277     {
278       SCM art_ev = SCM_EOL;
279       for (; scm_is_pair (art_mus); art_mus = scm_cdr (art_mus))
280         {
281           Music *m = unsmob<Music> (scm_car (art_mus));
282           art_ev = scm_cons (m->to_event ()->unprotect (), art_ev);
283         }
284       e->set_property ("articulations", scm_reverse_x (art_ev, SCM_EOL));
285     }
286
287   /*
288     ES TODO: This is a temporary fix. Stream_events should not be
289     aware of music.
290   */
291   e->set_property ("music-cause", self_scm ());
292
293   return e;
294 }
295
296 void
297 Music::send_to_context (Context *c)
298 {
299   Stream_event *ev = to_event ();
300   c->event_source ()->broadcast (ev);
301   ev->unprotect ();
302 }
303
304 Music *
305 make_music_by_name (SCM sym)
306 {
307   SCM rv = Lily::make_music (sym);
308
309   /* UGH. */
310   Music *m = unsmob<Music> (rv);
311   m->protect ();
312   return m;
313 }
314
315 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
316 SCM
317 Music::duration_length_callback (SCM m)
318 {
319   Music *me = unsmob<Music> (m);
320   Duration *d = unsmob<Duration> (me->get_property ("duration"));
321
322   Moment mom;
323   if (d)
324     mom = d->get_length ();
325   return mom.smobbed_copy ();
326 }
327
328 SCM
329 music_deep_copy (SCM m)
330 {
331   if (Music *mus = unsmob<Music> (m))
332       return mus->clone ()->unprotect ();
333   if (scm_is_pair (m))
334     {
335       SCM copy = SCM_EOL;
336       do
337         {
338           copy = scm_cons (music_deep_copy (scm_car (m)), copy);
339           m = scm_cdr (m);
340         }
341       while (scm_is_pair (m));
342       // Oh, come on, GUILE.  Why do you require the second argument
343       // of scm_reverse_x to be a proper list?  That makes no sense.
344       // return scm_reverse_x (copy, music_deep_copy (m));
345       SCM last_cons = copy;
346       copy = scm_reverse_x (copy, SCM_EOL);
347       scm_set_cdr_x (last_cons, music_deep_copy (m));
348       return copy;
349     }
350   return m;
351 }
352
353 void
354 set_origin (SCM m, SCM origin)
355 {
356   while (scm_is_pair (m))
357     {
358       set_origin (scm_car (m), origin);
359       m = scm_cdr (m);
360     }
361   if (Music *mus = unsmob<Music> (m))
362     mus->set_property ("origin", origin);
363 }