]> git.donarmstrong.com Git - lilypond.git/blob - lily/music.cc
77c8f34335629a5c75c4d2e7e9614976e95e259e
[lilypond.git] / lily / music.cc
1 /*
2   music.cc -- implement Music
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "music.hh"
10
11 #include "context.hh"
12 #include "dispatcher.hh"
13 #include "duration.hh"
14 #include "input.hh"
15 #include "international.hh"
16 #include "ly-smobs.icc"
17 #include "main.hh"
18 #include "music-sequence.hh"
19 #include "score.hh"
20 #include "warn.hh"
21
22 /*
23   Music is anything that has (possibly zero) duration and supports
24   both time compression and transposition.
25
26   In Lily, everything that can be thought to have a length and a pitch
27   (which has a duration which can be transposed) is considered "music".
28 */
29 bool
30 Music::internal_is_music_type (SCM k) const
31 {
32   SCM ifs = get_property ("types");
33
34   return scm_c_memq (k, ifs) != SCM_BOOL_F;
35 }
36
37 Music::Music (SCM init)
38   : Prob (ly_symbol2scm ("Music"), init)
39 {
40   length_callback_ = SCM_EOL;
41   start_callback_ = SCM_EOL;
42   
43   length_callback_ = get_property ("length-callback");
44   if (!ly_is_procedure (length_callback_))
45     length_callback_ = duration_length_callback_proc;
46
47   start_callback_ = get_property ("start-callback");
48 }
49
50 void
51 Music::derived_mark () const
52 {
53   scm_gc_mark (length_callback_);
54   scm_gc_mark (start_callback_);
55 }
56
57 SCM
58 Music::copy_mutable_properties () const
59 {
60   return ly_music_deep_copy (mutable_property_alist_);
61 }
62
63 void
64 Music::type_check_assignment (SCM s, SCM v) const
65 {
66   ::type_check_assignment (s, v, ly_symbol2scm ("music-type?"));
67 }
68
69 Music::Music (Music const &m)
70   : Prob (m)
71 {
72   length_callback_ = m.length_callback_;
73   start_callback_ = m.start_callback_;
74
75   /// why? 
76   set_spot (*m.origin ());
77 }
78
79 Moment
80 Music::get_length () const
81 {
82   SCM lst = get_property ("length");
83   if (unsmob_moment (lst))
84     return *unsmob_moment (lst);
85
86   if (ly_is_procedure (length_callback_))
87     {
88       SCM res = scm_call_1 (length_callback_, self_scm ());
89       return *unsmob_moment (res);
90     }
91
92   return Moment (0);
93 }
94
95 Moment
96 Music::start_mom () const
97 {
98   SCM lst = start_callback_;
99   if (ly_is_procedure (lst))
100     {
101       SCM res = scm_call_1 (lst, self_scm ());
102       return *unsmob_moment (res);
103     }
104
105   Moment m;
106   return m;
107 }
108
109 void
110 print_alist (SCM a, SCM port)
111 {
112   /* SCM_EOL  -> catch malformed lists.  */
113   for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
114     {
115       scm_display (scm_caar (s), port);
116       scm_puts (" = ", port);
117       scm_write (scm_cdar (s), port);
118       scm_puts ("\n", port);
119     }
120 }
121
122
123 Pitch
124 Music::generic_to_relative_octave (Pitch last)
125 {
126   SCM elt = get_property ("element");
127   Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
128   if (old_pit)
129     {
130       Pitch new_pit = *old_pit;
131       new_pit = new_pit.to_relative_octave (last);
132
133       SCM check = get_property ("absolute-octave");
134       if (scm_is_number (check)
135           && new_pit.get_octave () != scm_to_int (check))
136         {
137           Pitch expected_pit (scm_to_int (check),
138                               new_pit.get_notename (),
139                               new_pit.get_alteration ());
140           origin ()->warning (_f ("octave check failed; expected \"%s\", found: \"%s\"",
141                                   expected_pit.to_string (),
142                                   new_pit.to_string ()));
143           new_pit = expected_pit;
144         }
145
146       set_property ("pitch", new_pit.smobbed_copy ());
147
148       last = new_pit;
149     }
150
151   if (Music *m = unsmob_music (elt))
152     last = m->to_relative_octave (last);
153
154   last = music_list_to_relative (get_property ("elements"), last, false);
155   return last;
156 }
157
158 Pitch
159 Music::to_relative_octave (Pitch last)
160 {
161   SCM callback = get_property ("to-relative-callback");
162   if (ly_is_procedure (callback))
163     {
164       Pitch *p = unsmob_pitch (scm_call_2 (callback, self_scm (), last.smobbed_copy ()));
165       return *p;
166     }
167
168   return generic_to_relative_octave (last);
169 }
170
171 void
172 Music::compress (Moment factor)
173 {
174   SCM elt = get_property ("element");
175
176   if (Music *m = unsmob_music (elt))
177     m->compress (factor);
178
179   compress_music_list (get_property ("elements"), factor);
180   Duration *d = unsmob_duration (get_property ("duration"));
181   if (d)
182     set_property ("duration", d->compressed (factor.main_part_).smobbed_copy ());
183 }
184
185 /*
186   This mutates alist.  Hence, make sure that it is not shared 
187 */
188 void
189 transpose_mutable (SCM alist, Pitch delta)
190 {
191   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
192     {
193       SCM entry = scm_car (s);
194       SCM prop = scm_car (entry);
195       SCM val = scm_cdr (entry);
196       SCM new_val = val;
197       
198       if (Pitch *p = unsmob_pitch (val))
199         {
200           Pitch transposed = p->transposed (delta);
201           if (transposed.get_alteration ().abs () > Rational (1,1))
202             {
203               warning (_f ("transposition by %s makes alteration larger than double",
204                            delta.to_string ()));
205             }
206
207           new_val = transposed.smobbed_copy ();
208         }
209       else if (prop == ly_symbol2scm ("element"))
210         {
211           if (Music *m = unsmob_music (val))
212             m->transpose (delta);
213         }
214       else if (prop == ly_symbol2scm ("elements"))
215         transpose_music_list (val, delta);
216       else if (prop == ly_symbol2scm ("pitch-alist") &&
217                scm_is_pair (val))
218         new_val = ly_transpose_key_alist (val, delta.smobbed_copy ());
219
220       if (val != new_val)
221         scm_set_cdr_x (entry , new_val);
222     }
223 }
224
225 void
226 Music::transpose (Pitch delta)
227 {
228   if (to_boolean (get_property ("untransposable")))
229     return;
230
231   transpose_mutable (mutable_property_alist_, delta);
232 }
233
234 void
235 Music::set_spot (Input ip)
236 {
237   set_property ("origin", make_input (ip));
238 }
239
240 Input *
241 Music::origin () const
242 {
243   Input *ip = unsmob_input (get_property ("origin"));
244   return ip ? ip : &dummy_input_global;
245 }
246
247 /*
248   ES TODO: This method should probably be reworked or junked.
249 */
250 Stream_event *
251 Music::to_event () const
252 {
253   SCM class_name = ly_camel_case_2_lisp_identifier (get_property ("name"));
254
255   // catch programming mistakes.
256   if (!internal_is_music_type (class_name))
257     {
258       programming_error ("Not a music type");
259     }
260
261   Stream_event *e = new Stream_event (class_name, mutable_property_alist_);
262   Moment length = get_length ();
263   if (length.to_bool ())
264     e->set_property ("length", length.smobbed_copy ());
265
266   // articulations as events.
267   SCM art_mus = e->get_property ("articulations");
268   if (scm_is_pair (art_mus))
269     {
270       SCM art_ev = SCM_EOL;
271       for (; scm_is_pair (art_mus); art_mus = scm_cdr (art_mus))
272         {
273           Music *m = unsmob_music (scm_car (art_mus));
274           SCM ev = m ? m->to_event ()->unprotect () : scm_car (art_mus);
275           art_ev = scm_cons (ev, art_ev);
276         }
277       e->set_property ("articulations", scm_reverse_x (art_ev, SCM_EOL));
278     }
279
280   /*
281     ES TODO: This is a temporary fix. Stream_events should not be
282     aware of music.
283   */
284   e->set_property ("music-cause", self_scm ());
285
286   return e;
287 }
288
289 void
290 Music::send_to_context (Context *c)
291 {
292   Stream_event *ev = to_event ();
293   c->event_source ()->broadcast (ev);
294   ev->unprotect ();
295 }
296
297 Music *
298 make_music_by_name (SCM sym)
299 {
300   SCM make_music_proc = ly_lily_module_constant ("make-music");
301   SCM rv = scm_call_1 (make_music_proc, sym);
302
303   /* UGH. */
304   Music *m = unsmob_music (rv);
305   m->protect ();
306   return m;
307 }
308
309 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
310 SCM
311 Music::duration_length_callback (SCM m)
312 {
313   Music *me = unsmob_music (m);
314   Duration *d = unsmob_duration (me->get_property ("duration"));
315
316   Moment mom;
317   if (d)
318     mom = d->get_length ();
319   return mom.smobbed_copy ();
320 }
321
322 Music *
323 unsmob_music (SCM m)
324 {
325   return dynamic_cast<Music*> (unsmob_prob (m));
326 }
327