2 music-scheme.cc -- implement Music bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
14 LY_DEFINE (ly_music_length, "ly:music-length",
16 "Get the length of music expression @var{mus}, and return as a @code{Moment} object.")
18 Music *sc = unsmob_music (mus);
19 SCM_ASSERT_TYPE (sc, mus, SCM_ARG1, __FUNCTION__, "music");
20 return sc->get_length ().smobbed_copy ();
23 LY_DEFINE (ly_music_property,
24 "ly:music-property", 2, 0, 0, (SCM mus, SCM sym),
25 "Get the property @var{sym} of music expression @var{mus}.\n"
26 "If @var{sym} is undefined, return @code{' ()}.\n")
28 Music *sc = unsmob_music (mus);
29 SCM_ASSERT_TYPE (sc, mus, SCM_ARG1, __FUNCTION__, "music");
30 SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");
32 return sc->internal_get_property (sym);
35 LY_DEFINE (ly_music_set_property, "ly:music-set-property!",
36 3, 0, 0, (SCM mus, SCM sym, SCM val),
37 "Set property @var{sym} in music expression @var{mus} to @var{val}.")
39 Music *sc = unsmob_music (mus);
40 SCM_ASSERT_TYPE (sc, mus, SCM_ARG1, __FUNCTION__, "music");
41 SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");
43 bool ok = type_check_assignment (sym, val, ly_symbol2scm ("music-type?"));
45 sc->internal_set_property (sym, val);
47 return SCM_UNSPECIFIED;
50 LY_DEFINE (ly_music_name, "ly:music-name",
52 "Return the name of @var{music}.")
54 Music *m = unsmob_music (mus);
55 SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "music");
57 char const *nm = classname (m);
58 return scm_makfrom0str (nm);
61 /* todo: property args */
62 LY_DEFINE (ly_make_music, "ly:make-music",
64 "Make a C++ Music object, initialize with\n"
67 "This function is for internal use, and is only called by "
68 "@code{make-music}, which is the preferred interface "
69 "for creating music objects. ")
71 Music *ms = new Music (props);
72 return ms->unprotect ();
75 /* todo: property args */
76 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
78 "Return an alist containing the mutable properties of @var{mus}.\n"
79 "The immutable properties are not available, since "
80 "they are constant and initialized by the "
81 "@code{make-music} function.\n")
83 Music *m = unsmob_music (mus);
84 SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "music");
85 return m->get_property_alist (true);
88 LY_DEFINE (ly_music_list_p, "ly:music-list?",
90 "Type predicate: return true if @var{lst} is a list "
93 if (scm_list_p (lst) == SCM_BOOL_T)
94 while (scm_is_pair (lst))
96 if (!unsmob_music (scm_car (lst)))
104 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
106 "Copy @var{m} and all sub expressions of @var{m}")
109 if (unsmob_music (m))
111 Music *mcopy = unsmob_music (m)->clone ();
112 copy = mcopy->unprotect ();
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)));
120 LY_DEFINE (ly_music_transpose, "ly:music-transpose",
121 2, 0, 0, (SCM m, SCM p),
122 "Transpose @var{m} such that central C is mapped to @var{p}. "
125 Music *sc = unsmob_music (m);
126 Pitch *sp = unsmob_pitch (p);
127 SCM_ASSERT_TYPE (sc, m, SCM_ARG1, __FUNCTION__, "music");
128 SCM_ASSERT_TYPE (sp, p, SCM_ARG2, __FUNCTION__, "pitch");
132 return sc->self_scm ();
136 TODO: should take moment factor?
138 LY_DEFINE (ly_music_compress, "ly:music-compress",
139 2, 0, 0, (SCM m, SCM factor),
140 "Compress music object @var{m} by moment @var{factor}.")
142 Music *sc = unsmob_music (m);
144 SCM_ASSERT_TYPE (sc, m, SCM_ARG1, __FUNCTION__, "music");
145 SCM_ASSERT_TYPE (unsmob_moment (factor), factor, SCM_ARG2, __FUNCTION__, "moment");
147 sc->compress (*unsmob_moment (factor));
148 return sc->self_scm ();
151 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
153 "Extract the duration field from @var{mus}, and return the length.")
155 Music *m = unsmob_music (mus);
156 SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "Music");
158 Duration *d = unsmob_duration (m->get_property ("duration"));
162 len = d->get_length ();
164 programming_error ("music has no duration");
165 return len.smobbed_copy ();
168 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0, 0,
170 "Compress @var{mus} by factor @var{fact}, which is a @code{Moment}.")
172 Music *m = unsmob_music (mus);
173 Moment *f = unsmob_moment (fact);
174 SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "Music");
175 SCM_ASSERT_TYPE (f, fact, SCM_ARG2, __FUNCTION__, "Moment");
177 Duration *d = unsmob_duration (m->get_property ("duration"));
179 m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
180 return SCM_UNSPECIFIED;
184 This is hairy, since the scale in a key-change event may contain
188 TODO: this should use ly:pitch.
190 LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist",
191 2, 0, 0, (SCM l, SCM pit),
192 "Make a new key alist of @var{l} transposed by pitch @var{pit}")
194 SCM newlist = SCM_EOL;
195 Pitch *p = unsmob_pitch (pit);
197 for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
199 SCM key = scm_caar (s);
200 SCM alter = scm_cdar (s);
201 if (scm_is_pair (key))
203 Pitch orig (scm_to_int (scm_car (key)),
204 scm_to_int (scm_cdr (key)),
207 orig = orig.transposed (*p);
209 SCM key = scm_cons (scm_from_int (orig.get_octave ()),
210 scm_from_int (orig.get_notename ()));
212 newlist = scm_cons (scm_cons (key, scm_from_int (orig.get_alteration ())),
215 else if (scm_is_number (key))
217 Pitch orig (0, scm_to_int (key), scm_to_int (alter));
218 orig = orig.transposed (*p);
220 key = scm_from_int (orig.get_notename ());
221 alter = scm_from_int (orig.get_alteration ());
222 newlist = scm_cons (scm_cons (key, alter), newlist);
225 return scm_reverse_x (newlist, SCM_EOL);