2 music-scheme.cc -- implement Music bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "duration.hh"
14 LY_DEFINE (ly_music_length
, "ly:music-length",
16 "Get the length of music expression @var{mus} and return"
17 " it as a @code{Moment} object.")
19 LY_ASSERT_TYPE (unsmob_music
, mus
, 1);
20 Music
*sc
= unsmob_music (mus
);
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}."
27 " If @var{sym} is undefined, return @code{'()}.")
29 LY_ASSERT_TYPE (unsmob_music
, mus
, 1);
30 return ly_prob_property (mus
, sym
, dfault
);
33 LY_DEFINE (ly_music_set_property_x
, "ly:music-set-property!",
34 3, 0, 0, (SCM mus
, SCM sym
, SCM val
),
35 "Set property @var{sym} in music expression @var{mus} to"
38 LY_ASSERT_TYPE (unsmob_music
, mus
, 1);
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++ @code{Music} object and initialize it with"
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}."
69 " The immutable properties are not available, since they are"
70 " constant and initialized by the @code{make-music} function.")
72 LY_ASSERT_TYPE (unsmob_music
, mus
, 1);
73 Music
*m
= unsmob_music (mus
);
74 return m
->get_property_alist (true);
77 LY_DEFINE (ly_music_list_p
, "ly:music-list?",
79 "Type predicate: Return true if @var{lst} is a list"
82 if (scm_list_p (lst
) == SCM_BOOL_T
)
83 while (scm_is_pair (lst
))
85 if (!unsmob_music (scm_car (lst
)))
93 LY_DEFINE (ly_music_deep_copy
, "ly:music-deep-copy",
95 "Copy @var{m} and all sub expressions of@tie{}@var{m}.")
100 Music
*mcopy
= unsmob_music (m
)->clone ();
101 copy
= mcopy
->unprotect ();
103 else if (scm_is_pair (m
))
104 copy
= scm_cons (ly_music_deep_copy (scm_car (m
)),
105 ly_music_deep_copy (scm_cdr (m
)));
109 LY_DEFINE (ly_music_transpose
, "ly:music-transpose",
110 2, 0, 0, (SCM m
, SCM p
),
111 "Transpose @var{m} such that central@tie{}C is mapped"
112 " to@tie{}@var{p}. Return@tie{}@var{m}.")
114 LY_ASSERT_TYPE (unsmob_music
, m
, 1);
115 LY_ASSERT_SMOB (Pitch
, p
, 2);
117 Music
*sc
= unsmob_music (m
);
118 Pitch
*sp
= unsmob_pitch (p
);
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@tie{}@var{m} by moment @var{factor}.")
132 LY_ASSERT_TYPE (unsmob_music
, m
, 1);
133 LY_ASSERT_TYPE (unsmob_moment
, factor
, 2);
135 Music
*sc
= unsmob_music (m
);
136 sc
->compress (*unsmob_moment (factor
));
137 return sc
->self_scm ();
140 LY_DEFINE (ly_music_duration_length
, "ly:music-duration-length", 1, 0, 0,
142 "Extract the duration field from @var{mus} and return the"
145 LY_ASSERT_TYPE (unsmob_music
, mus
, 1);
146 Music
*m
= unsmob_music (mus
);
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"
163 LY_ASSERT_TYPE (unsmob_music
, mus
, 1);
164 LY_ASSERT_SMOB (Moment
, fact
, 2);
166 Music
*m
= unsmob_music (mus
);
167 Moment
*f
= unsmob_moment (fact
);
169 Duration
*d
= unsmob_duration (m
->get_property ("duration"));
171 m
->set_property ("duration", d
->compressed (f
->main_part_
).smobbed_copy ());
172 return SCM_UNSPECIFIED
;
176 This is hairy, since the scale in a key-change event may contain
180 TODO: this should use ly:pitch.
182 LY_DEFINE (ly_transpose_key_alist
, "ly:transpose-key-alist",
183 2, 0, 0, (SCM l
, SCM pit
),
184 "Make a new key alist of@tie{}@var{l} transposed by"
187 SCM newlist
= SCM_EOL
;
188 Pitch
*p
= unsmob_pitch (pit
);
190 for (SCM s
= l
; scm_is_pair (s
); s
= scm_cdr (s
))
192 SCM key
= scm_caar (s
);
193 SCM alter
= scm_cdar (s
);
194 if (scm_is_pair (key
))
196 Pitch
orig (scm_to_int (scm_car (key
)),
197 scm_to_int (scm_cdr (key
)),
198 ly_scm2rational (alter
));
200 orig
= orig
.transposed (*p
);
202 SCM key
= scm_cons (scm_from_int (orig
.get_octave ()),
203 scm_from_int (orig
.get_notename ()));
205 newlist
= scm_cons (scm_cons (key
, ly_rational2scm (orig
.get_alteration ())),
208 else if (scm_is_number (key
))
210 Pitch
orig (0, scm_to_int (key
), ly_scm2rational (alter
));
211 orig
= orig
.transposed (*p
);
213 key
= scm_from_int (orig
.get_notename ());
214 alter
= ly_rational2scm (orig
.get_alteration ());
215 newlist
= scm_cons (scm_cons (key
, alter
), newlist
);
218 return scm_reverse_x (newlist
, SCM_EOL
);