]> git.donarmstrong.com Git - lilypond.git/blob - lily/event.cc
string() -> to_string()
[lilypond.git] / lily / event.cc
1 /*
2   event.cc -- implement Event
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "event.hh"
10 #include "warn.hh"
11 #include "event.hh"
12   
13 Moment
14 Event::get_length () const
15 {
16   Duration *d = unsmob_duration (get_mus_property ("duration"));
17   if (!d)
18     {
19       Moment m ;
20       return m;
21     }
22   return d->get_length ();
23 }
24
25 void
26 Event::compress (Moment m)
27 {
28   Duration *d =  unsmob_duration (get_mus_property ("duration"));
29   if (d)
30     set_mus_property ("duration", d ->compressed (m.main_part_).smobbed_copy ());
31 }
32
33 void
34 Event::transpose (Pitch delta)
35 {
36   Pitch *p = unsmob_pitch (get_mus_property ("pitch"));
37   if (!p)
38     return ;
39
40   Pitch np = p->transposed (delta);
41   
42   if (abs (np.get_alteration ()) > 2)
43     {
44         warning (_f ("Transposition by %s makes alteration larger than two",
45           delta.to_string ()));
46     }
47
48   set_mus_property ("pitch", np.smobbed_copy ());
49 }
50
51 Pitch
52 Event::to_relative_octave (Pitch last)
53 {
54   Pitch *old_pit = unsmob_pitch (get_mus_property ("pitch"));
55   if (old_pit)
56     {
57       Pitch new_pit = *old_pit;
58       new_pit = new_pit.to_relative_octave (last);
59       set_mus_property ("pitch", new_pit.smobbed_copy ());
60   
61       return new_pit;
62     }
63   return last;
64 }
65   
66 Event::Event ()
67   : Music ()
68 {
69 }
70
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.")
75 {
76   Music* m =   unsmob_music(mus);
77   SCM_ASSERT_TYPE(m, mus, SCM_ARG1, __FUNCTION__, "Music");
78   
79   Duration *d = unsmob_duration (m->get_mus_property ("duration"));
80
81   Moment l ;
82   
83   if (d)
84     {
85       l = d->get_length ();  
86     }
87   else
88     programming_error("Music has no duration");
89   return l.smobbed_copy();
90   
91 }
92
93
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.")
97 {
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");
102   
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;
107 }
108
109
110
111 /*
112   This is hairy, since the scale in a key-change event may contain
113   octaveless notes.
114
115
116   TODO: this should use ly:pitch. 
117  */
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}")
121 {
122   SCM newlist = SCM_EOL;
123   Pitch *p = unsmob_pitch (pitch);
124   
125   for (SCM s = l; gh_pair_p (s); s = ly_cdr (s))
126     {
127       SCM key = ly_caar (s);
128       SCM alter = ly_cdar (s);
129       if (gh_pair_p (key))
130         {
131           Pitch orig (gh_scm2int (ly_car (key)),
132                       gh_scm2int (ly_cdr (key)),
133                       gh_scm2int (alter));
134
135           orig =orig.transposed (*p);
136
137           SCM key = gh_cons (scm_int2num (orig.get_octave ()),
138                              scm_int2num (orig.get_notename ()));
139
140           newlist = gh_cons (gh_cons (key, scm_int2num (orig.get_alteration ())),
141                              newlist);
142         }
143       else if (gh_number_p (key))
144         {
145           Pitch orig (0, gh_scm2int (key), gh_scm2int (alter));
146           orig = orig.transposed (*p);
147
148           key =scm_int2num (orig.get_notename ());
149           alter = scm_int2num (orig.get_alteration());
150           newlist = gh_cons (gh_cons (key, alter), newlist);
151         }
152     }
153   return scm_reverse_x (newlist, SCM_EOL);
154 }
155
156 void
157 Key_change_ev::transpose (Pitch p)
158 {
159   SCM pa = get_mus_property ("pitch-alist");
160
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 ());
165 }
166
167 bool
168 alist_equal_p (SCM a, SCM b)
169 {
170   for (SCM s = a;
171        gh_pair_p (s); s = ly_cdr (s))
172     {
173       SCM key = ly_caar (s);
174       SCM val = ly_cdar (s);
175       SCM l = scm_assoc (key, b);
176
177       if (l == SCM_BOOL_F
178           || !gh_equal_p ( ly_cdr (l), val))
179
180         return false;
181     }
182   return true;
183 }
184 ADD_MUSIC (Key_change_ev);