]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-scheme.cc
Merge with git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond.git
[lilypond.git] / lily / music-scheme.cc
1 /*
2   music-scheme.cc -- implement Music bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "music.hh"
10
11 #include "duration.hh"
12 #include "warn.hh"
13 #include "pitch.hh"
14
15 LY_DEFINE (ly_music_length, "ly:music-length",
16            1, 0, 0, (SCM mus),
17            "Get the length of music expression @var{mus}, and return as a @code{Moment} object.")
18 {
19   Music *sc = unsmob_music (mus);
20   SCM_ASSERT_TYPE (sc, mus, SCM_ARG1, __FUNCTION__, "music");
21   return sc->get_length ().smobbed_copy ();
22 }
23
24 LY_DEFINE (ly_music_property,
25            "ly:music-property", 2, 1, 0, (SCM mus, SCM sym, SCM dfault),
26            "Get the property @var{sym} of music expression @var{mus}.\n"
27            "If @var{sym} is undefined, return @code{'()}.\n")
28 {
29   Music *sc = unsmob_music (mus);
30   SCM_ASSERT_TYPE (sc, mus, SCM_ARG1, __FUNCTION__, "music");
31   return ly_prob_property (mus,sym,dfault);
32 }
33
34 LY_DEFINE (ly_music_set_property, "ly:music-set-property!",
35            3, 0, 0, (SCM mus, SCM sym, SCM val),
36            "Set property @var{sym} in music expression @var{mus} to @var{val}.")
37 {
38   Music *sc = unsmob_music (mus);
39   SCM_ASSERT_TYPE (sc, mus, SCM_ARG1, __FUNCTION__, "music");
40   return ly_prob_set_property_x (mus, sym, val);
41 }
42
43
44 /* todo:  property args */
45 LY_DEFINE (ly_make_music, "ly:make-music",
46            1, 0, 0, (SCM props),
47            "Make a C++ Music object, initialize with\n"
48            "@var{props}. \n\n"
49            ""
50            "This function is for internal use, and is only called by "
51            "@code{make-music}, which is the preferred interface "
52            "for creating music objects. ")
53 {
54   Music *ms = new Music (props);
55   return ms->unprotect ();
56 }
57
58 LY_DEFINE (ly_music_p, "ly:music?",
59            1, 0, 0, (SCM obj),
60            "Type predicate")
61 {
62   return scm_from_bool (unsmob_music (obj));
63 }
64
65 /* todo: property args */
66 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
67            1, 0, 0, (SCM mus),
68            "Return an alist containing the mutable properties of @var{mus}.\n"
69            "The immutable properties are not available, since "
70            "they are constant and initialized by the "
71            "@code{make-music} function.\n")
72 {
73   Music *m = unsmob_music (mus);
74   SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "music");
75   return m->get_property_alist (true);
76 }
77
78 LY_DEFINE (ly_music_list_p, "ly:music-list?",
79            1, 0, 0, (SCM lst),
80            "Type predicate: return true if @var{lst} is a list "
81            "of music objects.")
82 {
83   if (scm_list_p (lst) == SCM_BOOL_T)
84     while (scm_is_pair (lst))
85       {
86         if (!unsmob_music (scm_car (lst)))
87           return SCM_BOOL_F;
88         lst = scm_cdr (lst);
89       }
90
91   return SCM_BOOL_T;
92 }
93
94 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
95            1, 0, 0, (SCM m),
96            "Copy @var{m} and all sub expressions of @var{m}")
97 {
98   SCM copy = m;
99   if (unsmob_music (m))
100     {
101       Music *mcopy = unsmob_music (m)->clone ();
102       copy = mcopy->unprotect ();
103     }
104   else if (scm_is_pair (m))
105     copy = scm_cons (ly_music_deep_copy (scm_car (m)),
106                      ly_music_deep_copy (scm_cdr (m)));
107   return copy;
108 }
109
110 LY_DEFINE (ly_music_transpose, "ly:music-transpose",
111            2, 0, 0, (SCM m, SCM p),
112            "Transpose @var{m} such that central C is mapped to @var{p}. "
113            "Return @var{m}.")
114 {
115   Music *sc = unsmob_music (m);
116   Pitch *sp = unsmob_pitch (p);
117   SCM_ASSERT_TYPE (sc, m, SCM_ARG1, __FUNCTION__, "music");
118   SCM_ASSERT_TYPE (sp, p, SCM_ARG2, __FUNCTION__, "pitch");
119
120   sc->transpose (*sp);
121   // SCM_UNDEFINED ?
122   return sc->self_scm ();
123 }
124
125 /*
126   TODO: should take moment factor?
127 */
128 LY_DEFINE (ly_music_compress, "ly:music-compress",
129            2, 0, 0, (SCM m, SCM factor),
130            "Compress music object @var{m} by moment @var{factor}.")
131 {
132   Music *sc = unsmob_music (m);
133
134   SCM_ASSERT_TYPE (sc, m, SCM_ARG1, __FUNCTION__, "music");
135   SCM_ASSERT_TYPE (unsmob_moment (factor), factor, SCM_ARG2, __FUNCTION__, "moment");
136
137   sc->compress (*unsmob_moment (factor));
138   return sc->self_scm ();
139 }
140
141 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
142            (SCM mus),
143            "Extract the duration field from @var{mus}, and return the length.")
144 {
145   Music *m = unsmob_music (mus);
146   SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "Music");
147
148   Duration *d = unsmob_duration (m->get_property ("duration"));
149   Moment len;
150
151   if (d)
152     len = d->get_length ();
153   else
154     programming_error ("music has no duration");
155   return len.smobbed_copy ();
156 }
157
158 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
159            (SCM mus, SCM fact),
160            "Compress @var{mus} by factor @var{fact}, which is a @code{Moment}.")
161 {
162   Music *m = unsmob_music (mus);
163   Moment *f = unsmob_moment (fact);
164   SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "Music");
165   SCM_ASSERT_TYPE (f, fact, SCM_ARG2, __FUNCTION__, "Moment");
166
167   Duration *d = unsmob_duration (m->get_property ("duration"));
168   if (d)
169     m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
170   return SCM_UNSPECIFIED;
171 }
172
173 /*
174   This is hairy, since the scale in a key-change event may contain
175   octaveless notes.
176
177
178   TODO: this should use ly:pitch.
179 */
180 LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist",
181            2, 0, 0, (SCM l, SCM pit),
182            "Make a new key alist of @var{l} transposed by pitch @var{pit}")
183 {
184   SCM newlist = SCM_EOL;
185   Pitch *p = unsmob_pitch (pit);
186
187   for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
188     {
189       SCM key = scm_caar (s);
190       SCM alter = scm_cdar (s);
191       if (scm_is_pair (key))
192         {
193           Pitch orig (scm_to_int (scm_car (key)),
194                       scm_to_int (scm_cdr (key)),
195                       scm_to_int (alter));
196
197           orig = orig.transposed (*p);
198
199           SCM key = scm_cons (scm_from_int (orig.get_octave ()),
200                               scm_from_int (orig.get_notename ()));
201
202           newlist = scm_cons (scm_cons (key, scm_from_int (orig.get_alteration ())),
203                               newlist);
204         }
205       else if (scm_is_number (key))
206         {
207           Pitch orig (0, scm_to_int (key), scm_to_int (alter));
208           orig = orig.transposed (*p);
209
210           key = scm_from_int (orig.get_notename ());
211           alter = scm_from_int (orig.get_alteration ());
212           newlist = scm_cons (scm_cons (key, alter), newlist);
213         }
214     }
215   return scm_reverse_x (newlist, SCM_EOL);
216 }
217