]> git.donarmstrong.com Git - lilypond.git/blob - lily/music.cc
(parse_symbol_list): Bugfix.
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "music.hh"
10 #include "music-sequence.hh"
11 #include "duration.hh"
12 #include "input-smob.hh"
13 #include "ly-smobs.icc"
14 #include "main.hh"
15 #include "pitch.hh"
16 #include "score.hh"
17 #include "warn.hh"
18
19 /*
20   Music is anything that has duration and supports both time compression
21   and transposition.
22
23   In Lily, everything that can be thought to have a length and a pitch
24   (which has a duration which can be transposed) is considered "music",
25 */
26 bool
27 Music::internal_is_music_type (SCM k) const
28 {
29   SCM ifs = get_property ("types");
30
31   return scm_c_memq (k, ifs) != SCM_BOOL_F;
32 }
33
34 String
35 Music::name () const
36 {
37   SCM nm = get_property ("name");
38   if (scm_is_symbol (nm))
39     {
40       return ly_symbol2string (nm);
41     }
42   else
43     {
44       return classname (this);
45     }
46 }
47
48 Music::Music (SCM init)
49 {
50   self_scm_ = SCM_EOL;
51   immutable_property_alist_ = init;
52   mutable_property_alist_ = SCM_EOL;
53   smobify_self ();
54
55   length_callback_ = get_property ("length-callback");
56   if (!ly_is_procedure (length_callback_))
57     {
58       length_callback_ = duration_length_callback_proc;
59     }
60
61   start_callback_ = get_property ("start-callback");
62 }
63
64 Music::Music (Music const &m)
65 {
66   immutable_property_alist_ = m.immutable_property_alist_;
67   mutable_property_alist_ = SCM_EOL;
68   self_scm_ = SCM_EOL;
69
70   /* First we smobify_self, then we copy over the stuff.  If we don't,
71      stack vars that hold the copy might be optimized away, meaning
72      that they won't be protected from GC. */
73   smobify_self ();
74   mutable_property_alist_ = ly_music_deep_copy (m.mutable_property_alist_);
75   length_callback_ = m.length_callback_;
76   start_callback_ = m.start_callback_;
77   set_spot (*m.origin ());
78 }
79
80 Music::~Music ()
81 {
82 }
83
84 SCM
85 Music::get_property_alist (bool m) const
86 {
87   return (m) ? mutable_property_alist_ : immutable_property_alist_;
88 }
89
90 SCM
91 Music::mark_smob (SCM m)
92 {
93   Music *mus = (Music *) SCM_CELL_WORD_1 (m);
94   scm_gc_mark (mus->immutable_property_alist_);
95   return mus->mutable_property_alist_;
96 }
97
98 Moment
99 Music::get_length () const
100 {
101   SCM lst = get_property ("length");
102   if (unsmob_moment (lst))
103     return *unsmob_moment (lst);
104
105   if (ly_is_procedure (length_callback_))
106     {
107       SCM res = scm_call_1 (length_callback_, self_scm ());
108       return *unsmob_moment (res);
109     }
110
111   return Moment (0);
112 }
113
114 Moment
115 Music::start_mom () const
116 {
117   SCM lst = start_callback_;
118   if (ly_is_procedure (lst))
119     {
120       SCM res = scm_call_1 (lst, self_scm ());
121       return *unsmob_moment (res);
122     }
123
124   Moment m;
125   return m;
126 }
127
128 void
129 print_alist (SCM a, SCM port)
130 {
131   /* SCM_EOL  -> catch malformed lists.  */
132   for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
133     {
134       scm_display (scm_caar (s), port);
135       scm_puts (" = ", port);
136       scm_write (scm_cdar (s), port);
137       scm_puts ("\n", port);
138     }
139 }
140
141 int
142 Music::print_smob (SCM s, SCM p, scm_print_state*)
143 {
144   scm_puts ("#<Music ", p);
145   Music *m = unsmob_music (s);
146
147   SCM nm = m->get_property ("name");
148   if (scm_is_symbol (nm) || scm_is_string (nm))
149     scm_display (nm, p);
150   else
151     scm_puts (classname (m), p);
152
153   /* Printing properties takes a lot of time, especially during backtraces.
154      For inspecting, it is better to explicitly use an inspection
155      function.  */
156
157   scm_puts (">", p);
158   return 1;
159 }
160
161 Pitch
162 Music::generic_to_relative_octave (Pitch last)
163 {
164   SCM elt = get_property ("element");
165   Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
166   if (old_pit)
167     {
168       Pitch new_pit = *old_pit;
169       new_pit = new_pit.to_relative_octave (last);
170
171       SCM check = get_property ("absolute-octave");
172       if (scm_is_number (check)
173           && new_pit.get_octave () != scm_to_int (check))
174         {
175           Pitch expected_pit (scm_to_int (check),
176                               new_pit.get_notename (),
177                               new_pit.get_alteration ());
178           origin ()->warning (_f ("octave check failed; expected %s, found: %s",
179                                   expected_pit.to_string (),
180                                   new_pit.to_string ()));
181           new_pit = expected_pit;
182         }
183
184       set_property ("pitch", new_pit.smobbed_copy ());
185
186       last = new_pit;
187     }
188
189   if (Music *m = unsmob_music (elt))
190     last = m->to_relative_octave (last);
191
192   last = music_list_to_relative (get_property ("elements"), last, false);
193   return last;
194 }
195
196 Pitch
197 Music::to_relative_octave (Pitch last)
198 {
199   SCM callback = get_property ("to-relative-callback");
200   if (ly_is_procedure (callback))
201     {
202       Pitch *p = unsmob_pitch (scm_call_2 (callback, self_scm (), last.smobbed_copy ()));
203       return *p;
204     }
205
206   return generic_to_relative_octave (last);
207 }
208
209 void
210 Music::compress (Moment factor)
211 {
212   SCM elt = get_property ("element");
213
214   if (Music *m = unsmob_music (elt))
215     m->compress (factor);
216
217   compress_music_list (get_property ("elements"), factor);
218   Duration *d = unsmob_duration (get_property ("duration"));
219   if (d)
220     set_property ("duration", d->compressed (factor.main_part_).smobbed_copy ());
221 }
222
223 void
224 Music::transpose (Pitch delta)
225 {
226   if (to_boolean (get_property ("untransposable")))
227     return;
228
229   for (SCM s = this->get_property_alist (true); scm_is_pair (s); s = scm_cdr (s))
230     {
231       SCM entry = scm_car (s);
232       SCM val = scm_cdr (entry);
233
234       if (Pitch *p = unsmob_pitch (val))
235         {
236           Pitch transposed = p->transposed (delta);
237           scm_set_cdr_x (entry, transposed.smobbed_copy ());
238
239           if (abs (transposed.get_alteration ()) > DOUBLE_SHARP)
240             {
241               warning (_f ("transposition by %s makes alteration larger than double",
242                            delta.to_string ()));
243             }
244         }
245     }
246
247   SCM elt = get_property ("element");
248
249   if (Music *m = unsmob_music (elt))
250     m->transpose (delta);
251
252   transpose_music_list (get_property ("elements"), delta);
253
254   /*
255     UGH - how do this more generically?
256   */
257   SCM pa = get_property ("pitch-alist");
258   if (scm_is_pair (pa))
259     {
260       set_property ("pitch-alist", ly_transpose_key_alist (pa, delta.smobbed_copy ()));
261     }
262 }
263
264 IMPLEMENT_TYPE_P (Music, "ly:music?");
265 IMPLEMENT_SMOBS (Music);
266 IMPLEMENT_DEFAULT_EQUAL_P (Music);
267
268 SCM
269 Music::internal_get_property (SCM sym) const
270 {
271   SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
272   if (s != SCM_BOOL_F)
273     return scm_cdr (s);
274
275   s = scm_sloppy_assq (sym, immutable_property_alist_);
276   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
277 }
278
279 SCM
280 Music::internal_get_object (SCM s) const
281 {
282   return internal_get_property (s);
283 }
284
285 void
286 Music::internal_set_object (SCM s, SCM v)
287 {
288   return internal_set_property (s, v);
289 }
290
291 void
292 Music::internal_set_property (SCM s, SCM v)
293 {
294   if (do_internal_type_checking_global)
295     if (!type_check_assignment (s, v, ly_symbol2scm ("music-type?")))
296       abort ();
297
298   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
299 }
300
301 void
302 Music::set_spot (Input ip)
303 {
304   set_property ("origin", make_input (ip));
305 }
306
307 Input *
308 Music::origin () const
309 {
310   Input *ip = unsmob_input (get_property ("origin"));
311   return ip ? ip : &dummy_input_global;
312 }
313
314 Music *
315 make_music_by_name (SCM sym)
316 {
317   SCM make_music_proc = ly_lily_module_constant ("make-music");
318   SCM rv = scm_call_1 (make_music_proc, sym);
319
320   /* UGH. */
321   Music *m = unsmob_music (rv);
322   m->protect ();
323   return m;
324 }
325
326 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
327 SCM
328 Music::duration_length_callback (SCM m)
329 {
330   Music *me = unsmob_music (m);
331   Duration *d = unsmob_duration (me->get_property ("duration"));
332
333   Moment mom;
334   if (d)
335     {
336       mom = d->get_length ();
337     }
338   return mom.smobbed_copy ();
339 }