2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 #include "duration.hh"
23 #include "program-option.hh"
26 LY_DEFINE (ly_music_length, "ly:music-length",
28 "Get the length of music expression @var{mus} and return"
29 " it as a @code{Moment} object.")
31 LY_ASSERT_TYPE (Music::unsmob, mus, 1);
32 Music *sc = Music::unsmob (mus);
33 return sc->get_length ().smobbed_copy ();
36 LY_DEFINE (ly_music_property, "ly:music-property",
37 2, 1, 0, (SCM mus, SCM sym, SCM val),
38 "Return the value for property @var{sym} of music expression"
39 " @var{mus}. If no value is found, return @var{val} or"
40 " @code{'()} if @var{val} is not specified.")
42 LY_ASSERT_TYPE (Music::unsmob, mus, 1);
43 return ly_prob_property (mus, sym, val);
46 LY_DEFINE (ly_music_set_property_x, "ly:music-set-property!",
47 3, 0, 0, (SCM mus, SCM sym, SCM val),
48 "Set property @var{sym} in music expression @var{mus} to"
51 LY_ASSERT_TYPE (Music::unsmob, mus, 1);
53 return ly_prob_set_property_x (mus, sym, val);
56 /* todo: property args */
57 LY_DEFINE (ly_make_music, "ly:make-music",
59 "Make a C++ @code{Music} object and initialize it with"
62 "This function is for internal use and is only called by"
63 " @code{make-music}, which is the preferred interface"
64 " for creating music objects.")
66 Music *ms = new Music (props);
67 return ms->unprotect ();
70 LY_DEFINE (ly_music_p, "ly:music?",
72 "Is @var{obj} a music object?")
74 return scm_from_bool (Music::unsmob (obj));
77 LY_DEFINE (ly_event_p, "ly:event?",
79 "Is @var{obj} a proper (non-rhythmic) event object?")
81 if (Music *m = Music::unsmob (obj))
83 return scm_from_bool (m->is_mus_type ("post-event"));
88 /* todo: property args */
89 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
91 "Return an alist containing the mutable properties of @var{mus}."
92 " The immutable properties are not available, since they are"
93 " constant and initialized by the @code{make-music} function.")
95 LY_ASSERT_TYPE (Music::unsmob, mus, 1);
96 Music *m = Music::unsmob (mus);
97 return m->get_property_alist (true);
100 LY_DEFINE (ly_music_list_p, "ly:music-list?",
102 "Is @var{lst} a list of music objects?")
104 if (!ly_is_list (lst))
107 while (scm_is_pair (lst))
109 if (!Music::unsmob (scm_car (lst)))
117 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
119 "Copy @var{m} and all sub expressions of@tie{}@var{m}."
120 " @var{m} may be an arbitrary type; cons cells and music"
121 " are copied recursively.")
123 if (Music::unsmob (m))
124 return Music::unsmob (m)->clone ()->unprotect ();
130 copy = scm_cons (ly_music_deep_copy (scm_car (m)), copy);
133 while (scm_is_pair (m));
134 // Oh, come on, GUILE. Why do you require the second argument
135 // of scm_reverse_x to be a proper list? That makes no sense.
136 // return scm_reverse_x (copy, ly_music_deep_copy (m));
137 SCM last_cons = copy;
138 copy = scm_reverse_x (copy, SCM_EOL);
139 scm_set_cdr_x (last_cons, ly_music_deep_copy (m));
145 LY_DEFINE (ly_music_transpose, "ly:music-transpose",
146 2, 0, 0, (SCM m, SCM p),
147 "Transpose @var{m} such that central@tie{}C is mapped"
148 " to@tie{}@var{p}. Return@tie{}@var{m}.")
150 LY_ASSERT_TYPE (Music::unsmob, m, 1);
151 LY_ASSERT_SMOB (Pitch, p, 2);
153 Music *sc = Music::unsmob (m);
154 Pitch *sp = Pitch::unsmob (p);
158 return sc->self_scm ();
162 TODO: should take moment factor?
164 LY_DEFINE (ly_music_compress, "ly:music-compress",
165 2, 0, 0, (SCM m, SCM factor),
166 "Compress music object@tie{}@var{m} by moment @var{factor}.")
168 LY_ASSERT_TYPE (Music::unsmob, m, 1);
169 LY_ASSERT_TYPE (Moment::unsmob, factor, 2);
171 Music *sc = Music::unsmob (m);
172 sc->compress (*Moment::unsmob (factor));
173 return sc->self_scm ();
176 LY_DEFINE (ly_make_music_relative_x, "ly:make-music-relative!",
177 2, 0, 0, (SCM music, SCM pitch),
178 "Make @var{music} relative to @var{pitch},"
179 " return final pitch.")
181 LY_ASSERT_TYPE (Music::unsmob, music, 1);
182 LY_ASSERT_TYPE (Pitch::unsmob, pitch, 2);
184 Pitch start = *Pitch::unsmob (pitch);
185 Music *m = Music::unsmob (music);
186 Pitch last = m->to_relative_octave (start);
188 return last.smobbed_copy ();
191 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
193 "Extract the duration field from @var{mus} and return the"
196 LY_ASSERT_TYPE (Music::unsmob, mus, 1);
197 Music *m = Music::unsmob (mus);
199 Duration *d = Duration::unsmob (m->get_property ("duration"));
203 len = d->get_length ();
205 programming_error ("music has no duration");
206 return len.smobbed_copy ();
209 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
211 "Compress @var{mus} by factor @var{fact}, which is a"
214 LY_ASSERT_TYPE (Music::unsmob, mus, 1);
215 LY_ASSERT_SMOB (Moment, fact, 2);
217 Music *m = Music::unsmob (mus);
218 Moment *f = Moment::unsmob (fact);
220 Duration *d = Duration::unsmob (m->get_property ("duration"));
222 m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
223 return SCM_UNSPECIFIED;
227 This is hairy, since the scale in a key-change event may contain
231 TODO: this should use ly:pitch.
233 LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist",
234 2, 0, 0, (SCM l, SCM pit),
235 "Make a new key alist of@tie{}@var{l} transposed by"
238 SCM newlist = SCM_EOL;
239 Pitch *p = Pitch::unsmob (pit);
241 for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
243 SCM key = scm_caar (s);
244 SCM alter = scm_cdar (s);
245 if (scm_is_pair (key))
247 Pitch orig (scm_to_int (scm_car (key)),
248 scm_to_int (scm_cdr (key)),
249 ly_scm2rational (alter));
251 orig = orig.transposed (*p);
253 SCM key = scm_cons (scm_from_int (orig.get_octave ()),
254 scm_from_int (orig.get_notename ()));
256 newlist = scm_cons (scm_cons (key, ly_rational2scm (orig.get_alteration ())),
259 else if (scm_is_number (key))
261 Pitch orig (0, scm_to_int (key), ly_scm2rational (alter));
262 orig = orig.transposed (*p);
264 key = scm_from_int (orig.get_notename ());
265 alter = ly_rational2scm (orig.get_alteration ());
266 newlist = scm_cons (scm_cons (key, alter), newlist);
269 return scm_reverse_x (newlist, SCM_EOL);