2 music.cc -- implement Music
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "music-sequence.hh"
11 #include "duration.hh"
12 #include "input-smob.hh"
13 #include "ly-smobs.icc"
20 Music is anything that has duration and supports both time compression
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",
27 Music::internal_is_music_type (SCM k) const
29 SCM ifs = get_property ("types");
31 return scm_c_memq (k, ifs) != SCM_BOOL_F;
37 SCM nm = get_property ("name");
38 if (scm_is_symbol (nm))
40 return ly_symbol2string (nm);
44 return classname (this);
48 Music::Music (SCM init)
51 immutable_property_alist_ = init;
52 mutable_property_alist_ = SCM_EOL;
55 length_callback_ = get_property ("length-callback");
56 start_callback_ = get_property ("start-callback");
59 Music::Music (Music const &m)
61 immutable_property_alist_ = m.immutable_property_alist_;
62 mutable_property_alist_ = SCM_EOL;
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. */
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 ());
82 Music::get_property_alist (bool m) const
84 return (m) ? mutable_property_alist_ : immutable_property_alist_;
88 Music::mark_smob (SCM m)
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_);
97 Music::get_length () const
99 SCM lst = get_property ("length");
100 if (unsmob_moment (lst))
101 return *unsmob_moment (lst);
103 if (ly_c_procedure_p (length_callback_))
105 SCM res = scm_call_1 (length_callback_, self_scm ());
106 return *unsmob_moment (res);
113 Music::start_mom () const
115 SCM lst = get_property ("start-callback");
116 if (ly_c_procedure_p (lst))
118 SCM res = scm_call_1 (lst, self_scm ());
119 return *unsmob_moment (res);
127 print_alist (SCM a, SCM port)
129 /* SCM_EOL -> catch malformed lists. */
130 for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
132 scm_display (scm_caar (s), port);
133 scm_puts (" = ", port);
134 scm_write (scm_cdar (s), port);
135 scm_puts ("\n", port);
140 Music::print_smob (SCM s, SCM p, scm_print_state*)
142 scm_puts ("#<Music ", p);
143 Music* m = unsmob_music (s);
145 SCM nm = m->get_property ("name");
146 if (scm_is_symbol (nm) || scm_is_string (nm))
149 scm_puts (classname (m),p);
151 /* Printing properties takes a lot of time, especially during backtraces.
152 For inspecting, it is better to explicitly use an inspection
160 Music::to_relative_octave (Pitch last)
162 SCM callback = get_property ("to-relative-callback");
163 if (ly_c_procedure_p (callback))
165 Pitch * p = unsmob_pitch (scm_call_2 (callback, self_scm(), last.smobbed_copy ()));
169 SCM elt = get_property ("element");
170 Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
173 Pitch new_pit = *old_pit;
174 new_pit = new_pit.to_relative_octave (last);
176 SCM check = get_property ("absolute-octave");
177 if (scm_is_number (check) &&
178 new_pit.get_octave () != scm_to_int (check))
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;
189 set_property ("pitch", new_pit.smobbed_copy ());
195 if (Music *m = unsmob_music (elt))
196 last = m->to_relative_octave (last);
198 last = music_list_to_relative (get_property ("elements"), last, false);
203 Music::compress (Moment factor)
205 SCM elt = get_property ("element");
207 if (Music *m = unsmob_music (elt))
208 m->compress (factor);
210 compress_music_list (get_property ("elements"), factor);
211 Duration *d = unsmob_duration (get_property ("duration"));
213 set_property ("duration", d ->compressed (factor.main_part_).smobbed_copy ());
217 Music::transpose (Pitch delta)
219 if (to_boolean (get_property ("untransposable")))
222 for (SCM s = this->get_property_alist (true); scm_is_pair (s); s = scm_cdr (s))
224 SCM entry = scm_car (s);
225 SCM val = scm_cdr (entry);
227 if (Pitch * p = unsmob_pitch (val))
229 Pitch transposed = p->transposed (delta);
230 scm_set_cdr_x (entry, transposed.smobbed_copy ());
232 if (abs (transposed.get_alteration ()) > DOUBLE_SHARP)
234 warning (_f ("Transposition by %s makes alteration larger than double",
235 delta.to_string ()));
240 SCM elt = get_property ("element");
242 if (Music* m = unsmob_music (elt))
243 m->transpose (delta);
245 transpose_music_list (get_property ("elements"), delta);
248 IMPLEMENT_TYPE_P (Music, "ly:music?");
249 IMPLEMENT_SMOBS (Music);
250 IMPLEMENT_DEFAULT_EQUAL_P (Music);
253 Music::internal_get_property (SCM sym) const
255 SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
259 s = scm_sloppy_assq (sym, immutable_property_alist_);
260 return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
264 Music::internal_set_property (SCM s, SCM v)
266 if (do_internal_type_checking_global)
267 if (!type_check_assignment (s, v, ly_symbol2scm ("music-type?")))
270 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
274 Music::set_spot (Input ip)
276 set_property ("origin", make_input (ip));
280 Music::origin () const
282 Input *ip = unsmob_input (get_property ("origin"));
283 return ip ? ip : & dummy_input_global;
287 Music::duration_log () const
289 if (is_mus_type ("rhythmic-event"))
290 return unsmob_duration (get_property ("duration"))->duration_log ();
295 make_music_by_name (SCM sym)
297 SCM make_music_proc = ly_lily_module_constant ("make-music");
298 SCM rv = scm_call_1 (make_music_proc, sym);
301 scm_gc_protect_object (rv);
302 return unsmob_music (rv);