]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-scheme.cc
dc2d9a6aa24640bb933942cd13050b25467af5a5
[lilypond.git] / lily / music-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "music.hh"
21
22 #include "duration.hh"
23 #include "program-option.hh"
24 #include "warn.hh"
25
26 LY_DEFINE (ly_music_length, "ly:music-length",
27            1, 0, 0, (SCM mus),
28            "Get the length of music expression @var{mus} and return"
29            " it as a @code{Moment} object.")
30 {
31   LY_ASSERT_TYPE (unsmob_music, mus, 1);
32   Music *sc = unsmob_music (mus);
33   return sc->get_length ().smobbed_copy ();
34 }
35
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.")
41 {
42   LY_ASSERT_TYPE (unsmob_music, mus, 1);
43   return ly_prob_property (mus, sym, val);
44 }
45
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"
49            " @var{val}.")
50 {
51   LY_ASSERT_TYPE (unsmob_music, mus, 1);
52
53   return ly_prob_set_property_x (mus, sym, val);
54 }
55
56 /* todo:  property args */
57 LY_DEFINE (ly_make_music, "ly:make-music",
58            1, 0, 0, (SCM props),
59            "Make a C++ @code{Music} object and initialize it with"
60            " @var{props}.\n"
61            "\n"
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.")
65 {
66   Music *ms = new Music (props);
67   return ms->unprotect ();
68 }
69
70 LY_DEFINE (ly_music_p, "ly:music?",
71            1, 0, 0, (SCM obj),
72            "Is @var{obj} a music object?")
73 {
74   return scm_from_bool (unsmob_music (obj));
75 }
76
77 LY_DEFINE (ly_event_p, "ly:event?",
78            1, 0, 0, (SCM obj),
79            "Is @var{obj} a proper (non-rhythmic) event object?")
80 {
81   if (Music *m = unsmob_music (obj))
82     {
83       return scm_from_bool (m->is_mus_type ("post-event"));
84     }
85   return SCM_BOOL_F;
86 }
87
88 /* todo: property args */
89 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
90            1, 0, 0, (SCM mus),
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.")
94 {
95   LY_ASSERT_TYPE (unsmob_music, mus, 1);
96   Music *m = unsmob_music (mus);
97   return m->get_property_alist (true);
98 }
99
100 LY_DEFINE (ly_music_list_p, "ly:music-list?",
101            1, 0, 0, (SCM lst),
102            "Is @var{lst} a list of music objects?")
103 {
104   if (scm_list_p (lst) == SCM_BOOL_T)
105     while (scm_is_pair (lst))
106       {
107         if (!unsmob_music (scm_car (lst)))
108           return SCM_BOOL_F;
109         lst = scm_cdr (lst);
110       }
111
112   return SCM_BOOL_T;
113 }
114
115 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
116            1, 0, 0, (SCM m),
117            "Copy @var{m} and all sub expressions of@tie{}@var{m}."
118            " @var{m} may be an arbitrary type; cons cells and music"
119            " are copied recursively.")
120 {
121   if (unsmob_music (m))
122       return unsmob_music (m)->clone ()->unprotect ();
123   if (scm_is_pair (m))
124     {
125       SCM copy = SCM_EOL;
126       do
127         {
128           copy = scm_cons (ly_music_deep_copy (scm_car (m)), copy);
129           m = scm_cdr (m);
130         }
131       while (scm_is_pair (m));
132       // Oh, come on, GUILE.  Why do you require the second argument
133       // of scm_reverse_x to be a proper list?  That makes no sense.
134       // return scm_reverse_x (copy, ly_music_deep_copy (m));
135       SCM last_cons = copy;
136       copy = scm_reverse_x (copy, SCM_EOL);
137       scm_set_cdr_x (last_cons, ly_music_deep_copy (m));
138       return copy;
139     }
140   return m;
141 }
142
143 LY_DEFINE (ly_music_transpose, "ly:music-transpose",
144            2, 0, 0, (SCM m, SCM p),
145            "Transpose @var{m} such that central@tie{}C is mapped"
146            " to@tie{}@var{p}.  Return@tie{}@var{m}.")
147 {
148   LY_ASSERT_TYPE (unsmob_music, m, 1);
149   LY_ASSERT_SMOB (Pitch, p, 2);
150
151   Music *sc = unsmob_music (m);
152   Pitch *sp = unsmob_pitch (p);
153
154   sc->transpose (*sp);
155   // SCM_UNDEFINED ?
156   return sc->self_scm ();
157 }
158
159 /*
160   TODO: should take moment factor?
161 */
162 LY_DEFINE (ly_music_compress, "ly:music-compress",
163            2, 0, 0, (SCM m, SCM factor),
164            "Compress music object@tie{}@var{m} by moment @var{factor}.")
165 {
166   LY_ASSERT_TYPE (unsmob_music, m, 1);
167   LY_ASSERT_TYPE (unsmob_moment, factor, 2);
168
169   Music *sc = unsmob_music (m);
170   sc->compress (*unsmob_moment (factor));
171   return sc->self_scm ();
172 }
173
174 LY_DEFINE (ly_make_music_relative_x, "ly:make-music-relative!",
175            2, 0, 0, (SCM music, SCM pitch),
176            "Make @var{music} relative to @var{pitch},"
177            " return final pitch.")
178 {
179   LY_ASSERT_TYPE (unsmob_music, music, 1);
180   LY_ASSERT_TYPE (unsmob_pitch, pitch, 2);
181
182   Pitch start = *unsmob_pitch (pitch);
183   Music *m = unsmob_music (music);
184   Pitch last = m->to_relative_octave (start);
185
186   return last.smobbed_copy ();
187 }
188
189 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
190            (SCM mus),
191            "Extract the duration field from @var{mus} and return the"
192            " length.")
193 {
194   LY_ASSERT_TYPE (unsmob_music, mus, 1);
195   Music *m = unsmob_music (mus);
196
197   Duration *d = unsmob_duration (m->get_property ("duration"));
198   Moment len;
199
200   if (d)
201     len = d->get_length ();
202   else
203     programming_error ("music has no duration");
204   return len.smobbed_copy ();
205 }
206
207 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
208            (SCM mus, SCM fact),
209            "Compress @var{mus} by factor @var{fact}, which is a"
210            " @code{Moment}.")
211 {
212   LY_ASSERT_TYPE (unsmob_music, mus, 1);
213   LY_ASSERT_SMOB (Moment, fact, 2);
214
215   Music *m = unsmob_music (mus);
216   Moment *f = unsmob_moment (fact);
217
218   Duration *d = unsmob_duration (m->get_property ("duration"));
219   if (d)
220     m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
221   return SCM_UNSPECIFIED;
222 }
223
224 /*
225   This is hairy, since the scale in a key-change event may contain
226   octaveless notes.
227
228
229   TODO: this should use ly:pitch.
230 */
231 LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist",
232            2, 0, 0, (SCM l, SCM pit),
233            "Make a new key alist of@tie{}@var{l} transposed by"
234            " pitch @var{pit}.")
235 {
236   SCM newlist = SCM_EOL;
237   Pitch *p = unsmob_pitch (pit);
238
239   for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
240     {
241       SCM key = scm_caar (s);
242       SCM alter = scm_cdar (s);
243       if (scm_is_pair (key))
244         {
245           Pitch orig (scm_to_int (scm_car (key)),
246                       scm_to_int (scm_cdr (key)),
247                       ly_scm2rational (alter));
248
249           orig = orig.transposed (*p);
250
251           SCM key = scm_cons (scm_from_int (orig.get_octave ()),
252                               scm_from_int (orig.get_notename ()));
253
254           newlist = scm_cons (scm_cons (key, ly_rational2scm (orig.get_alteration ())),
255                               newlist);
256         }
257       else if (scm_is_number (key))
258         {
259           Pitch orig (0, scm_to_int (key), ly_scm2rational (alter));
260           orig = orig.transposed (*p);
261
262           key = scm_from_int (orig.get_notename ());
263           alter = ly_rational2scm (orig.get_alteration ());
264           newlist = scm_cons (scm_cons (key, alter), newlist);
265         }
266     }
267   return scm_reverse_x (newlist, SCM_EOL);
268 }
269