]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-scheme.cc
Message-handling: Make the Input class use the functions in warn.cc
[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 LY_DEFINE (ly_event_p, "ly:event?",
77            1, 0, 0, (SCM obj),
78            "Is @var{obj} an event object?")
79 {
80   if (Music *m = unsmob_music (obj))
81     {
82       return scm_from_bool (m->is_mus_type ("event"));
83     }
84   return SCM_BOOL_F;
85 }
86
87 /* todo: property args */
88 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
89            1, 0, 0, (SCM mus),
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.")
93 {
94   LY_ASSERT_TYPE (unsmob_music, mus, 1);
95   Music *m = unsmob_music (mus);
96   return m->get_property_alist (true);
97 }
98
99 LY_DEFINE (ly_music_list_p, "ly:music-list?",
100            1, 0, 0, (SCM lst),
101            "Is @var{lst} a list of music objects?")
102 {
103   if (scm_list_p (lst) == SCM_BOOL_T)
104     while (scm_is_pair (lst))
105       {
106         if (!unsmob_music (scm_car (lst)))
107           return SCM_BOOL_F;
108         lst = scm_cdr (lst);
109       }
110
111   return SCM_BOOL_T;
112 }
113
114 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
115            1, 0, 0, (SCM m),
116            "Copy @var{m} and all sub expressions of@tie{}@var{m}.")
117 {
118   SCM copy = m;
119   if (unsmob_music (m))
120     {
121       Music *mcopy = unsmob_music (m)->clone ();
122       copy = mcopy->unprotect ();
123     }
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)));
127   return copy;
128 }
129
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}.")
134 {
135   LY_ASSERT_TYPE (unsmob_music, m, 1);
136   LY_ASSERT_SMOB (Pitch, p, 2);
137
138   Music *sc = unsmob_music (m);
139   Pitch *sp = unsmob_pitch (p);
140
141   sc->transpose (*sp);
142   // SCM_UNDEFINED ?
143   return sc->self_scm ();
144 }
145
146 /*
147   TODO: should take moment factor?
148 */
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}.")
152 {
153   LY_ASSERT_TYPE (unsmob_music, m, 1);
154   LY_ASSERT_TYPE (unsmob_moment, factor, 2);
155
156   Music *sc = unsmob_music (m);
157   sc->compress (*unsmob_moment (factor));
158   return sc->self_scm ();
159 }
160
161 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
162            (SCM mus),
163            "Extract the duration field from @var{mus} and return the"
164            " length.")
165 {
166   LY_ASSERT_TYPE (unsmob_music, mus, 1);
167   Music *m = unsmob_music (mus);
168
169   Duration *d = unsmob_duration (m->get_property ("duration"));
170   Moment len;
171
172   if (d)
173     len = d->get_length ();
174   else
175     programming_error ("music has no duration");
176   return len.smobbed_copy ();
177 }
178
179 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
180            (SCM mus, SCM fact),
181            "Compress @var{mus} by factor @var{fact}, which is a"
182            " @code{Moment}.")
183 {
184   LY_ASSERT_TYPE (unsmob_music, mus, 1);
185   LY_ASSERT_SMOB (Moment, fact, 2);
186
187   Music *m = unsmob_music (mus);
188   Moment *f = unsmob_moment (fact);
189
190   Duration *d = unsmob_duration (m->get_property ("duration"));
191   if (d)
192     m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
193   return SCM_UNSPECIFIED;
194 }
195
196 /*
197   This is hairy, since the scale in a key-change event may contain
198   octaveless notes.
199
200
201   TODO: this should use ly:pitch.
202 */
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"
206            " pitch @var{pit}.")
207 {
208   SCM newlist = SCM_EOL;
209   Pitch *p = unsmob_pitch (pit);
210
211   for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
212     {
213       SCM key = scm_caar (s);
214       SCM alter = scm_cdar (s);
215       if (scm_is_pair (key))
216         {
217           Pitch orig (scm_to_int (scm_car (key)),
218                       scm_to_int (scm_cdr (key)),
219                       ly_scm2rational (alter));
220
221           orig = orig.transposed (*p);
222
223           SCM key = scm_cons (scm_from_int (orig.get_octave ()),
224                               scm_from_int (orig.get_notename ()));
225
226           newlist = scm_cons (scm_cons (key, ly_rational2scm (orig.get_alteration ())),
227                               newlist);
228         }
229       else if (scm_is_number (key))
230         {
231           Pitch orig (0, scm_to_int (key), ly_scm2rational (alter));
232           orig = orig.transposed (*p);
233
234           key = scm_from_int (orig.get_notename ());
235           alter = ly_rational2scm (orig.get_alteration ());
236           newlist = scm_cons (scm_cons (key, alter), newlist);
237         }
238     }
239   return scm_reverse_x (newlist, SCM_EOL);
240 }
241