lilypond-1.3.144
[lilypond.git] / lily / music.cc
blob917e14021b4e75d8c2b535526ab42d195e61a922
1 /*
2 music.cc -- implement Music
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include "input-smob.hh"
10 #include "music.hh"
11 #include "music-list.hh"
12 #include "debug.hh"
13 #include "pitch.hh"
14 #include "ly-smobs.icc"
16 SCM
17 ly_deep_mus_copy (SCM m)
19 if (unsmob_music (m))
21 SCM ss = unsmob_music (m)->clone ()->self_scm ();
22 scm_unprotect_object (ss);
23 return ss;
25 else if (gh_pair_p (m))
27 return gh_cons (ly_deep_mus_copy (gh_car (m)), ly_deep_mus_copy (gh_cdr (m)));
29 else
30 return m;
34 Music::Music ()
36 immutable_property_alist_ = SCM_EOL;
37 mutable_property_alist_ = SCM_EOL;
38 smobify_self ();
41 Music::Music (Music const &m)
43 immutable_property_alist_ = m.immutable_property_alist_;
44 SCM c =ly_deep_mus_copy (m.mutable_property_alist_);
45 mutable_property_alist_ = c;
47 smobify_self ();
49 set_spot (*m.origin ());
53 Music::Music (SCM l)
55 immutable_property_alist_ = l;
56 mutable_property_alist_ = SCM_EOL;
57 smobify_self ();
61 SCM
62 Music::mark_smob (SCM m)
64 Music * mus = (Music *)SCM_CELL_WORD_1 (m);
65 scm_gc_mark (mus->immutable_property_alist_);
66 scm_gc_mark (mus->mutable_property_alist_);
67 return SCM_EOL;
70 void
71 Music::compress (Moment)
77 Moment
78 Music::length_mom () const
80 SCM l = get_mus_property ("length");
81 if (unsmob_moment (l))
82 return *unsmob_moment (l);
83 else if (gh_procedure_p (l))
85 SCM res = gh_call1 (l, self_scm ());
86 return *unsmob_moment (res);
89 return 0;
92 void
93 print_alist (SCM a, SCM port)
95 for (SCM s = a; gh_pair_p (s); s = gh_cdr (s))
97 scm_display (gh_caar (s), port);
98 scm_puts (" = ", port);
99 scm_write (gh_cdar (s), port);
100 scm_puts ("\n", port);
105 Music::print_smob (SCM s, SCM p, scm_print_state*)
107 scm_puts ("#<Music ", p);
108 Music* m = unsmob_music (s);
109 scm_puts (classname (m),p);
111 print_alist (m->mutable_property_alist_, p);
112 print_alist (m->immutable_property_alist_, p);
114 scm_puts (">",p);
115 return 1;
118 Pitch
119 Music::to_relative_octave (Pitch m)
121 return m;
125 void
126 Music::transpose (Pitch)
130 IMPLEMENT_TYPE_P (Music, "music?");
131 IMPLEMENT_UNSMOB (Music,music);
132 IMPLEMENT_SMOBS (Music);
133 IMPLEMENT_DEFAULT_EQUAL_P (Music);
135 /****************************/
138 Music::get_mus_property (const char *nm) const
140 SCM sym = ly_symbol2scm (nm);
141 return get_mus_property (sym);
145 Music::get_mus_property (SCM sym) const
147 SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
148 if (s != SCM_BOOL_F)
149 return gh_cdr (s);
151 s = scm_sloppy_assq (sym, immutable_property_alist_);
152 return (s == SCM_BOOL_F) ? SCM_EOL : gh_cdr (s);
156 Remove the value associated with KEY, and return it. The result is
157 that a next call will yield SCM_EOL (and not the underlying
158 `basic' property.
161 Music::remove_mus_property (const char* key)
163 SCM val = get_mus_property (key);
164 if (val != SCM_EOL)
165 set_mus_property (key, SCM_EOL);
166 return val;
169 void
170 Music::set_mus_property (const char* k, SCM v)
172 SCM s = ly_symbol2scm (k);
173 set_mus_property (s, v);
176 void
177 Music::set_immutable_mus_property (const char*k, SCM v)
179 SCM s = ly_symbol2scm (k);
180 set_immutable_mus_property (s, v);
183 void
184 Music::set_immutable_mus_property (SCM s, SCM v)
186 immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
187 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
189 void
190 Music::set_mus_property (SCM s, SCM v)
192 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
195 #include "main.hh"
197 void
198 Music::set_spot (Input ip)
200 set_mus_property ("origin", make_input (ip));
205 Input*
206 Music::origin () const
208 Input *ip = unsmob_input (get_mus_property ("origin"));
209 return ip ? ip : & dummy_input_global;
216 Music::~Music ()
222 ly_get_mus_property (SCM mus, SCM sym)
224 Music * sc = unsmob_music (mus);
226 if (sc)
228 return sc->get_mus_property (sym);
230 else
232 warning (_ ("ly_get_mus_property (): Not a Music"));
233 scm_write (mus, scm_current_error_port ());
235 return SCM_EOL;
240 ly_set_mus_property (SCM mus, SCM sym, SCM val)
242 Music * sc = unsmob_music (mus);
244 if (!gh_symbol_p (sym))
246 warning (_ ("ly_set_mus_property (): Not a symbol"));
247 scm_write (mus, scm_current_error_port ());
249 return SCM_UNSPECIFIED;
252 if (sc)
254 sc->set_mus_property (sym, val);
256 else
258 warning (_ ("ly_set_mus_property (): not of type Music"));
259 scm_write (mus, scm_current_error_port ());
262 return SCM_UNSPECIFIED;
266 // to do property args
268 ly_make_music (SCM type)
270 if (!gh_string_p (type))
272 warning (_ ("ly_make_music (): Not a string"));
273 scm_write (type, scm_current_error_port ());
275 return SCM_UNSPECIFIED;
277 else
279 SCM s = get_music (ly_scm2string (type))->self_scm ();
280 scm_unprotect_object (s);
281 return s;
286 ly_music_name (SCM mus)
288 Music * m = unsmob_music (mus);
289 const char *nm ="";
290 if (!m)
292 warning (_ ("ly_music_name (): Not a music expression"));
293 scm_write (mus, scm_current_error_port ());
295 else
296 nm = classname (m);
297 return ly_str02scm (nm);
300 static void
301 init_functions ()
303 scm_make_gsubr ("ly-get-mus-property", 2, 0, 0, (Scheme_function_unknown)ly_get_mus_property);
304 scm_make_gsubr ("ly-set-mus-property", 3, 0, 0, (Scheme_function_unknown)ly_set_mus_property);
305 scm_make_gsubr ("ly-make-music", 1, 0, 0, (Scheme_function_unknown)ly_make_music);
306 scm_make_gsubr ("ly-music-name", 1, 0, 0, (Scheme_function_unknown)ly_music_name);
308 ADD_SCM_INIT_FUNC (musicscm,init_functions);
309 ADD_MUSIC(Music);