]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
* lily/lyric-phrasing-engraver.cc: move from
[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 b = get_property ("autoBeaming");
131   if (!to_boolean (b))
132     daddy_trans_->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T :SCM_BOOL_F);
133 }
134
135 void
136 Beam_engraver::process_music ()
137 {
138   if (evs_drul_[STOP])
139     {
140       prev_start_ev_ =0;
141       finished_beam_ = beam_;
142       finished_beam_info_ = beam_info_;
143
144       beam_info_ =0;
145       beam_ = 0;
146     }
147
148   if (beam_ && !to_boolean (get_property ("allowBeamBreak")))
149     {
150       top_engraver ()->forbid_breaks ();
151     }
152
153   if (evs_drul_[START])
154     {
155       if (beam_)
156         {
157           evs_drul_[START]->origin ()->warning (_ ("already have a beam"));
158           return;
159         }
160
161       set_melisma (true);
162       prev_start_ev_ = evs_drul_[START];
163       beam_ = new Spanner (get_property ("Beam"));
164       SCM smp = get_property ("measurePosition");
165       Moment mp = (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
166
167       beam_start_location_ = mp;
168       beam_start_mom_ = now_mom ();
169       
170       beam_info_ = new Beaming_info_list;
171       
172       /* urg, must copy to Auto_beam_engraver too */
173  
174       announce_grob(beam_, evs_drul_[START]->self_scm());
175     }
176
177 }
178
179
180 void
181 Beam_engraver::typeset_beam ()
182 {
183   if (finished_beam_)
184     {
185       finished_beam_info_->beamify(beat_length_, subdivide_beams_);
186       Beam::set_beaming (finished_beam_, finished_beam_info_);
187       typeset_grob (finished_beam_);
188       delete finished_beam_info_;
189       finished_beam_info_ =0;
190       finished_beam_ = 0;
191     }
192 }
193
194 void
195 Beam_engraver::start_translation_timestep ()
196 {
197   evs_drul_ [START] =0;
198   evs_drul_[STOP] = 0;
199   
200   if (beam_)
201     {
202       set_melisma (true);
203       
204       subdivide_beams_ = to_boolean(get_property("subdivideBeams"));
205       beat_length_ = *unsmob_moment (get_property ("beatLength"));
206     }
207 }
208
209 void
210 Beam_engraver::stop_translation_timestep ()
211 {
212   typeset_beam ();
213   if (now_stop_ev_ )
214     {
215       finished_beam_ = beam_;
216       finished_beam_info_ = beam_info_;
217
218       now_stop_ev_ = 0;
219       beam_ = 0;
220       beam_info_ = 0;
221       typeset_beam();
222           set_melisma (false);
223     }
224 }
225
226 void
227 Beam_engraver::finalize ()
228 {
229   typeset_beam ();
230   if (beam_)
231     {
232       prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
233
234       /*
235         we don't typeset it, (we used to, but it was commented
236         out. Reason unknown) */
237       beam_->suicide ();
238       delete beam_info_;
239     }
240 }
241
242 void
243 Beam_engraver::acknowledge_grob (Grob_info info)
244 {
245   if (beam_)
246     {
247       if (Rest::has_interface (info.grob_))
248         {
249           info.grob_->add_offset_callback (Beam::rest_collision_callback_proc, Y_AXIS);
250         }
251       else if (Stem::has_interface (info.grob_))
252         {
253           Moment now = now_mom();
254
255           if (!valid_start_moment ())
256             return ;
257           
258           Item *stem = dynamic_cast<Item*> (info.grob_);
259           if (Stem::get_beam (stem))
260             return;
261
262           Music* m = info.music_cause();
263           if (!m->is_mus_type ("rhythmic-event"))
264             {
265               String s = _ ("stem must have Rhythmic structure");
266               if (info.music_cause ())
267                 info.music_cause ()->origin ()->warning (s);
268               else
269                 ::warning (s);
270           
271               return;
272             }
273
274
275           last_stem_added_at_ = now;
276           int durlog  = unsmob_duration (m->get_mus_property ("duration"))-> duration_log ();
277           if (durlog <= 2)
278             {
279               m->origin ()->warning (_ ("stem doesn't fit in beam"));
280               prev_start_ev_->origin ()->warning (_ ("beam was started here"));
281               /*
282                 don't return, since
283
284                 [r4 c8] can just as well be modern notation.
285               */
286             }
287
288           stem->set_grob_property ("duration-log",
289                                     scm_int2num (durlog));
290           Moment stem_location = now - beam_start_mom_ + beam_start_location_;
291           beam_info_->add_stem (stem_location,
292  (durlog- 2) >? 0);
293           Beam::add_stem (beam_, stem);
294         }
295     }
296 }
297
298
299
300
301
302 ENTER_DESCRIPTION(Beam_engraver,
303 /* descr */       "Handles Beam events by engraving Beams.    If omitted, then notes will be "
304 "printed with flags instead of beams.",
305 /* creats*/       "Beam",
306 /* accepts */     "beam-event abort-event",
307 /* acks  */      "stem-interface rest-interface",
308 /* reads */       "beamMelismaBusy beatLength subdivideBeams",
309 /* write */       "");
310
311
312 class Grace_beam_engraver : public Beam_engraver
313 {
314 public:
315   TRANSLATOR_DECLARATIONS(Grace_beam_engraver);  
316
317 protected:
318   virtual bool valid_start_moment();
319   virtual bool valid_end_moment ();
320 };
321
322 Grace_beam_engraver::Grace_beam_engraver()
323 {
324 }
325
326 bool
327 Grace_beam_engraver::valid_start_moment()
328 {
329   Moment n = now_mom ();
330
331   return n.grace_part_ != Rational (0);
332 }
333
334
335 bool
336 Grace_beam_engraver::valid_end_moment ()
337 {
338   return beam_ && last_stem_added_at_.grace_part_ != Rational(0);
339 }
340
341
342
343 ENTER_DESCRIPTION(Grace_beam_engraver,
344 /* descr */       "Handles Beam events by engraving Beams.  If omitted, then notes will "
345 "be printed with flags instead of beams. Only engraves beams when we "
346 " are at grace points in time. "
347 ,
348 /* creats*/       "Beam",
349 /* accepts */     "beam-event abort-event",
350 /* acks  */      "stem-interface rest-interface",
351 /* reads */       "beamMelismaBusy beatLength allowBeamBreak subdivideBeams",
352 /* write */       "");
353