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 /* todo: property args */
77 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
79 "Return an alist containing the mutable properties of @var{mus}."
80 " The immutable properties are not available, since they are"
81 " constant and initialized by the @code{make-music} function.")
83 LY_ASSERT_TYPE (unsmob_music, mus, 1);
84 Music *m = unsmob_music (mus);
85 return m->get_property_alist (true);
88 LY_DEFINE (ly_music_list_p, "ly:music-list?",
90 "Is @var{lst} a list of music objects?")
92 if (scm_list_p (lst) == SCM_BOOL_T)
93 while (scm_is_pair (lst))
95 if (!unsmob_music (scm_car (lst)))
103 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
105 "Copy @var{m} and all sub expressions of@tie{}@var{m}.")
108 if (unsmob_music (m))
110 Music *mcopy = unsmob_music (m)->clone ();
111 copy = mcopy->unprotect ();
113 else if (scm_is_pair (m))
114 copy = scm_cons (ly_music_deep_copy (scm_car (m)),
115 ly_music_deep_copy (scm_cdr (m)));
119 LY_DEFINE (ly_music_transpose, "ly:music-transpose",
120 2, 0, 0, (SCM m, SCM p),
121 "Transpose @var{m} such that central@tie{}C is mapped"
122 " to@tie{}@var{p}. Return@tie{}@var{m}.")
124 LY_ASSERT_TYPE (unsmob_music, m, 1);
125 LY_ASSERT_SMOB (Pitch, p, 2);
127 Music *sc = unsmob_music (m);
128 Pitch *sp = unsmob_pitch (p);
132 return sc->self_scm ();
136 TODO: should take moment factor?
138 LY_DEFINE (ly_music_compress, "ly:music-compress",
139 2, 0, 0, (SCM m, SCM factor),
140 "Compress music object@tie{}@var{m} by moment @var{factor}.")
142 LY_ASSERT_TYPE (unsmob_music, m, 1);
143 LY_ASSERT_TYPE (unsmob_moment, factor, 2);
145 Music *sc = unsmob_music (m);
146 sc->compress (*unsmob_moment (factor));
147 return sc->self_scm ();
150 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
152 "Extract the duration field from @var{mus} and return the"
155 LY_ASSERT_TYPE (unsmob_music, mus, 1);
156 Music *m = unsmob_music (mus);
158 Duration *d = unsmob_duration (m->get_property ("duration"));
162 len = d->get_length ();
164 programming_error ("music has no duration");
165 return len.smobbed_copy ();
168 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
170 "Compress @var{mus} by factor @var{fact}, which is a"
173 LY_ASSERT_TYPE (unsmob_music, mus, 1);
174 LY_ASSERT_SMOB (Moment, fact, 2);
176 Music *m = unsmob_music (mus);
177 Moment *f = unsmob_moment (fact);
179 Duration *d = unsmob_duration (m->get_property ("duration"));
181 m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
182 return SCM_UNSPECIFIED;
186 This is hairy, since the scale in a key-change event may contain
190 TODO: this should use ly:pitch.
192 LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist",
193 2, 0, 0, (SCM l, SCM pit),
194 "Make a new key alist of@tie{}@var{l} transposed by"
197 SCM newlist = SCM_EOL;
198 Pitch *p = unsmob_pitch (pit);
200 for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
202 SCM key = scm_caar (s);
203 SCM alter = scm_cdar (s);
204 if (scm_is_pair (key))
206 Pitch orig (scm_to_int (scm_car (key)),
207 scm_to_int (scm_cdr (key)),
208 ly_scm2rational (alter));
210 orig = orig.transposed (*p);
212 SCM key = scm_cons (scm_from_int (orig.get_octave ()),
213 scm_from_int (orig.get_notename ()));
215 newlist = scm_cons (scm_cons (key, ly_rational2scm (orig.get_alteration ())),
218 else if (scm_is_number (key))
220 Pitch orig (0, scm_to_int (key), ly_scm2rational (alter));
221 orig = orig.transposed (*p);
223 key = scm_from_int (orig.get_notename ());
224 alter = ly_rational2scm (orig.get_alteration ());
225 newlist = scm_cons (scm_cons (key, alter), newlist);
228 return scm_reverse_x (newlist, SCM_EOL);