]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-scheme.cc
Grand fixcc.py run on all .hh .cc files.
[lilypond.git] / lily / music-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2011 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 /* todo:  property args */
56 LY_DEFINE (ly_make_music, "ly:make-music",
57            1, 0, 0, (SCM props),
58            "Make a C++ @code{Music} object and initialize it with"
59            " @var{props}.\n"
60            "\n"
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.")
64 {
65   Music *ms = new Music (props);
66   return ms->unprotect ();
67 }
68
69 LY_DEFINE (ly_music_p, "ly:music?",
70            1, 0, 0, (SCM obj),
71            "Is @var{obj} a music object?")
72 {
73   return scm_from_bool (unsmob_music (obj));
74 }
75
76 /* todo: property args */
77 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
78            1, 0, 0, (SCM mus),
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.")
82 {
83   LY_ASSERT_TYPE (unsmob_music, mus, 1);
84   Music *m = unsmob_music (mus);
85   return m->get_property_alist (true);
86 }
87
88 LY_DEFINE (ly_music_list_p, "ly:music-list?",
89            1, 0, 0, (SCM lst),
90            "Is @var{lst} a list of music objects?")
91 {
92   if (scm_list_p (lst) == SCM_BOOL_T)
93     while (scm_is_pair (lst))
94       {
95         if (!unsmob_music (scm_car (lst)))
96           return SCM_BOOL_F;
97         lst = scm_cdr (lst);
98       }
99
100   return SCM_BOOL_T;
101 }
102
103 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
104            1, 0, 0, (SCM m),
105            "Copy @var{m} and all sub expressions of@tie{}@var{m}.")
106 {
107   SCM copy = m;
108   if (unsmob_music (m))
109     {
110       Music *mcopy = unsmob_music (m)->clone ();
111       copy = mcopy->unprotect ();
112     }
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)));
116   return copy;
117 }
118
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}.")
123 {
124   LY_ASSERT_TYPE (unsmob_music, m, 1);
125   LY_ASSERT_SMOB (Pitch, p, 2);
126
127   Music *sc = unsmob_music (m);
128   Pitch *sp = unsmob_pitch (p);
129
130   sc->transpose (*sp);
131   // SCM_UNDEFINED ?
132   return sc->self_scm ();
133 }
134
135 /*
136   TODO: should take moment factor?
137 */
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}.")
141 {
142   LY_ASSERT_TYPE (unsmob_music, m, 1);
143   LY_ASSERT_TYPE (unsmob_moment, factor, 2);
144
145   Music *sc = unsmob_music (m);
146   sc->compress (*unsmob_moment (factor));
147   return sc->self_scm ();
148 }
149
150 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
151            (SCM mus),
152            "Extract the duration field from @var{mus} and return the"
153            " length.")
154 {
155   LY_ASSERT_TYPE (unsmob_music, mus, 1);
156   Music *m = unsmob_music (mus);
157
158   Duration *d = unsmob_duration (m->get_property ("duration"));
159   Moment len;
160
161   if (d)
162     len = d->get_length ();
163   else
164     programming_error ("music has no duration");
165   return len.smobbed_copy ();
166 }
167
168 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
169            (SCM mus, SCM fact),
170            "Compress @var{mus} by factor @var{fact}, which is a"
171            " @code{Moment}.")
172 {
173   LY_ASSERT_TYPE (unsmob_music, mus, 1);
174   LY_ASSERT_SMOB (Moment, fact, 2);
175
176   Music *m = unsmob_music (mus);
177   Moment *f = unsmob_moment (fact);
178
179   Duration *d = unsmob_duration (m->get_property ("duration"));
180   if (d)
181     m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
182   return SCM_UNSPECIFIED;
183 }
184
185 /*
186   This is hairy, since the scale in a key-change event may contain
187   octaveless notes.
188
189
190   TODO: this should use ly:pitch.
191 */
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"
195            " pitch @var{pit}.")
196 {
197   SCM newlist = SCM_EOL;
198   Pitch *p = unsmob_pitch (pit);
199
200   for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
201     {
202       SCM key = scm_caar (s);
203       SCM alter = scm_cdar (s);
204       if (scm_is_pair (key))
205         {
206           Pitch orig (scm_to_int (scm_car (key)),
207                       scm_to_int (scm_cdr (key)),
208                       ly_scm2rational (alter));
209
210           orig = orig.transposed (*p);
211
212           SCM key = scm_cons (scm_from_int (orig.get_octave ()),
213                               scm_from_int (orig.get_notename ()));
214
215           newlist = scm_cons (scm_cons (key, ly_rational2scm (orig.get_alteration ())),
216                               newlist);
217         }
218       else if (scm_is_number (key))
219         {
220           Pitch orig (0, scm_to_int (key), ly_scm2rational (alter));
221           orig = orig.transposed (*p);
222
223           key = scm_from_int (orig.get_notename ());
224           alter = ly_rational2scm (orig.get_alteration ());
225           newlist = scm_cons (scm_cons (key, alter), newlist);
226         }
227     }
228   return scm_reverse_x (newlist, SCM_EOL);
229 }
230