]> git.donarmstrong.com Git - lilypond.git/blob - lily/event.cc
* configure.in: Test for and accept lmodern if EC fonts not found.
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "event.hh"
10
11 #include "warn.hh"
12
13 Moment
14 Event::get_length () const
15 {
16   Duration *d = unsmob_duration (get_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_property ("duration"));
29   if (d)
30     set_property ("duration", d ->compressed (m.main_part_).smobbed_copy ());
31 }
32
33
34 Pitch
35 Event::to_relative_octave (Pitch last)
36 {
37   Pitch *old_pit = unsmob_pitch (get_property ("pitch"));
38   if (old_pit)
39     {
40       Pitch new_pit = *old_pit;
41       new_pit = new_pit.to_relative_octave (last);
42
43       SCM check = get_property ("absolute-octave");
44       if (scm_is_number (check) &&
45           new_pit.get_octave () != scm_to_int (check))
46         {
47           Pitch expected_pit (scm_to_int (check),
48                               new_pit.get_notename (),
49                               new_pit.get_alteration ());
50           origin ()->warning (_f ("octave check failed; expected %s, found: %s",
51                                   expected_pit.to_string (),
52                                   new_pit.to_string ()));
53           new_pit = expected_pit;
54         }
55       
56       set_property ("pitch", new_pit.smobbed_copy ());
57   
58       return new_pit;
59     }
60   return last;
61 }
62   
63 Event::Event ()
64   : Music ()
65 {
66 }
67
68 ADD_MUSIC (Event);
69
70 LY_DEFINE (ly_music_duration_length, "ly:music-duration-length", 1, 0,0,
71           (SCM mus),
72           "Extract the duration field from @var{mus}, and return the length.")
73 {
74   Music* m =   unsmob_music (mus);
75   SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "Music");
76   
77   Duration *d = unsmob_duration (m->get_property ("duration"));
78
79   Moment l ;
80   
81   if (d)
82     {
83       l = d->get_length ();  
84     }
85   else
86     programming_error ("Music has no duration");
87   return l.smobbed_copy ();
88   
89 }
90
91
92 LY_DEFINE (ly_music_duration_compress, "ly:music-duration-compress", 2, 0,0,
93           (SCM mus, SCM fact),
94           "Compress @var{mus} by factor @var{fact}, which is a @code{Moment}.")
95 {
96   Music* m =   unsmob_music (mus);
97   Moment * f = unsmob_moment (fact);
98   SCM_ASSERT_TYPE (m, mus, SCM_ARG1, __FUNCTION__, "Music");
99   SCM_ASSERT_TYPE (f, fact, SCM_ARG2, __FUNCTION__, "Moment");
100   
101   Duration *d = unsmob_duration (m->get_property ("duration"));
102   if (d)
103     m->set_property ("duration", d->compressed (f->main_part_).smobbed_copy ());
104   return SCM_UNSPECIFIED;
105 }
106
107
108
109 /*
110   This is hairy, since the scale in a key-change event may contain
111   octaveless notes.
112
113
114   TODO: this should use ly:pitch. 
115  */
116 LY_DEFINE (ly_transpose_key_alist, "ly:transpose-key-alist",
117           2, 0, 0, (SCM l, SCM pit),
118           "Make a new key alist of @var{l} transposed by pitch @var{pit}")
119 {
120   SCM newlist = SCM_EOL;
121   Pitch *p = unsmob_pitch (pit);
122   
123   for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
124     {
125       SCM key = scm_caar (s);
126       SCM alter = scm_cdar (s);
127       if (scm_is_pair (key))
128         {
129           Pitch orig (scm_to_int (scm_car (key)),
130                       scm_to_int (scm_cdr (key)),
131                       scm_to_int (alter));
132
133           orig =orig.transposed (*p);
134
135           SCM key = scm_cons (scm_int2num (orig.get_octave ()),
136                              scm_int2num (orig.get_notename ()));
137
138           newlist = scm_cons (scm_cons (key, scm_int2num (orig.get_alteration ())),
139                              newlist);
140         }
141       else if (scm_is_number (key))
142         {
143           Pitch orig (0, scm_to_int (key), scm_to_int (alter));
144           orig = orig.transposed (*p);
145
146           key =scm_int2num (orig.get_notename ());
147           alter = scm_int2num (orig.get_alteration ());
148           newlist = scm_cons (scm_cons (key, alter), newlist);
149         }
150     }
151   return scm_reverse_x (newlist, SCM_EOL);
152 }
153
154 void
155 Key_change_ev::transpose (Pitch p)
156 {
157   SCM pa = get_property ("pitch-alist");
158
159   set_property ("pitch-alist", ly_transpose_key_alist (pa, p.smobbed_copy ()));
160   Pitch tonic = *unsmob_pitch (get_property ("tonic"));
161   set_property ("tonic",
162                     tonic.smobbed_copy ());
163 }
164
165 bool
166 alist_equal_p (SCM a, SCM b)
167 {
168   for (SCM s = a;
169        scm_is_pair (s); s = scm_cdr (s))
170     {
171       SCM key = scm_caar (s);
172       SCM val = scm_cdar (s);
173       SCM l = scm_assoc (key, b);
174
175       if (l == SCM_BOOL_F
176           || !ly_c_equal_p ( scm_cdr (l), val))
177
178         return false;
179     }
180   return true;
181 }
182 ADD_MUSIC (Key_change_ev);