2 music-scheme.cc -- implement Music bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
15 LY_DEFINE (ly_music_length, "ly:music-length",
17 "Get the length of music expression @var{mus}, and return as a @code{Moment} object.")
19 Music *sc = unsmob_music (mus);
20 SCM_ASSERT_TYPE (sc, mus, SCM_ARG1, __FUNCTION__, "music");
21 return sc->get_length ().smobbed_copy ();
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")
29 Music *sc = unsmob_music (mus);
30 SCM_ASSERT_TYPE (sc, mus, SCM_ARG1, __FUNCTION__, "music");
31 return ly_prob_property (mus,sym,dfault);
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}.")
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);
44 /* todo: property args */
45 LY_DEFINE (ly_make_music, "ly:make-music",
47 "Make a C++ Music object, initialize with\n"
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. ")
54 Music *ms = new Music (props);
55 return ms->unprotect ();
58 LY_DEFINE (ly_music_p, "ly:music?",
62 return scm_from_bool (unsmob_music (obj));
65 /* todo: property args */
66 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
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")
73 Music *m = unsmob_music (mus);
74 SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "music");
75 return m->get_property_alist (true);
78 LY_DEFINE (ly_music_list_p, "ly:music-list?",
80 "Type predicate: return true if @var{lst} is a list "
83 if (scm_list_p (lst) == SCM_BOOL_T)
84 while (scm_is_pair (lst))
86 if (!unsmob_music (scm_car (lst)))
94 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
96 "Copy @var{m} and all sub expressions of @var{m}")
101 Music *mcopy = unsmob_music (m)->clone ();
102 copy = mcopy->unprotect ();
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)));
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}. "
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");
122 return sc->self_scm ();
126 TODO: should take moment factor?
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}.")
132 Music *sc = unsmob_music (m);
134 SCM_ASSERT_TYPE (sc, m, SCM_ARG1, __FUNCTION__, "music");
135 SCM_ASSERT_TYPE (unsmob_moment (factor), factor, SCM_ARG2, __FUNCTION__, "moment");
137 sc->compress (*unsmob_moment (factor));
138 return sc->self_scm ();
141 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
143 "Extract the duration field from @var{mus}, and return the length.")
145 Music *m = unsmob_music (mus);
146 SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "Music");
148 Duration *d = unsmob_duration (m->get_property ("duration"));
152 len = d->get_length ();
154 programming_error ("music has no duration");
155 return len.smobbed_copy ();
158 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
160 "Compress @var{mus} by factor @var{fact}, which is a @code{Moment}.")
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");
167 Duration *d = unsmob_duration (m->get_property ("duration"));
169 m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
170 return SCM_UNSPECIFIED;
174 This is hairy, since the scale in a key-change event may contain
178 TODO: this should use ly:pitch.
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}")
184 SCM newlist = SCM_EOL;
185 Pitch *p = unsmob_pitch (pit);
187 for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
189 SCM key = scm_caar (s);
190 SCM alter = scm_cdar (s);
191 if (scm_is_pair (key))
193 Pitch orig (scm_to_int (scm_car (key)),
194 scm_to_int (scm_cdr (key)),
197 orig = orig.transposed (*p);
199 SCM key = scm_cons (scm_from_int (orig.get_octave ()),
200 scm_from_int (orig.get_notename ()));
202 newlist = scm_cons (scm_cons (key, scm_from_int (orig.get_alteration ())),
205 else if (scm_is_number (key))
207 Pitch orig (0, scm_to_int (key), scm_to_int (alter));
208 orig = orig.transposed (*p);
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);
215 return scm_reverse_x (newlist, SCM_EOL);