]> git.donarmstrong.com Git - lilypond.git/blob - lily/music.cc
* lily/include/transposed-music.hh (class Transposed_music): remove.
[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--2004 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::to_relative_octave (Pitch last)
161 {
162   SCM callback = get_property ("to-relative-callback");
163   if (ly_c_procedure_p (callback))
164     {
165       Pitch * p = unsmob_pitch (scm_call_2 (callback, self_scm(), last.smobbed_copy ()));
166       return *p;
167     }
168
169   SCM elt = get_property ("element");
170   Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
171   if (old_pit)
172     {
173       Pitch new_pit = *old_pit;
174       new_pit = new_pit.to_relative_octave (last);
175
176       SCM check = get_property ("absolute-octave");
177       if (scm_is_number (check) &&
178           new_pit.get_octave () != scm_to_int (check))
179         {
180           Pitch expected_pit (scm_to_int (check),
181                               new_pit.get_notename (),
182                               new_pit.get_alteration ());
183           origin ()->warning (_f ("octave check failed; expected %s, found: %s",
184                                   expected_pit.to_string (),
185                                   new_pit.to_string ()));
186           new_pit = expected_pit;
187         }
188       
189       set_property ("pitch", new_pit.smobbed_copy ());
190   
191       last = new_pit;
192     }
193
194   
195   if (Music *m = unsmob_music (elt))
196     last = m->to_relative_octave (last);
197
198   last = music_list_to_relative (get_property ("elements"), last, false);
199   return last;
200 }
201
202 void
203 Music::compress (Moment factor)
204 {
205   SCM elt = get_property ("element");
206
207   if (Music *m = unsmob_music (elt))
208     m->compress (factor);
209
210   compress_music_list (get_property ("elements"), factor);
211   Duration *d =  unsmob_duration (get_property ("duration"));
212   if (d)
213     set_property ("duration", d ->compressed (factor.main_part_).smobbed_copy ());
214 }
215
216 void
217 Music::transpose (Pitch delta)
218 {
219   if (to_boolean (get_property ("untransposable")))
220     return ;
221   
222   for (SCM s = this->get_property_alist (true); scm_is_pair (s); s = scm_cdr (s))
223     {
224       SCM entry = scm_car (s);
225       SCM val = scm_cdr (entry);
226
227       if (Pitch * p = unsmob_pitch (val))
228         {
229           Pitch transposed =  p->transposed (delta);
230           scm_set_cdr_x (entry, transposed.smobbed_copy ());
231
232           if (abs (transposed.get_alteration ()) > DOUBLE_SHARP)
233             {
234               warning (_f ("Transposition by %s makes alteration larger than double",
235                            delta.to_string ()));
236             }
237         }
238     }
239  
240   SCM elt = get_property ("element");
241
242   if (Music* m = unsmob_music (elt))
243     m->transpose (delta);
244
245   transpose_music_list (get_property ("elements"), delta);
246 }
247
248 IMPLEMENT_TYPE_P (Music, "ly:music?");
249 IMPLEMENT_SMOBS (Music);
250 IMPLEMENT_DEFAULT_EQUAL_P (Music);
251
252 SCM
253 Music::internal_get_property (SCM sym) const
254 {
255   SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
256   if (s != SCM_BOOL_F)
257     return scm_cdr (s);
258
259   s = scm_sloppy_assq (sym, immutable_property_alist_);
260   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
261 }
262
263 void
264 Music::internal_set_property (SCM s, SCM v)
265 {
266   if (do_internal_type_checking_global)
267     if (!type_check_assignment (s, v, ly_symbol2scm ("music-type?")))
268       abort ();
269
270   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
271 }
272
273 void
274 Music::set_spot (Input ip)
275 {
276   set_property ("origin", make_input (ip));
277 }
278
279 Input*
280 Music::origin () const
281 {
282   Input *ip = unsmob_input (get_property ("origin"));
283   return ip ? ip : & dummy_input_global;
284 }
285
286 int
287 Music::duration_log () const
288 {
289   if (is_mus_type ("rhythmic-event"))
290     return unsmob_duration (get_property ("duration"))->duration_log ();
291   return 0;
292 }
293
294 Music*
295 make_music_by_name (SCM sym)
296 {
297   SCM make_music_proc = ly_lily_module_constant ("make-music");
298   SCM rv = scm_call_1 (make_music_proc, sym);
299
300   /* UGH. */
301   scm_gc_protect_object (rv);
302   return unsmob_music (rv);
303 }