(Which properties to
[lilypond.git] / lily / event.cc
blob004f0eb32eab1ebeb3764c27fe9bf0278754cffc
1 /*
2 event.cc -- implement Event
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include "event.hh"
10 #include "warn.hh"
11 #include "event.hh"
13 Moment
14 Event::get_length () const
16 Duration *d = unsmob_duration (get_property ("duration"));
17 if (!d)
19 Moment m ;
20 return m;
22 return d->get_length ();
25 void
26 Event::compress (Moment m)
28 Duration *d = unsmob_duration (get_property ("duration"));
29 if (d)
30 set_property ("duration", d ->compressed (m.main_part_).smobbed_copy ());
33 void
34 Event::transpose (Pitch delta)
37 TODO: should change music representation such that
38 _all_ pitch values are transposed automatically.
41 Pitch *p = unsmob_pitch (get_property ("pitch"));
42 if (!p)
43 return ;
45 Pitch np = p->transposed (delta);
47 if (abs (np.get_alteration ()) > DOUBLE_SHARP)
49 warning (_f ("Transposition by %s makes alteration larger than two",
50 delta.to_string ()));
53 set_property ("pitch", np.smobbed_copy ());
56 Pitch
57 Event::to_relative_octave (Pitch last)
59 Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
60 if (old_pit)
62 Pitch new_pit = *old_pit;
63 new_pit = new_pit.to_relative_octave (last);
65 SCM check = get_property ("absolute-octave");
66 if (gh_number_p (check) &&
67 new_pit.get_octave () != gh_scm2int (check))
69 String s =_("Failed octave check, got: ");
70 s += new_pit.to_string ();
71 new_pit = Pitch (gh_scm2int (check),
72 new_pit.get_notename (),
73 new_pit.get_alteration ());
75 s += " expected ";
76 s += new_pit.to_string ();
77 origin ()->warning (s);
80 set_property ("pitch", new_pit.smobbed_copy ());
82 return new_pit;
84 return last;
87 Event::Event ()
88 : Music ()
92 ADD_MUSIC (Event);
94 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0,0,
95 (SCM mus),
96 "Extract the duration field from @var{mus}, and return the length.")
98 Music* m = unsmob_music (mus);
99 SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "Music");
101 Duration *d = unsmob_duration (m->get_property ("duration"));
103 Moment l ;
105 if (d)
107 l = d->get_length ();
109 else
110 programming_error ("Music has no duration");
111 return l.smobbed_copy ();
116 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0,0,
117 (SCM mus, SCM fact),
118 "Compress @var{mus} by factor @var{fact}, which is a @code{Moment}.")
120 Music* m = unsmob_music (mus);
121 Moment * f = unsmob_moment (fact);
122 SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "Music");
123 SCM_ASSERT_TYPE (f, fact, SCM_ARG2, __FUNCTION__, "Moment");
125 Duration *d = unsmob_duration (m->get_property ("duration"));
126 if (d)
127 m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
128 return SCM_UNSPECIFIED;
134 This is hairy, since the scale in a key-change event may contain
135 octaveless notes.
138 TODO: this should use ly:pitch.
140 LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist",
141 2, 0, 0, (SCM l, SCM pit),
142 "Make a new key alist of @var{l} transposed by pitch @var{pit}")
144 SCM newlist = SCM_EOL;
145 Pitch *p = unsmob_pitch (pit);
147 for (SCM s = l; gh_pair_p (s); s = ly_cdr (s))
149 SCM key = ly_caar (s);
150 SCM alter = ly_cdar (s);
151 if (gh_pair_p (key))
153 Pitch orig (gh_scm2int (ly_car (key)),
154 gh_scm2int (ly_cdr (key)),
155 gh_scm2int (alter));
157 orig =orig.transposed (*p);
159 SCM key = gh_cons (scm_int2num (orig.get_octave ()),
160 scm_int2num (orig.get_notename ()));
162 newlist = gh_cons (gh_cons (key, scm_int2num (orig.get_alteration ())),
163 newlist);
165 else if (gh_number_p (key))
167 Pitch orig (0, gh_scm2int (key), gh_scm2int (alter));
168 orig = orig.transposed (*p);
170 key =scm_int2num (orig.get_notename ());
171 alter = scm_int2num (orig.get_alteration ());
172 newlist = gh_cons (gh_cons (key, alter), newlist);
175 return scm_reverse_x (newlist, SCM_EOL);
178 void
179 Key_change_ev::transpose (Pitch p)
181 SCM pa = get_property ("pitch-alist");
183 set_property ("pitch-alist", ly_transpose_key_alist (pa, p.smobbed_copy ()));
184 Pitch tonic = *unsmob_pitch (get_property ("tonic"));
185 set_property ("tonic",
186 tonic.smobbed_copy ());
189 bool
190 alist_equal_p (SCM a, SCM b)
192 for (SCM s = a;
193 gh_pair_p (s); s = ly_cdr (s))
195 SCM key = ly_caar (s);
196 SCM val = ly_cdar (s);
197 SCM l = scm_assoc (key, b);
199 if (l == SCM_BOOL_F
200 || !gh_equal_p ( ly_cdr (l), val))
202 return false;
204 return true;
206 ADD_MUSIC (Key_change_ev);