* input/regression/ambitus.ly: move file.
[lilypond.git] / lily / event.cc
blob199c85eb454d5f442eba8969817fde63f1586e1e
1 /*
2 event.cc -- implement Event
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2003 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_mus_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_mus_property ("duration"));
29 if (d)
30 set_mus_property ("duration", d ->compressed (m.main_part_).smobbed_copy ());
33 void
34 Event::transpose (Pitch delta)
36 Pitch *p = unsmob_pitch (get_mus_property ("pitch"));
37 if (!p)
38 return ;
40 Pitch np = p->transposed (delta);
42 if (abs (np.get_alteration ()) > 2)
44 warning (_f ("Transposition by %s makes alteration larger than two",
45 delta.string ()));
48 set_mus_property ("pitch", np.smobbed_copy ());
51 Pitch
52 Event::to_relative_octave (Pitch last)
54 Pitch *old_pit = unsmob_pitch (get_mus_property ("pitch"));
55 if (old_pit)
57 Pitch new_pit = *old_pit;
58 new_pit = new_pit.to_relative_octave (last);
59 set_mus_property ("pitch", new_pit.smobbed_copy ());
61 return new_pit;
63 return last;
66 Event::Event ()
67 : Music ()
71 ADD_MUSIC(Event);
72 LY_DEFINE(music_duration_length, "music-duration-length", 1, 0,0,
73 (SCM mus),
74 "Extract the duration field from @var{mus}, and return the length.")
76 Music* m = unsmob_music(mus);
77 SCM_ASSERT_TYPE(m, mus, SCM_ARG1, __FUNCTION__, "Music");
79 Duration *d = unsmob_duration (m->get_mus_property ("duration"));
81 Moment l ;
83 if (d)
85 l = d->get_length ();
87 else
88 programming_error("Music has no duration");
89 return l.smobbed_copy();
94 LY_DEFINE(music_duration_compress, "ly:music-duration-compress", 2, 0,0,
95 (SCM mus, SCM factor),
96 "Extract the duration field from @var{mus}, and compress it.")
98 Music* m = unsmob_music(mus);
99 Moment * f = unsmob_moment (factor);
100 SCM_ASSERT_TYPE(m, mus, SCM_ARG1, __FUNCTION__, "Music");
101 SCM_ASSERT_TYPE(f, factor, SCM_ARG2, __FUNCTION__, "Moment");
103 Duration *d = unsmob_duration (m->get_mus_property ("duration"));
104 if (d)
105 m->set_mus_property ("duration", d->compressed (f->main_part_).smobbed_copy());
106 return SCM_UNSPECIFIED;
112 This is hairy, since the scale in a key-change event may contain
113 octaveless notes.
116 TODO: this should use ly:pitch.
118 LY_DEFINE(transpose_key_alist, "ly:transpose-key-alist",
119 2, 0,0, (SCM l, SCM pitch),
120 "Make a new key alist of @var{l} transposed by pitch @var{pitch}")
122 SCM newlist = SCM_EOL;
123 Pitch *p = unsmob_pitch (pitch);
125 for (SCM s = l; gh_pair_p (s); s = ly_cdr (s))
127 SCM key = ly_caar (s);
128 SCM alter = ly_cdar (s);
129 if (gh_pair_p (key))
131 Pitch orig (gh_scm2int (ly_car (key)),
132 gh_scm2int (ly_cdr (key)),
133 gh_scm2int (alter));
135 orig =orig.transposed (*p);
137 SCM key = gh_cons (scm_int2num (orig.get_octave ()),
138 scm_int2num (orig.get_notename ()));
140 newlist = gh_cons (gh_cons (key, scm_int2num (orig.get_alteration ())),
141 newlist);
143 else if (gh_number_p (key))
145 Pitch orig (0, gh_scm2int (key), gh_scm2int (alter));
146 orig = orig.transposed (*p);
148 key =scm_int2num (orig.get_notename ());
149 alter = scm_int2num (orig.get_alteration());
150 newlist = gh_cons (gh_cons (key, alter), newlist);
153 return scm_reverse_x (newlist, SCM_EOL);
156 void
157 Key_change_ev::transpose (Pitch p)
159 SCM pa = get_mus_property ("pitch-alist");
161 set_mus_property ("pitch-alist", transpose_key_alist (pa, p.smobbed_copy()));
162 Pitch tonic = *unsmob_pitch (get_mus_property ("tonic"));
163 set_mus_property ("tonic",
164 tonic.smobbed_copy ());
167 bool
168 alist_equal_p (SCM a, SCM b)
170 for (SCM s = a;
171 gh_pair_p (s); s = ly_cdr (s))
173 SCM key = ly_caar (s);
174 SCM val = ly_cdar (s);
175 SCM l = scm_assoc (key, b);
177 if (l == SCM_BOOL_F
178 || !gh_equal_p ( ly_cdr (l), val))
180 return false;
182 return true;
184 ADD_MUSIC (Key_change_ev);