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>
11 #include "duration.hh"
12 #include "input-smob.hh"
13 #include "ly-smobs.icc"
15 #include "music-list.hh"
21 Music::internal_is_music_type (SCM k) const
23 SCM ifs = get_property ("types");
25 return scm_c_memq (k, ifs) != SCM_BOOL_F;
31 SCM nm = get_property ("name");
32 if (scm_is_symbol (nm))
34 return ly_symbol2string (nm);
38 return classname (this);
45 immutable_property_alist_ = SCM_EOL;
46 mutable_property_alist_ = SCM_EOL;
50 Music::Music (Music const &m)
52 immutable_property_alist_ = m.immutable_property_alist_;
53 mutable_property_alist_ = SCM_EOL;
56 /* First we smobify_self, then we copy over the stuff. If we don't,
57 stack vars that hold the copy might be optimized away, meaning
58 that they won't be protected from GC. */
60 mutable_property_alist_ = ly_music_deep_copy (m.mutable_property_alist_);
61 set_spot (*m.origin ());
71 Music::get_property_alist (bool m) const
73 return (m) ? mutable_property_alist_ : immutable_property_alist_;
77 Music::mark_smob (SCM m)
79 Music *mus = (Music*) SCM_CELL_WORD_1 (m);
80 scm_gc_mark (mus->immutable_property_alist_);
81 scm_gc_mark (mus->mutable_property_alist_);
86 Music::get_length () const
88 SCM lst = get_property ("length");
89 if (unsmob_moment (lst))
90 return *unsmob_moment (lst);
91 else if (ly_c_procedure_p (lst))
93 SCM res = scm_call_1 (lst, self_scm ());
94 return *unsmob_moment (res);
101 Music::start_mom () const
103 SCM lst = get_property ("start-moment-function");
104 if (ly_c_procedure_p (lst))
106 SCM res = scm_call_1 (lst, self_scm ());
107 return *unsmob_moment (res);
115 print_alist (SCM a, SCM port)
117 /* SCM_EOL -> catch malformed lists. */
118 for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
120 scm_display (scm_caar (s), port);
121 scm_puts (" = ", port);
122 scm_write (scm_cdar (s), port);
123 scm_puts ("\n", port);
128 Music::print_smob (SCM s, SCM p, scm_print_state*)
130 scm_puts ("#<Music ", p);
131 Music* m = unsmob_music (s);
133 SCM nm = m->get_property ("name");
134 if (scm_is_symbol (nm) || scm_is_string (nm))
137 scm_puts (classname (m),p);
139 /* Printing properties takes a lot of time, especially during backtraces.
140 For inspecting, it is better to explicitly use an inspection
148 Music::to_relative_octave (Pitch p)
150 SCM elt = get_property ("element");
152 if (Music *m = unsmob_music (elt))
153 p = m->to_relative_octave (p);
155 p = music_list_to_relative (get_property ("elements"), p, false);
160 Music::compress (Moment factor)
162 SCM elt = get_property ("element");
164 if (Music *m = unsmob_music (elt))
165 m->compress (factor);
167 compress_music_list (get_property ("elements"), factor);
171 Music::transpose (Pitch delta)
173 for (SCM s = this->get_property_alist (true); scm_is_pair (s); s = scm_cdr (s))
175 SCM entry = scm_car (s);
176 SCM val = scm_cdr (entry);
178 if (Pitch * p = unsmob_pitch (val))
180 Pitch transposed = p->transposed (delta);
181 scm_set_cdr_x (entry, transposed.smobbed_copy ());
183 if (abs (transposed.get_alteration ()) > DOUBLE_SHARP)
185 warning (_f ("Transposition by %s makes alteration larger than two",
186 delta.to_string ()));
191 SCM elt = get_property ("element");
193 if (Music* m = unsmob_music (elt))
194 m->transpose (delta);
196 transpose_music_list (get_property ("elements"), delta);
199 IMPLEMENT_TYPE_P (Music, "ly:music?");
200 IMPLEMENT_SMOBS (Music);
201 IMPLEMENT_DEFAULT_EQUAL_P (Music);
204 Music::internal_get_property (SCM sym) const
206 SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
210 s = scm_sloppy_assq (sym, immutable_property_alist_);
211 return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
215 Music::internal_set_property (SCM s, SCM v)
217 if (do_internal_type_checking_global)
218 if (!type_check_assignment (s, v, ly_symbol2scm ("music-type?")))
221 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
225 Music::set_spot (Input ip)
227 set_property ("origin", make_input (ip));
231 Music::origin () const
233 Input *ip = unsmob_input (get_property ("origin"));
234 return ip ? ip : & dummy_input_global;
238 Music::duration_log () const
240 if (is_mus_type ("rhythmic-event"))
241 return unsmob_duration (get_property ("duration"))->duration_log ();
246 make_music_by_name (SCM sym)
248 SCM make_music_proc = ly_lily_module_constant ("make-music");
249 SCM rv = scm_call_1 (make_music_proc, sym);
252 scm_gc_protect_object (rv);
253 return unsmob_music (rv);