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