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