]> git.donarmstrong.com Git - lilypond.git/blob - lily/music.cc
(DECLARE_BASE_SMOBS): add methods
[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
85 SCM
86 Music::get_property_alist (bool m) const
87 {
88   return (m) ? mutable_property_alist_ : immutable_property_alist_;
89 }
90
91 SCM
92 Music::mark_smob (SCM m)
93 {
94   Music *mus = (Music *) SCM_CELL_WORD_1 (m);
95   scm_gc_mark (mus->immutable_property_alist_);
96   return mus->mutable_property_alist_;
97 }
98
99 Moment
100 Music::get_length () const
101 {
102   SCM lst = get_property ("length");
103   if (unsmob_moment (lst))
104     return *unsmob_moment (lst);
105
106   if (ly_is_procedure (length_callback_))
107     {
108       SCM res = scm_call_1 (length_callback_, self_scm ());
109       return *unsmob_moment (res);
110     }
111
112   return Moment (0);
113 }
114
115 Moment
116 Music::start_mom () const
117 {
118   SCM lst = start_callback_;
119   if (ly_is_procedure (lst))
120     {
121       SCM res = scm_call_1 (lst, self_scm ());
122       return *unsmob_moment (res);
123     }
124
125   Moment m;
126   return m;
127 }
128
129 void
130 print_alist (SCM a, SCM port)
131 {
132   /* SCM_EOL  -> catch malformed lists.  */
133   for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
134     {
135       scm_display (scm_caar (s), port);
136       scm_puts (" = ", port);
137       scm_write (scm_cdar (s), port);
138       scm_puts ("\n", port);
139     }
140 }
141
142 int
143 Music::print_smob (SCM s, SCM p, scm_print_state*)
144 {
145   scm_puts ("#<Music ", p);
146   Music *m = unsmob_music (s);
147
148   SCM nm = m->get_property ("name");
149   if (scm_is_symbol (nm) || scm_is_string (nm))
150     scm_display (nm, p);
151   else
152     scm_puts (classname (m), p);
153
154   /* Printing properties takes a lot of time, especially during backtraces.
155      For inspecting, it is better to explicitly use an inspection
156      function.  */
157
158   scm_puts (">", p);
159   return 1;
160 }
161
162 Pitch
163 Music::generic_to_relative_octave (Pitch last)
164 {
165   SCM elt = get_property ("element");
166   Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
167   if (old_pit)
168     {
169       Pitch new_pit = *old_pit;
170       new_pit = new_pit.to_relative_octave (last);
171
172       SCM check = get_property ("absolute-octave");
173       if (scm_is_number (check)
174           && new_pit.get_octave () != scm_to_int (check))
175         {
176           Pitch expected_pit (scm_to_int (check),
177                               new_pit.get_notename (),
178                               new_pit.get_alteration ());
179           origin ()->warning (_f ("octave check failed; expected %s, found: %s",
180                                   expected_pit.to_string (),
181                                   new_pit.to_string ()));
182           new_pit = expected_pit;
183         }
184
185       set_property ("pitch", new_pit.smobbed_copy ());
186
187       last = new_pit;
188     }
189
190   if (Music *m = unsmob_music (elt))
191     last = m->to_relative_octave (last);
192
193   last = music_list_to_relative (get_property ("elements"), last, false);
194   return last;
195 }
196
197 Pitch
198 Music::to_relative_octave (Pitch last)
199 {
200   SCM callback = get_property ("to-relative-callback");
201   if (ly_is_procedure (callback))
202     {
203       Pitch *p = unsmob_pitch (scm_call_2 (callback, self_scm (), last.smobbed_copy ()));
204       return *p;
205     }
206
207   return generic_to_relative_octave (last);
208 }
209
210 void
211 Music::compress (Moment factor)
212 {
213   SCM elt = get_property ("element");
214
215   if (Music *m = unsmob_music (elt))
216     m->compress (factor);
217
218   compress_music_list (get_property ("elements"), factor);
219   Duration *d = unsmob_duration (get_property ("duration"));
220   if (d)
221     set_property ("duration", d->compressed (factor.main_part_).smobbed_copy ());
222 }
223
224 void
225 Music::transpose (Pitch delta)
226 {
227   if (to_boolean (get_property ("untransposable")))
228     return;
229
230   for (SCM s = this->get_property_alist (true); scm_is_pair (s); s = scm_cdr (s))
231     {
232       SCM entry = scm_car (s);
233       SCM val = scm_cdr (entry);
234
235       if (Pitch *p = unsmob_pitch (val))
236         {
237           Pitch transposed = p->transposed (delta);
238           scm_set_cdr_x (entry, transposed.smobbed_copy ());
239
240           if (abs (transposed.get_alteration ()) > DOUBLE_SHARP)
241             {
242               warning (_f ("transposition by %s makes alteration larger than double",
243                            delta.to_string ()));
244             }
245         }
246     }
247
248   SCM elt = get_property ("element");
249
250   if (Music *m = unsmob_music (elt))
251     m->transpose (delta);
252
253   transpose_music_list (get_property ("elements"), delta);
254
255   /*
256     UGH - how do this more generically?
257   */
258   SCM pa = get_property ("pitch-alist");
259   if (scm_is_pair (pa))
260     {
261       set_property ("pitch-alist", ly_transpose_key_alist (pa, delta.smobbed_copy ()));
262     }
263 }
264
265 IMPLEMENT_TYPE_P (Music, "ly:music?");
266 IMPLEMENT_SMOBS (Music);
267 IMPLEMENT_DEFAULT_EQUAL_P (Music);
268
269 SCM
270 Music::internal_get_property (SCM sym) const
271 {
272   SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
273   if (s != SCM_BOOL_F)
274     return scm_cdr (s);
275
276   s = scm_sloppy_assq (sym, immutable_property_alist_);
277   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
278 }
279
280
281 SCM
282 Music::internal_get_object (SCM s) const
283 {
284   return internal_get_property (s);
285 }
286
287 void
288 Music::internal_set_object (SCM s, SCM v)
289 {
290   return internal_set_property (s,v);
291 }
292
293 void
294 Music::internal_set_property (SCM s, SCM v)
295 {
296   if (do_internal_type_checking_global)
297     if (!type_check_assignment (s, v, ly_symbol2scm ("music-type?")))
298       abort ();
299
300   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
301 }
302
303 void
304 Music::set_spot (Input ip)
305 {
306   set_property ("origin", make_input (ip));
307 }
308
309 Input *
310 Music::origin () const
311 {
312   Input *ip = unsmob_input (get_property ("origin"));
313   return ip ? ip : &dummy_input_global;
314 }
315
316 Music *
317 make_music_by_name (SCM sym)
318 {
319   SCM make_music_proc = ly_lily_module_constant ("make-music");
320   SCM rv = scm_call_1 (make_music_proc, sym);
321
322   /* UGH. */
323   Music *m = unsmob_music (rv);
324   m->protect ();
325   return m;
326 }
327
328
329 MAKE_SCHEME_CALLBACK (Music, duration_length_callback, 1);
330 SCM
331 Music::duration_length_callback (SCM m)
332 {
333   Music *me = unsmob_music (m);
334   Duration *d = unsmob_duration (me->get_property ("duration"));
335
336   Moment mom;
337   if (d)
338     {
339       mom = d->get_length ();
340     }
341   return mom.smobbed_copy ();
342 }