]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
* lily/include/event.hh: remove file.
[lilypond.git] / lily / auto-beam-engraver.cc
1 /*
2   auto-beam-engraver.cc -- implement Auto_beam_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "engraver.hh"
10 #include "beaming.hh"
11 #include "beam.hh"
12 #include "stem.hh"
13 #include "warn.hh"
14 #include "bar-line.hh"
15 #include "rest.hh"
16 #include "item.hh"
17 #include "spanner.hh"
18 #include "context.hh"
19 #include "duration.hh"
20
21
22 class Auto_beam_engraver : public Engraver
23 {
24   TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
25
26 protected:
27   virtual void stop_translation_timestep ();
28   virtual void start_translation_timestep ();
29   virtual void process_music ();
30   virtual bool try_music (Music *);
31   virtual void finalize ();
32   virtual void acknowledge_grob (Grob_info);
33   virtual void process_acknowledged_grobs ();
34
35 private:
36   bool test_moment (Direction, Moment);
37   void consider_begin (Moment);
38   void consider_end (Moment);
39   Spanner *create_beam ();
40   void begin_beam ();
41   void end_beam ();
42   void junk_beam ();
43   bool is_same_grace_state (Grob *e);
44   void typeset_beam ();
45
46   Music *forbid_;
47   /*
48     shortest_mom is the shortest note in the beam.
49   */
50   Moment shortest_mom_;
51   Spanner *finished_beam_;
52   Link_array<Item> *stems_;
53
54   int process_acknowledged_count_;
55   Moment last_add_mom_;
56   /*
57     Projected ending of the  beam we're working on.
58   */
59   Moment extend_mom_;
60   Moment beam_start_moment_;
61   Moment beam_start_location_;
62
63   bool subdivide_beams_;
64   Moment beat_length_;
65
66   // We act as if beam were created, and start a grouping anyway.
67   Beaming_info_list *grouping_;
68   SCM beam_settings_;           // ugh. should protect ? 
69
70   Beaming_info_list *finished_grouping_;
71 };
72
73 void
74 Auto_beam_engraver::process_music ()
75 {
76   if (scm_is_string (get_property ("whichBar")))
77     {
78       consider_end (shortest_mom_);
79       junk_beam ();
80     }
81
82   if (forbid_)
83     {
84       consider_end (shortest_mom_);
85       junk_beam ();
86     }
87 }
88
89 Auto_beam_engraver::Auto_beam_engraver ()
90 {
91   forbid_ = 0;
92 process_acknowledged_count_ = 0;
93   stems_ = 0;
94   shortest_mom_ = Moment (Rational (1, 8));
95   finished_beam_ = 0;
96   finished_grouping_ = 0;
97   grouping_ = 0;
98   beam_settings_ = SCM_EOL;
99 }
100
101 bool
102 Auto_beam_engraver::try_music (Music *m)
103 {
104   if (m->is_mus_type ("beam-forbid-event"))
105     {
106       forbid_ = m;
107       return true;
108     }
109
110   return false;
111 }
112
113 bool
114 Auto_beam_engraver::test_moment (Direction dir, Moment test)
115 {
116   return scm_call_3 (get_property ("autoBeamCheck"),
117                      context ()->self_scm (),
118                      scm_from_int (dir),
119                      test.smobbed_copy ())
120     != SCM_BOOL_F;
121 }
122     
123 void
124 Auto_beam_engraver::consider_begin (Moment test_mom)
125 {
126   bool on = to_boolean (get_property ("autoBeaming"));
127   if (!stems_ && on
128       && !forbid_)
129     {
130       bool b = test_moment (START, test_mom);
131       if (b)
132         begin_beam ();
133     }
134 }
135
136 void
137 Auto_beam_engraver::consider_end (Moment test_mom)
138 {
139   if (stems_)
140     {
141       /* Allow already started autobeam to end:
142          don't check for autoBeaming */
143       bool b = test_moment (STOP, test_mom);
144       if (b)
145         end_beam ();
146     }
147 }
148
149 Spanner *
150 Auto_beam_engraver::create_beam ()
151 {
152   if (to_boolean (get_property ("skipTypesetting")))
153     return 0;
154
155   Spanner *beam = new Spanner (beam_settings_, context ()->get_grob_key ("Beam"));
156   for (int i = 0; i < stems_->size (); i++)
157     {
158       /*
159         watch out for stem tremolos and abbreviation beams
160       */
161       if (Stem::get_beam ((*stems_)[i]))
162         {
163           scm_gc_unprotect_object (beam->self_scm ());
164           return 0;
165         }
166       Beam::add_stem (beam, (*stems_)[i]);
167     }
168
169   announce_grob (beam, (*stems_)[0]->self_scm ());
170
171   return beam;
172 }
173
174 void
175 Auto_beam_engraver::begin_beam ()
176 {
177   if (stems_ || grouping_)
178     {
179       programming_error ("already have autobeam");
180       return;
181     }
182
183   stems_ = new Link_array<Item>;
184   grouping_ = new Beaming_info_list;
185   beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
186
187   beam_start_moment_ = now_mom ();
188   beam_start_location_
189     = robust_scm2moment (get_property ("measurePosition"), Moment (0));
190   subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
191   beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
192 }
193
194 void
195 Auto_beam_engraver::junk_beam ()
196 {
197   if (!stems_)
198     return;
199
200   delete stems_;
201   stems_ = 0;
202   delete grouping_;
203   grouping_ = 0;
204   beam_settings_ = SCM_EOL;
205
206   shortest_mom_ = Moment (Rational (1, 8));
207 }
208
209 void
210 Auto_beam_engraver::end_beam ()
211 {
212   if (stems_->size () < 2)
213     {
214       junk_beam ();
215     }
216   else
217     {
218       finished_beam_ = create_beam ();
219       if (finished_beam_)
220         finished_grouping_ = grouping_;
221       delete stems_;
222       stems_ = 0;
223       grouping_ = 0;
224       beam_settings_ = SCM_EOL;
225     }
226
227   shortest_mom_ = Moment (Rational (1, 8));
228 }
229
230 void
231 Auto_beam_engraver::typeset_beam ()
232 {
233   if (finished_beam_)
234     {
235       finished_grouping_->beamify (beat_length_, subdivide_beams_);
236       Beam::set_beaming (finished_beam_, finished_grouping_);
237       finished_beam_ = 0;
238
239       delete finished_grouping_;
240       finished_grouping_ = 0;
241     }
242 }
243
244 void
245 Auto_beam_engraver::start_translation_timestep ()
246 {
247   process_acknowledged_count_ = 0;
248   /*
249     don't beam over skips
250   */
251   if (stems_)
252     {
253       Moment now = now_mom ();
254       if (extend_mom_ < now)
255         {
256           end_beam ();
257         }
258     }
259   forbid_ = 0;
260 }
261
262 void
263 Auto_beam_engraver::stop_translation_timestep ()
264 {
265   typeset_beam ();
266 }
267
268 void
269 Auto_beam_engraver::finalize ()
270 {
271   /* finished beams may be typeset */
272   typeset_beam ();
273   /* but unfinished may need another announce/acknowledge pass */
274   if (stems_)
275     junk_beam ();
276 }
277
278 void
279 Auto_beam_engraver::acknowledge_grob (Grob_info info)
280 {
281   /* Duplicated from process_music (), since
282      Repeat_acknowledge_engraver::process_music () may also set whichBar.  */
283
284   Moment now = now_mom ();
285   if (scm_is_string (get_property ("whichBar"))
286       && beam_start_moment_ < now)
287     {
288       consider_end (shortest_mom_);
289       junk_beam ();
290     }
291
292   if (stems_)
293     {
294       if (Beam::has_interface (info.grob ()))
295         {
296           end_beam ();
297         }
298       else if (Bar_line::has_interface (info.grob ()))
299         {
300           end_beam ();
301         }
302       else if (Rest::has_interface (info.grob ()))
303         {
304           end_beam ();
305         }
306     }
307
308   if (Stem::has_interface (info.grob ()))
309     {
310       Item *stem = dynamic_cast<Item *> (info.grob ());
311       Music *m = info.music_cause ();
312       if (!m->is_mus_type ("rhythmic-event"))
313         {
314           programming_error ("stem must have rhythmic structure");
315           return;
316         }
317
318       /*
319         Don't (start) auto-beam over empty stems; skips or rests
320       */
321       if (!Stem::head_count (stem))
322         {
323           if (stems_)
324             end_beam ();
325           return;
326         }
327
328       if (Stem::get_beam (stem))
329         {
330           if (stems_)
331             junk_beam ();
332           return;
333         }
334
335       int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
336
337       if (durlog <= 2)
338         {
339           if (stems_)
340             end_beam ();
341           return;
342         }
343
344       /*
345         ignore grace notes.
346       */
347       if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
348         return;
349
350       Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
351
352       consider_end (dur);
353       consider_begin (dur);
354
355       if (dur < shortest_mom_)
356         shortest_mom_ = dur;
357
358       if (!stems_)
359         return;
360
361       grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
362                            durlog - 2);
363       stems_->push (stem);
364       last_add_mom_ = now;
365       extend_mom_ = max (extend_mom_, now) + m->get_length ();
366     }
367 }
368
369 void
370 Auto_beam_engraver::process_acknowledged_grobs ()
371 {
372   if (extend_mom_ > now_mom ())
373     return ; 
374
375   if (!process_acknowledged_count_)
376     {
377       consider_end (shortest_mom_);
378       consider_begin (shortest_mom_);
379     }
380   else if (process_acknowledged_count_ > 1)
381     {
382       if (stems_)
383         {
384           Moment now = now_mom ();
385           if ((extend_mom_ < now)
386               || ((extend_mom_ == now) && (last_add_mom_ != now)))
387             {
388               end_beam ();
389             }
390           else if (!stems_->size ())
391             {
392               junk_beam ();
393             }
394         }
395     }
396
397   process_acknowledged_count_++;
398 }
399
400 ADD_TRANSLATOR (Auto_beam_engraver,
401                 /* descr */ "Generate beams based on measure characteristics and observed "
402                 "Stems.  Uses beatLength, measureLength and measurePosition to decide "
403                 "when to start and stop a beam.  Overriding beaming is done through "
404                 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
405                 "@code{stemRightBeamCount}. ",
406                 /* creats*/ "Beam",
407                 /* accepts */ "beam-forbid-event",
408                 /* acks  */ "stem-interface rest-interface beam-interface bar-line-interface",
409                 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",
410                 /* write */ "");