]> git.donarmstrong.com Git - lilypond.git/blob - lily/music.cc
50d20245b2ddf5f61a55e2bc4565a6b1f4336762
[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--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
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"
15
16 SCM
17 ly_deep_mus_copy (SCM m)
18 {
19   if (unsmob_music (m))
20     {
21       SCM ss =  unsmob_music (m)->clone ()->self_scm ();
22       scm_gc_unprotect_object (ss);
23       return ss;
24     }
25   else if (gh_pair_p (m))
26     {
27       return gh_cons (ly_deep_mus_copy (ly_car (m)), ly_deep_mus_copy (ly_cdr (m)));
28     }
29   else
30     return m;
31 }
32
33
34 Music::Music ()
35 {
36   immutable_property_alist_ = SCM_EOL;
37   mutable_property_alist_ = SCM_EOL;
38   smobify_self ();
39 }
40
41 Music::Music (Music const &m)
42 {
43   immutable_property_alist_ = m.immutable_property_alist_;
44   SCM c =ly_deep_mus_copy (m.mutable_property_alist_);
45   mutable_property_alist_ = c;
46
47   smobify_self ();
48
49   set_spot (*m.origin ());
50 }
51
52
53 Music::Music (SCM l)
54 {
55   immutable_property_alist_ = l;
56   mutable_property_alist_ = SCM_EOL;
57   smobify_self ();
58 }
59
60
61 SCM
62 Music::mark_smob (SCM m)
63 {
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;
68 }
69
70 void    
71 Music::compress (Moment)
72 {
73 }
74
75
76
77 Moment
78 Music::length_mom () const
79 {
80   SCM l = get_mus_property ("length");
81   if (unsmob_moment (l))
82     return *unsmob_moment (l);
83   else if (gh_procedure_p (l))
84     {
85       SCM res = gh_call1 (l, self_scm ());
86       return *unsmob_moment (res);
87     }
88     
89   return 0;
90 }
91
92 Moment
93 Music::start_mom () const
94 {
95   Moment m ;
96   return m;
97 }
98
99 void
100 print_alist (SCM a, SCM port)
101 {
102   for (SCM s = a; gh_pair_p (s); s = ly_cdr (s))
103     {
104       scm_display (ly_caar (s), port);
105       scm_puts (" = ", port); 
106       scm_write (ly_cdar (s), port);
107       scm_puts ("\n", port);
108     }
109 }
110
111 int
112 Music::print_smob (SCM s, SCM p, scm_print_state*)
113 {
114   scm_puts ("#<Music ", p);
115   Music* m = unsmob_music (s);
116   scm_puts (classname (m),p);
117
118   print_alist (m->mutable_property_alist_, p);
119   print_alist (m->immutable_property_alist_, p);
120   
121   scm_puts (">",p);
122   return 1;
123 }
124
125 Pitch
126 Music::to_relative_octave (Pitch m)
127 {
128   return m;
129 }
130
131
132 void
133 Music::transpose (Pitch delta)
134 {
135   Pitch *p = unsmob_pitch (get_mus_property ("pitch"));
136   if (!p)
137     return ;
138
139   Pitch np = *p;
140   np.transpose (delta);
141   
142   if (abs (np.alteration_i_) > 2)
143     {
144         warning (_f ("Transposition by %s makes accidental larger than two",
145           delta.str ()));
146     }
147
148   set_mus_property ("pitch", np.smobbed_copy ());
149 }
150
151 IMPLEMENT_TYPE_P (Music, "music?");
152
153 IMPLEMENT_SMOBS (Music);
154 IMPLEMENT_DEFAULT_EQUAL_P (Music);
155
156 /****************************/
157
158 SCM
159 Music::internal_get_mus_property (SCM sym) const
160 {
161   SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
162   if (s != SCM_BOOL_F)
163     return ly_cdr (s);
164
165   s = scm_sloppy_assq (sym, immutable_property_alist_);
166   return (s == SCM_BOOL_F) ? SCM_EOL : ly_cdr (s); 
167 }
168
169 #if 0
170 /*
171   Remove the value associated with KEY, and return it. The result is
172   that a next call will yield SCM_EOL (and not the underlying
173   `basic' property.
174 */
175 SCM
176 Music::remove_mus_property (const char* key)
177 {
178   SCM val = get_mus_property (key);
179   if (val != SCM_EOL)
180     set_mus_property (key, SCM_EOL);
181   return val;
182 }
183
184 SCM
185 Music::get_mus_property (const char *nm) const
186 {
187   SCM sym = ly_symbol2scm (nm);
188   return get_mus_property (sym);
189 }
190
191 void
192 Music::set_mus_property (const char* k, SCM v)
193 {
194   SCM s = ly_symbol2scm (k);
195   set_mus_property (s, v);
196 }
197
198 void
199 Music::set_immutable_mus_property (SCM s, SCM v)
200 {
201   immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
202   mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
203 }
204 #endif
205
206 void
207 Music::internal_set_mus_property (SCM s, SCM v)
208 {
209   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
210 }
211
212 #include "main.hh"
213
214 void
215 Music::set_spot (Input ip)
216 {
217   set_mus_property ("origin", make_input (ip));
218 }
219
220
221
222 Input*
223 Music::origin () const
224 {
225   Input *ip = unsmob_input (get_mus_property ("origin"));
226   return ip ? ip : & dummy_input_global; 
227 }
228
229
230
231
232
233 Music::~Music ()
234 {
235   
236 }
237
238 SCM
239 ly_get_mus_property (SCM mus, SCM sym)
240 {
241   Music * sc = unsmob_music (mus);
242   
243   if (sc)
244     {
245       return sc->internal_get_mus_property (sym);
246     }
247   else
248     {
249       warning (_ ("ly_get_mus_property (): Not a Music"));
250       scm_write (mus, scm_current_error_port ());
251     }
252   return SCM_EOL;
253 }
254
255
256 SCM
257 ly_set_mus_property (SCM mus, SCM sym, SCM val)
258 {
259   Music * sc = unsmob_music (mus);
260
261   if (!gh_symbol_p (sym))
262     {
263       warning (_ ("ly_set_mus_property (): Not a symbol"));
264       scm_write (mus, scm_current_error_port ());      
265
266       return SCM_UNSPECIFIED;
267     }
268
269   if (sc)
270     {
271       sc->internal_set_mus_property (sym, val);
272     }
273   else
274     {
275       warning (_ ("ly_set_mus_property ():  not of type Music"));
276       scm_write (mus, scm_current_error_port ());
277     }
278
279   return SCM_UNSPECIFIED;
280 }
281
282
283 // to do  property args 
284 SCM
285 ly_make_music (SCM type)
286 {
287   if (!gh_string_p (type))
288     {
289       warning (_ ("ly_make_music (): Not a string"));
290       scm_write (type, scm_current_error_port ());      
291
292       return SCM_UNSPECIFIED;
293     }
294   else
295     {
296       SCM s =       get_music (ly_scm2string (type))->self_scm ();
297       scm_gc_unprotect_object (s);
298       return s;
299     }
300 }
301
302 SCM
303 ly_music_name (SCM mus)
304 {
305   Music * m = unsmob_music (mus);
306   const char *nm ="";
307   if (!m)
308     {
309       warning (_ ("ly_music_name (): Not a music expression"));
310       scm_write (mus, scm_current_error_port ());      
311     }
312    else
313      nm = classname (m);
314    return ly_str02scm (nm);
315 }
316
317 static void
318 init_functions ()
319 {
320   scm_c_define_gsubr ("ly-get-mus-property", 2, 0, 0, (Scheme_function_unknown)ly_get_mus_property);
321   scm_c_define_gsubr ("ly-set-mus-property", 3, 0, 0, (Scheme_function_unknown)ly_set_mus_property);
322   scm_c_define_gsubr ("ly-make-music", 1, 0, 0, (Scheme_function_unknown)ly_make_music);
323   scm_c_define_gsubr ("ly-music-name", 1, 0, 0, (Scheme_function_unknown)ly_music_name);    
324 }
325 ADD_SCM_INIT_FUNC (musicscm,init_functions);
326 ADD_MUSIC(Music);