]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-scheme.cc
b414dc88ce14ec19c2c024457882ae53722b3b76
[lilypond.git] / lily / music-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2010 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 "warn.hh"
24
25 LY_DEFINE (ly_music_length, "ly:music-length",
26            1, 0, 0, (SCM mus),
27            "Get the length of music expression @var{mus} and return"
28            " it as a @code{Moment} object.")
29 {
30   LY_ASSERT_TYPE (unsmob_music, mus, 1);
31   Music *sc = unsmob_music (mus);
32   return sc->get_length ().smobbed_copy ();
33 }
34
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.")
40 {
41   LY_ASSERT_TYPE (unsmob_music, mus, 1);
42   return ly_prob_property (mus, sym, val);
43 }
44
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"
48            " @var{val}.")
49 {
50   LY_ASSERT_TYPE (unsmob_music, mus, 1);
51
52   return ly_prob_set_property_x (mus, sym, val);
53 }
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 /* todo: property args */
78 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
79            1, 0, 0, (SCM mus),
80            "Return an alist containing the mutable properties of @var{mus}."
81            "  The immutable properties are not available, since they are"
82            " constant and initialized by the @code{make-music} function.")
83 {
84   LY_ASSERT_TYPE (unsmob_music, mus, 1);
85   Music *m = unsmob_music (mus);
86   return m->get_property_alist (true);
87 }
88
89 LY_DEFINE (ly_music_list_p, "ly:music-list?",
90            1, 0, 0, (SCM lst),
91            "Is @var{lst} a list of music objects?")
92 {
93   if (scm_list_p (lst) == SCM_BOOL_T)
94     while (scm_is_pair (lst))
95       {
96         if (!unsmob_music (scm_car (lst)))
97           return SCM_BOOL_F;
98         lst = scm_cdr (lst);
99       }
100
101   return SCM_BOOL_T;
102 }
103
104 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
105            1, 0, 0, (SCM m),
106            "Copy @var{m} and all sub expressions of@tie{}@var{m}.")
107 {
108   SCM copy = m;
109   if (unsmob_music (m))
110     {
111       Music *mcopy = unsmob_music (m)->clone ();
112       copy = mcopy->unprotect ();
113     }
114   else if (scm_is_pair (m))
115     copy = scm_cons (ly_music_deep_copy (scm_car (m)),
116                      ly_music_deep_copy (scm_cdr (m)));
117   return copy;
118 }
119
120 LY_DEFINE (ly_music_transpose, "ly:music-transpose",
121            2, 0, 0, (SCM m, SCM p),
122            "Transpose @var{m} such that central@tie{}C is mapped"
123            " to@tie{}@var{p}.  Return@tie{}@var{m}.")
124 {
125   LY_ASSERT_TYPE (unsmob_music, m, 1);
126   LY_ASSERT_SMOB (Pitch, p, 2);
127
128   Music *sc = unsmob_music (m);
129   Pitch *sp = unsmob_pitch (p);
130
131   sc->transpose (*sp);
132   // SCM_UNDEFINED ?
133   return sc->self_scm ();
134 }
135
136 /*
137   TODO: should take moment factor?
138 */
139 LY_DEFINE (ly_music_compress, "ly:music-compress",
140            2, 0, 0, (SCM m, SCM factor),
141            "Compress music object@tie{}@var{m} by moment @var{factor}.")
142 {
143   LY_ASSERT_TYPE (unsmob_music, m, 1);
144   LY_ASSERT_TYPE (unsmob_moment, factor, 2);
145
146   Music *sc = unsmob_music (m);
147   sc->compress (*unsmob_moment (factor));
148   return sc->self_scm ();
149 }
150
151 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
152            (SCM mus),
153            "Extract the duration field from @var{mus} and return the"
154            " length.")
155 {
156   LY_ASSERT_TYPE (unsmob_music, mus, 1);
157   Music *m = unsmob_music (mus);
158
159   Duration *d = unsmob_duration (m->get_property ("duration"));
160   Moment len;
161
162   if (d)
163     len = d->get_length ();
164   else
165     programming_error ("music has no duration");
166   return len.smobbed_copy ();
167 }
168
169 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
170            (SCM mus, SCM fact),
171            "Compress @var{mus} by factor @var{fact}, which is a"
172            " @code{Moment}.")
173 {
174   LY_ASSERT_TYPE (unsmob_music, mus, 1);
175   LY_ASSERT_SMOB (Moment, fact, 2);
176   
177   Music *m = unsmob_music (mus);
178   Moment *f = unsmob_moment (fact);
179
180   Duration *d = unsmob_duration (m->get_property ("duration"));
181   if (d)
182     m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
183   return SCM_UNSPECIFIED;
184 }
185
186 /*
187   This is hairy, since the scale in a key-change event may contain
188   octaveless notes.
189
190
191   TODO: this should use ly:pitch.
192 */
193 LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist",
194            2, 0, 0, (SCM l, SCM pit),
195            "Make a new key alist of@tie{}@var{l} transposed by"
196            " pitch @var{pit}.")
197 {
198   SCM newlist = SCM_EOL;
199   Pitch *p = unsmob_pitch (pit);
200
201   for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
202     {
203       SCM key = scm_caar (s);
204       SCM alter = scm_cdar (s);
205       if (scm_is_pair (key))
206         {
207           Pitch orig (scm_to_int (scm_car (key)),
208                       scm_to_int (scm_cdr (key)),
209                       ly_scm2rational (alter));
210
211           orig = orig.transposed (*p);
212
213           SCM key = scm_cons (scm_from_int (orig.get_octave ()),
214                               scm_from_int (orig.get_notename ()));
215
216           newlist = scm_cons (scm_cons (key, ly_rational2scm (orig.get_alteration ())),
217                               newlist);
218         }
219       else if (scm_is_number (key))
220         {
221           Pitch orig (0, scm_to_int (key), ly_scm2rational (alter));
222           orig = orig.transposed (*p);
223
224           key = scm_from_int (orig.get_notename ());
225           alter = ly_rational2scm (orig.get_alteration ());
226           newlist = scm_cons (scm_cons (key, alter), newlist);
227         }
228     }
229   return scm_reverse_x (newlist, SCM_EOL);
230 }
231