]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
* lily/accidental-engraver.cc: formatting fixes.
[lilypond.git] / lily / beam-engraver.cc
1 /*
2   beam-engraver.cc -- implement Beam_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "engraver.hh"
10 #include "beam.hh"
11 #include "stem.hh"
12 #include "warn.hh"
13 #include "beaming.hh"
14 #include "score-engraver.hh"
15 #include "rest.hh"
16 #include "drul-array.hh"
17 #include "item.hh"
18 #include "spanner.hh"
19 #include "context.hh"
20 #include "duration.hh"
21
22 #include "translator.icc"
23
24 class Beam_engraver : public Engraver
25 {
26 public:
27   DECLARE_ACKNOWLEDGER (stem);
28   DECLARE_ACKNOWLEDGER (rest);
29 protected:
30   Music *start_ev_;
31
32   Spanner *finished_beam_;
33   Spanner *beam_;
34   Music *prev_start_ev_;
35
36   Music *now_stop_ev_;
37
38   Beaming_info_list *beam_info_;
39   Beaming_info_list *finished_beam_info_;
40
41   /// location  within measure where beam started.
42   Moment beam_start_location_;
43
44   /// moment (global time) where beam started.
45   Moment beam_start_mom_;
46
47   bool subdivide_beams_;
48   Moment beat_length_;
49
50   void typeset_beam ();
51   void set_melisma (bool);
52
53   Moment last_stem_added_at_;
54   void stop_translation_timestep ();
55   void start_translation_timestep ();
56   virtual void finalize ();
57
58   virtual bool try_music (Music *);
59   void process_music ();
60
61   virtual bool valid_start_point ();
62   virtual bool valid_end_point ();
63
64 public:
65   TRANSLATOR_DECLARATIONS (Beam_engraver);
66 };
67
68 /*
69   Hmm. this isn't necessary, since grace beams and normal beams are
70   always nested.
71 */
72 bool
73 Beam_engraver::valid_start_point ()
74 {
75   Moment n = now_mom ();
76
77   return n.grace_part_ == Rational (0);
78 }
79
80 bool
81 Beam_engraver::valid_end_point ()
82 {
83   return valid_start_point ();
84 }
85
86 Beam_engraver::Beam_engraver ()
87 {
88   beam_ = 0;
89   finished_beam_ = 0;
90   finished_beam_info_ = 0;
91   beam_info_ = 0;
92   now_stop_ev_ = 0;
93   start_ev_ = 0;
94   prev_start_ev_ = 0;
95 }
96
97 bool
98 Beam_engraver::try_music (Music *m)
99 {
100   if (m->is_mus_type ("beam-event"))
101     {
102       Direction d = to_dir (m->get_property ("span-direction"));
103       if (d == START && !valid_start_point ())
104         return false;
105       if (d == STOP && !valid_end_point ())
106         return false;
107
108       if (d == START)
109         {
110           start_ev_ = m;
111         }
112       else if (d == STOP)
113         {
114           now_stop_ev_ = m;
115         }
116       return true;
117     }
118   return false;
119 }
120
121 void
122 Beam_engraver::set_melisma (bool ml)
123 {
124   SCM b = get_property ("autoBeaming");
125   if (!to_boolean (b))
126     context ()->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T : SCM_BOOL_F);
127 }
128
129 void
130 Beam_engraver::process_music ()
131 {
132   if (beam_ && !to_boolean (get_property ("allowBeamBreak")))
133     {
134       get_score_engraver ()->forbid_breaks ();
135     }
136
137   if (start_ev_)
138     {
139       if (beam_)
140         {
141           start_ev_->origin ()->warning (_ ("already have a beam"));
142           return;
143         }
144
145       set_melisma (true);
146       prev_start_ev_ = start_ev_;
147       beam_ = make_spanner ("Beam", start_ev_->self_scm ());
148       Moment mp (robust_scm2moment (get_property ("measurePosition"), Moment (0)));
149
150       beam_start_location_ = mp;
151       beam_start_mom_ = now_mom ();
152
153       beam_info_ = new Beaming_info_list;
154
155       /* urg, must copy to Auto_beam_engraver too */
156     }
157 }
158
159 void
160 Beam_engraver::typeset_beam ()
161 {
162   if (finished_beam_)
163     {
164       finished_beam_info_->beamify (beat_length_, subdivide_beams_);
165       Beam::set_beaming (finished_beam_, finished_beam_info_);
166
167       delete finished_beam_info_;
168       finished_beam_info_ = 0;
169       finished_beam_ = 0;
170     }
171 }
172
173 void
174 Beam_engraver::start_translation_timestep ()
175 {
176   start_ev_ = 0;
177
178   if (beam_)
179     {
180       set_melisma (true);
181
182       subdivide_beams_ = to_boolean (get_property ("subdivideBeams"));
183       beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
184     }
185 }
186
187 void
188 Beam_engraver::stop_translation_timestep ()
189 {
190   typeset_beam ();
191   if (now_stop_ev_)
192     {
193       finished_beam_ = beam_;
194       finished_beam_info_ = beam_info_;
195
196       now_stop_ev_ = 0;
197       beam_ = 0;
198       beam_info_ = 0;
199       typeset_beam ();
200       set_melisma (false);
201     }
202 }
203
204 void
205 Beam_engraver::finalize ()
206 {
207   typeset_beam ();
208   if (beam_)
209     {
210       prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
211
212       /*
213         we don't typeset it, (we used to, but it was commented
214         out. Reason unknown) */
215       beam_->suicide ();
216       delete beam_info_;
217     }
218 }
219
220 void
221 Beam_engraver::acknowledge_rest (Grob_info info)
222 {
223   if (beam_)
224     info.grob ()->add_offset_callback (Beam::rest_collision_callback_proc, Y_AXIS);
225 }
226
227 void
228 Beam_engraver::acknowledge_stem (Grob_info info)
229 {
230   if (beam_)
231     {
232       Moment now = now_mom ();
233
234       if (!valid_start_point ())
235         return;
236
237       Item *stem = dynamic_cast<Item *> (info.grob ());
238       if (Stem::get_beam (stem))
239         return;
240
241       Music *m = info.music_cause ();
242       if (!m->is_mus_type ("rhythmic-event"))
243         {
244           String s = _ ("stem must have Rhythmic structure");
245           if (info.music_cause ())
246             info.music_cause ()->origin ()->warning (s);
247           else
248             ::warning (s);
249
250           return;
251         }
252
253       last_stem_added_at_ = now;
254       int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
255       if (durlog <= 2)
256         {
257           m->origin ()->warning (_ ("stem doesn't fit in beam"));
258           prev_start_ev_->origin ()->warning (_ ("beam was started here"));
259           /*
260             don't return, since
261
262             [r4 c8] can just as well be modern notation.
263           */
264         }
265
266       stem->set_property ("duration-log",
267                           scm_from_int (durlog));
268       Moment stem_location = now - beam_start_mom_ + beam_start_location_;
269       beam_info_->add_stem (stem_location,
270                             max (durlog- 2, 0));
271       Beam::add_stem (beam_, stem);
272     }
273 }
274
275 ADD_ACKNOWLEDGER (Beam_engraver, stem);
276 ADD_ACKNOWLEDGER (Beam_engraver, rest);
277
278 ADD_TRANSLATOR (Beam_engraver,
279                 /* descr */ "Handles Beam events by engraving Beams.    If omitted, then notes will be "
280                 "printed with flags instead of beams.",
281                 /* creats*/ "Beam",
282                 /* accepts */ "beam-event",
283                 /* reads */ "beamMelismaBusy beatLength subdivideBeams",
284                 /* write */ "");
285
286 class Grace_beam_engraver : public Beam_engraver
287 {
288 public:
289   TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
290
291 protected:
292   virtual bool valid_start_point ();
293   virtual bool valid_end_point ();
294 };
295
296 Grace_beam_engraver::Grace_beam_engraver ()
297 {
298 }
299
300 bool
301 Grace_beam_engraver::valid_start_point ()
302 {
303   Moment n = now_mom ();
304
305   return n.grace_part_ != Rational (0);
306 }
307
308 bool
309 Grace_beam_engraver::valid_end_point ()
310 {
311   return beam_ && valid_start_point ();
312 }
313
314 ADD_ACKNOWLEDGER (Grace_beam_engraver, stem);
315 ADD_ACKNOWLEDGER (Grace_beam_engraver, rest);
316 ADD_TRANSLATOR (Grace_beam_engraver,
317                 /* descr */ "Handles Beam events by engraving Beams.  If omitted, then notes will "
318                 "be printed with flags instead of beams. Only engraves beams when we "
319                 " are at grace points in time. ",
320                 /* creats*/ "Beam",
321                 /* accepts */ "beam-event",
322                 /* reads */ "beamMelismaBusy beatLength allowBeamBreak subdivideBeams",
323                 /* write */ "");
324