2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2011 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"
25 LY_DEFINE (ly_music_length, "ly:music-length",
27 "Get the length of music expression @var{mus} and return"
28 " it as a @code{Moment} object.")
30 LY_ASSERT_TYPE (unsmob_music, mus, 1);
31 Music *sc = unsmob_music (mus);
32 return sc->get_length ().smobbed_copy ();
35 LY_DEFINE (ly_music_property, "ly:music-property",
36 2, 1, 0, (SCM mus, SCM sym, SCM val),
37 "Return the value for property @var{sym} of music expression"
38 " @var{mus}. If no value is found, return @var{val} or"
39 " @code{'()} if @var{val} is not specified.")
41 LY_ASSERT_TYPE (unsmob_music, mus, 1);
42 return ly_prob_property (mus, sym, val);
45 LY_DEFINE (ly_music_set_property_x, "ly:music-set-property!",
46 3, 0, 0, (SCM mus, SCM sym, SCM val),
47 "Set property @var{sym} in music expression @var{mus} to"
50 LY_ASSERT_TYPE (unsmob_music, mus, 1);
52 return ly_prob_set_property_x (mus, sym, val);
55 /* todo: property args */
56 LY_DEFINE (ly_make_music, "ly:make-music",
58 "Make a C++ @code{Music} object and initialize it with"
61 "This function is for internal use and is only called by"
62 " @code{make-music}, which is the preferred interface"
63 " for creating music objects.")
65 Music *ms = new Music (props);
66 return ms->unprotect ();
69 LY_DEFINE (ly_music_p, "ly:music?",
71 "Is @var{obj} a music object?")
73 return scm_from_bool (unsmob_music (obj));
76 LY_DEFINE (ly_event_p, "ly:event?",
78 "Is @var{obj} an event object?")
80 if (Music *m = unsmob_music (obj))
82 return scm_from_bool (m->is_mus_type ("event"));
87 /* todo: property args */
88 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
90 "Return an alist containing the mutable properties of @var{mus}."
91 " The immutable properties are not available, since they are"
92 " constant and initialized by the @code{make-music} function.")
94 LY_ASSERT_TYPE (unsmob_music, mus, 1);
95 Music *m = unsmob_music (mus);
96 return m->get_property_alist (true);
99 LY_DEFINE (ly_music_list_p, "ly:music-list?",
101 "Is @var{lst} a list of music objects?")
103 if (scm_list_p (lst) == SCM_BOOL_T)
104 while (scm_is_pair (lst))
106 if (!unsmob_music (scm_car (lst)))
114 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
116 "Copy @var{m} and all sub expressions of@tie{}@var{m}.")
119 if (unsmob_music (m))
121 Music *mcopy = unsmob_music (m)->clone ();
122 copy = mcopy->unprotect ();
124 else if (scm_is_pair (m))
125 copy = scm_cons (ly_music_deep_copy (scm_car (m)),
126 ly_music_deep_copy (scm_cdr (m)));
130 LY_DEFINE (ly_music_transpose, "ly:music-transpose",
131 2, 0, 0, (SCM m, SCM p),
132 "Transpose @var{m} such that central@tie{}C is mapped"
133 " to@tie{}@var{p}. Return@tie{}@var{m}.")
135 LY_ASSERT_TYPE (unsmob_music, m, 1);
136 LY_ASSERT_SMOB (Pitch, p, 2);
138 Music *sc = unsmob_music (m);
139 Pitch *sp = unsmob_pitch (p);
143 return sc->self_scm ();
147 TODO: should take moment factor?
149 LY_DEFINE (ly_music_compress, "ly:music-compress",
150 2, 0, 0, (SCM m, SCM factor),
151 "Compress music object@tie{}@var{m} by moment @var{factor}.")
153 LY_ASSERT_TYPE (unsmob_music, m, 1);
154 LY_ASSERT_TYPE (unsmob_moment, factor, 2);
156 Music *sc = unsmob_music (m);
157 sc->compress (*unsmob_moment (factor));
158 return sc->self_scm ();
161 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
163 "Extract the duration field from @var{mus} and return the"
166 LY_ASSERT_TYPE (unsmob_music, mus, 1);
167 Music *m = unsmob_music (mus);
169 Duration *d = unsmob_duration (m->get_property ("duration"));
173 len = d->get_length ();
175 programming_error ("music has no duration");
176 return len.smobbed_copy ();
179 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
181 "Compress @var{mus} by factor @var{fact}, which is a"
184 LY_ASSERT_TYPE (unsmob_music, mus, 1);
185 LY_ASSERT_SMOB (Moment, fact, 2);
187 Music *m = unsmob_music (mus);
188 Moment *f = unsmob_moment (fact);
190 Duration *d = unsmob_duration (m->get_property ("duration"));
192 m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
193 return SCM_UNSPECIFIED;
197 This is hairy, since the scale in a key-change event may contain
201 TODO: this should use ly:pitch.
203 LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist",
204 2, 0, 0, (SCM l, SCM pit),
205 "Make a new key alist of@tie{}@var{l} transposed by"
208 SCM newlist = SCM_EOL;
209 Pitch *p = unsmob_pitch (pit);
211 for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
213 SCM key = scm_caar (s);
214 SCM alter = scm_cdar (s);
215 if (scm_is_pair (key))
217 Pitch orig (scm_to_int (scm_car (key)),
218 scm_to_int (scm_cdr (key)),
219 ly_scm2rational (alter));
221 orig = orig.transposed (*p);
223 SCM key = scm_cons (scm_from_int (orig.get_octave ()),
224 scm_from_int (orig.get_notename ()));
226 newlist = scm_cons (scm_cons (key, ly_rational2scm (orig.get_alteration ())),
229 else if (scm_is_number (key))
231 Pitch orig (0, scm_to_int (key), ly_scm2rational (alter));
232 orig = orig.transposed (*p);
234 key = scm_from_int (orig.get_notename ());
235 alter = ly_rational2scm (orig.get_alteration ());
236 newlist = scm_cons (scm_cons (key, alter), newlist);
239 return scm_reverse_x (newlist, SCM_EOL);