]> git.donarmstrong.com Git - lilypond.git/blob - lily/music.cc
Doc-es: update Unfretted.
[lilypond.git] / lily / music.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2009 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 "ly-smobs.icc"
28 #include "main.hh"
29 #include "music-sequence.hh"
30 #include "score.hh"
31 #include "warn.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_c_memq (k, ifs) != SCM_BOOL_F;
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 ly_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
134 Pitch
135 Music::generic_to_relative_octave (Pitch last)
136 {
137   SCM elt = get_property ("element");
138   Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
139   if (old_pit)
140     {
141       Pitch new_pit = *old_pit;
142       new_pit = new_pit.to_relative_octave (last);
143
144       SCM check = get_property ("absolute-octave");
145       if (scm_is_number (check)
146           && new_pit.get_octave () != scm_to_int (check))
147         {
148           Pitch expected_pit (scm_to_int (check),
149                               new_pit.get_notename (),
150                               new_pit.get_alteration ());
151           origin ()->warning (_f ("octave check failed; expected \"%s\", found: \"%s\"",
152                                   expected_pit.to_string (),
153                                   new_pit.to_string ()));
154           new_pit = expected_pit;
155         }
156
157       set_property ("pitch", new_pit.smobbed_copy ());
158
159       last = new_pit;
160     }
161
162   if (Music *m = unsmob_music (elt))
163     last = m->to_relative_octave (last);
164
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 (), 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 = unsmob_music (elt))
188     m->compress (factor);
189
190   compress_music_list (get_property ("elements"), factor);
191   Duration *d = unsmob_duration (get_property ("duration"));
192   if (d)
193     set_property ("duration", d->compressed (factor.main_part_).smobbed_copy ());
194 }
195
196 /*
197   This mutates alist.  Hence, make sure that it is not shared 
198 */
199 void
200 transpose_mutable (SCM alist, Pitch delta)
201 {
202   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
203     {
204       SCM entry = scm_car (s);
205       SCM prop = scm_car (entry);
206       SCM val = scm_cdr (entry);
207       SCM new_val = val;
208       
209       if (Pitch *p = unsmob_pitch (val))
210         {
211           Pitch transposed = p->transposed (delta);
212           if (transposed.get_alteration ().abs () > Rational (1,1))
213             {
214               warning (_f ("transposition by %s makes alteration larger than double",
215                            delta.to_string ()));
216             }
217
218           new_val = transposed.smobbed_copy ();
219         }
220       else if (prop == ly_symbol2scm ("element"))
221         {
222           if (Music *m = unsmob_music (val))
223             m->transpose (delta);
224         }
225       else if (prop == ly_symbol2scm ("elements"))
226         transpose_music_list (val, delta);
227       else if (prop == ly_symbol2scm ("pitch-alist") &&
228                scm_is_pair (val))
229         new_val = ly_transpose_key_alist (val, delta.smobbed_copy ());
230
231       if (val != new_val)
232         scm_set_cdr_x (entry , new_val);
233     }
234 }
235
236 void
237 Music::transpose (Pitch delta)
238 {
239   if (to_boolean (get_property ("untransposable")))
240     return;
241
242   transpose_mutable (mutable_property_alist_, delta);
243 }
244
245 void
246 Music::set_spot (Input ip)
247 {
248   set_property ("origin", make_input (ip));
249 }
250
251 Input *
252 Music::origin () const
253 {
254   Input *ip = unsmob_input (get_property ("origin"));
255   return ip ? ip : &dummy_input_global;
256 }
257
258 /*
259   ES TODO: This method should probably be reworked or junked.
260 */
261 Stream_event *
262 Music::to_event () const
263 {
264   SCM class_name = ly_camel_case_2_lisp_identifier (get_property ("name"));
265
266   // catch programming mistakes.
267   if (!internal_is_music_type (class_name))
268     {
269       programming_error ("Not a music type");
270     }
271
272   Stream_event *e = new Stream_event (class_name, mutable_property_alist_);
273   Moment length = get_length ();
274   if (length.to_bool ())
275     e->set_property ("length", length.smobbed_copy ());
276
277   // articulations as events.
278   SCM art_mus = e->get_property ("articulations");
279   if (scm_is_pair (art_mus))
280     {
281       SCM art_ev = SCM_EOL;
282       for (; scm_is_pair (art_mus); art_mus = scm_cdr (art_mus))
283         {
284           Music *m = unsmob_music (scm_car (art_mus));
285           SCM ev = m ? m->to_event ()->unprotect () : scm_car (art_mus);
286           art_ev = scm_cons (ev, 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 = unsmob_music (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 = unsmob_music (m);
325   Duration *d = unsmob_duration (me->get_property ("duration"));
326
327   Moment mom;
328   if (d)
329     mom = d->get_length ();
330   return mom.smobbed_copy ();
331 }
332
333 Music *
334 unsmob_music (SCM m)
335 {
336   return dynamic_cast<Music*> (unsmob_prob (m));
337 }
338