]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-scheme.cc
Merge with master
[lilypond.git] / lily / music-scheme.cc
1 /*
2   music-scheme.cc -- implement Music bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "music.hh"
10
11 #include "duration.hh"
12 #include "warn.hh"
13
14 LY_DEFINE (ly_music_length, "ly:music-length",
15            1, 0, 0, (SCM mus),
16            "Get the length of music expression @var{mus}, and return as a @code{Moment} object.")
17 {
18   LY_ASSERT_TYPE (unsmob_music, mus, 1);
19   Music *sc = unsmob_music (mus);
20   return sc->get_length ().smobbed_copy ();
21 }
22
23 LY_DEFINE (ly_music_property,
24            "ly:music-property", 2, 1, 0, (SCM mus, SCM sym, SCM dfault),
25            "Get the property @var{sym} of music expression @var{mus}.\n"
26            "If @var{sym} is undefined, return @code{'()}.\n")
27 {
28   LY_ASSERT_TYPE (unsmob_music, mus, 1);
29   return ly_prob_property (mus,sym,dfault);
30 }
31
32 LY_DEFINE (ly_music_set_property_x, "ly:music-set-property!",
33            3, 0, 0, (SCM mus, SCM sym, SCM val),
34            "Set property @var{sym} in music expression @var{mus} to @var{val}.")
35 {
36   LY_ASSERT_TYPE (unsmob_music, mus, 1);
37
38   return ly_prob_set_property_x (mus, sym, val);
39 }
40
41
42 /* todo:  property args */
43 LY_DEFINE (ly_make_music, "ly:make-music",
44            1, 0, 0, (SCM props),
45            "Make a C++ Music object, initialize with\n"
46            "@var{props}. \n\n"
47            ""
48            "This function is for internal use, and is only called by "
49            "@code{make-music}, which is the preferred interface "
50            "for creating music objects. ")
51 {
52   Music *ms = new Music (props);
53   return ms->unprotect ();
54 }
55
56 LY_DEFINE (ly_music_p, "ly:music?",
57            1, 0, 0, (SCM obj),
58            "Type predicate")
59 {
60   return scm_from_bool (unsmob_music (obj));
61 }
62
63 /* todo: property args */
64 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
65            1, 0, 0, (SCM mus),
66            "Return an alist containing the mutable properties of @var{mus}.\n"
67            "The immutable properties are not available, since "
68            "they are constant and initialized by the "
69            "@code{make-music} function.\n")
70 {
71   LY_ASSERT_TYPE (unsmob_music, mus, 1);
72   Music *m = unsmob_music (mus);
73   return m->get_property_alist (true);
74 }
75
76 LY_DEFINE (ly_music_list_p, "ly:music-list?",
77            1, 0, 0, (SCM lst),
78            "Type predicate: return true if @var{lst} is a list "
79            "of music objects.")
80 {
81   if (scm_list_p (lst) == SCM_BOOL_T)
82     while (scm_is_pair (lst))
83       {
84         if (!unsmob_music (scm_car (lst)))
85           return SCM_BOOL_F;
86         lst = scm_cdr (lst);
87       }
88
89   return SCM_BOOL_T;
90 }
91
92 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
93            1, 0, 0, (SCM m),
94            "Copy @var{m} and all sub expressions of @var{m}")
95 {
96   SCM copy = m;
97   if (unsmob_music (m))
98     {
99       Music *mcopy = unsmob_music (m)->clone ();
100       copy = mcopy->unprotect ();
101     }
102   else if (scm_is_pair (m))
103     copy = scm_cons (ly_music_deep_copy (scm_car (m)),
104                      ly_music_deep_copy (scm_cdr (m)));
105   return copy;
106 }
107
108 LY_DEFINE (ly_music_transpose, "ly:music-transpose",
109            2, 0, 0, (SCM m, SCM p),
110            "Transpose @var{m} such that central C is mapped to @var{p}. "
111            "Return @var{m}.")
112 {
113   LY_ASSERT_TYPE (unsmob_music, m, 1);
114   LY_ASSERT_SMOB (Pitch, p, 2);
115
116   Music *sc = unsmob_music (m);
117   Pitch *sp = unsmob_pitch (p);
118
119   sc->transpose (*sp);
120   // SCM_UNDEFINED ?
121   return sc->self_scm ();
122 }
123
124 /*
125   TODO: should take moment factor?
126 */
127 LY_DEFINE (ly_music_compress, "ly:music-compress",
128            2, 0, 0, (SCM m, SCM factor),
129            "Compress music object @var{m} by moment @var{factor}.")
130 {
131   LY_ASSERT_TYPE (unsmob_music, m, 1);
132   LY_ASSERT_TYPE(unsmob_moment,factor, 2);
133
134   Music *sc = unsmob_music (m);
135   sc->compress (*unsmob_moment (factor));
136   return sc->self_scm ();
137 }
138
139 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
140            (SCM mus),
141            "Extract the duration field from @var{mus}, and return the length.")
142 {
143   LY_ASSERT_TYPE (unsmob_music, mus, 1);
144   Music *m = unsmob_music (mus);
145
146   Duration *d = unsmob_duration (m->get_property ("duration"));
147   Moment len;
148
149   if (d)
150     len = d->get_length ();
151   else
152     programming_error ("music has no duration");
153   return len.smobbed_copy ();
154 }
155
156 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
157            (SCM mus, SCM fact),
158            "Compress @var{mus} by factor @var{fact}, which is a @code{Moment}.")
159 {
160   LY_ASSERT_TYPE (unsmob_music, mus, 1);
161   LY_ASSERT_SMOB (Moment, fact, 2);
162   
163   Music *m = unsmob_music (mus);
164   Moment *f = unsmob_moment (fact);
165
166   Duration *d = unsmob_duration (m->get_property ("duration"));
167   if (d)
168     m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
169   return SCM_UNSPECIFIED;
170 }
171
172 /*
173   This is hairy, since the scale in a key-change event may contain
174   octaveless notes.
175
176
177   TODO: this should use ly:pitch.
178 */
179 LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist",
180            2, 0, 0, (SCM l, SCM pit),
181            "Make a new key alist of @var{l} transposed by pitch @var{pit}")
182 {
183   SCM newlist = SCM_EOL;
184   Pitch *p = unsmob_pitch (pit);
185
186   for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
187     {
188       SCM key = scm_caar (s);
189       SCM alter = scm_cdar (s);
190       if (scm_is_pair (key))
191         {
192           Pitch orig (scm_to_int (scm_car (key)),
193                       scm_to_int (scm_cdr (key)),
194                       ly_scm2rational (alter));
195
196           orig = orig.transposed (*p);
197
198           SCM key = scm_cons (scm_from_int (orig.get_octave ()),
199                               scm_from_int (orig.get_notename ()));
200
201           newlist = scm_cons (scm_cons (key, ly_rational2scm (orig.get_alteration ())),
202                               newlist);
203         }
204       else if (scm_is_number (key))
205         {
206           Pitch orig (0, scm_to_int (key), ly_scm2rational (alter));
207           orig = orig.transposed (*p);
208
209           key = scm_from_int (orig.get_notename ());
210           alter = ly_rational2scm (orig.get_alteration ());
211           newlist = scm_cons (scm_cons (key, alter), newlist);
212         }
213     }
214   return scm_reverse_x (newlist, SCM_EOL);
215 }
216