]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-scheme.cc
Merge branch 'lilypond/translation' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[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--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "music.hh"
10
11 #include "duration.hh"
12 #include "warn.hh"
13
14 LY_DEFINE (ly_music_length, "ly:music-length",
15            1, 0, 0, (SCM mus),
16            "Get the length of music expression @var{mus} and return"
17            " it as a @code{Moment} object.")
18 {
19   LY_ASSERT_TYPE (unsmob_music, mus, 1);
20   Music *sc = unsmob_music (mus);
21   return sc->get_length ().smobbed_copy ();
22 }
23
24 LY_DEFINE (ly_music_property, "ly:music-property",
25            2, 1, 0, (SCM mus, SCM sym, SCM val),
26            "Return the value for property @var{sym} of music expression"
27            " @var{mus}.  If no value is found, return @var{val} or"
28            " @code{'()} if @var{val} is not specified.")
29 {
30   LY_ASSERT_TYPE (unsmob_music, mus, 1);
31   return ly_prob_property (mus, sym, val);
32 }
33
34 LY_DEFINE (ly_music_set_property_x, "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"
37            " @var{val}.")
38 {
39   LY_ASSERT_TYPE (unsmob_music, mus, 1);
40
41   return ly_prob_set_property_x (mus, sym, val);
42 }
43
44
45 /* todo:  property args */
46 LY_DEFINE (ly_make_music, "ly:make-music",
47            1, 0, 0, (SCM props),
48            "Make a C++ @code{Music} object and initialize it with"
49            " @var{props}.\n"
50            "\n"
51            "This function is for internal use and is only called by"
52            " @code{make-music}, which is the preferred interface"
53            " for creating music objects.")
54 {
55   Music *ms = new Music (props);
56   return ms->unprotect ();
57 }
58
59 LY_DEFINE (ly_music_p, "ly:music?",
60            1, 0, 0, (SCM obj),
61            "Is @var{obj} a Music object?")
62 {
63   return scm_from_bool (unsmob_music (obj));
64 }
65
66 /* todo: property args */
67 LY_DEFINE (ly_music_mutable_properties, "ly:music-mutable-properties",
68            1, 0, 0, (SCM mus),
69            "Return an alist containing the mutable properties of @var{mus}."
70            "  The immutable properties are not available, since they are"
71            " constant and initialized by the @code{make-music} function.")
72 {
73   LY_ASSERT_TYPE (unsmob_music, mus, 1);
74   Music *m = unsmob_music (mus);
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            "Is @var{lst} a list of music objects?")
81 {
82   if (scm_list_p (lst) == SCM_BOOL_T)
83     while (scm_is_pair (lst))
84       {
85         if (!unsmob_music (scm_car (lst)))
86           return SCM_BOOL_F;
87         lst = scm_cdr (lst);
88       }
89
90   return SCM_BOOL_T;
91 }
92
93 LY_DEFINE (ly_music_deep_copy, "ly:music-deep-copy",
94            1, 0, 0, (SCM m),
95            "Copy @var{m} and all sub expressions of@tie{}@var{m}.")
96 {
97   SCM copy = m;
98   if (unsmob_music (m))
99     {
100       Music *mcopy = unsmob_music (m)->clone ();
101       copy = mcopy->unprotect ();
102     }
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)));
106   return copy;
107 }
108
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}.")
113 {
114   LY_ASSERT_TYPE (unsmob_music, m, 1);
115   LY_ASSERT_SMOB (Pitch, p, 2);
116
117   Music *sc = unsmob_music (m);
118   Pitch *sp = unsmob_pitch (p);
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@tie{}@var{m} by moment @var{factor}.")
131 {
132   LY_ASSERT_TYPE (unsmob_music, m, 1);
133   LY_ASSERT_TYPE (unsmob_moment, factor, 2);
134
135   Music *sc = unsmob_music (m);
136   sc->compress (*unsmob_moment (factor));
137   return sc->self_scm ();
138 }
139
140 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0, 0,
141            (SCM mus),
142            "Extract the duration field from @var{mus} and return the"
143            " length.")
144 {
145   LY_ASSERT_TYPE (unsmob_music, mus, 1);
146   Music *m = unsmob_music (mus);
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"
161            " @code{Moment}.")
162 {
163   LY_ASSERT_TYPE (unsmob_music, mus, 1);
164   LY_ASSERT_SMOB (Moment, fact, 2);
165   
166   Music *m = unsmob_music (mus);
167   Moment *f = unsmob_moment (fact);
168
169   Duration *d = unsmob_duration (m->get_property ("duration"));
170   if (d)
171     m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
172   return SCM_UNSPECIFIED;
173 }
174
175 /*
176   This is hairy, since the scale in a key-change event may contain
177   octaveless notes.
178
179
180   TODO: this should use ly:pitch.
181 */
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"
185            " pitch @var{pit}.")
186 {
187   SCM newlist = SCM_EOL;
188   Pitch *p = unsmob_pitch (pit);
189
190   for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
191     {
192       SCM key = scm_caar (s);
193       SCM alter = scm_cdar (s);
194       if (scm_is_pair (key))
195         {
196           Pitch orig (scm_to_int (scm_car (key)),
197                       scm_to_int (scm_cdr (key)),
198                       ly_scm2rational (alter));
199
200           orig = orig.transposed (*p);
201
202           SCM key = scm_cons (scm_from_int (orig.get_octave ()),
203                               scm_from_int (orig.get_notename ()));
204
205           newlist = scm_cons (scm_cons (key, ly_rational2scm (orig.get_alteration ())),
206                               newlist);
207         }
208       else if (scm_is_number (key))
209         {
210           Pitch orig (0, scm_to_int (key), ly_scm2rational (alter));
211           orig = orig.transposed (*p);
212
213           key = scm_from_int (orig.get_notename ());
214           alter = ly_rational2scm (orig.get_alteration ());
215           newlist = scm_cons (scm_cons (key, alter), newlist);
216         }
217     }
218   return scm_reverse_x (newlist, SCM_EOL);
219 }
220