]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / auto-beam-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2011 Jan Nieuwenhuizen <janneke@gnu.org>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "bar-line.hh"
21 #include "beaming-pattern.hh"
22 #include "beam.hh"
23 #include "context.hh"
24 #include "duration.hh"
25 #include "engraver.hh"
26 #include "item.hh"
27 #include "rest.hh"
28 #include "spanner.hh"
29 #include "stream-event.hh"
30 #include "stem.hh"
31 #include "warn.hh"
32
33 #include "translator.icc"
34
35 class Auto_beam_engraver : public Engraver
36 {
37   TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
38
39 protected:
40   void stop_translation_timestep ();
41   void process_music ();
42   virtual void finalize ();
43   virtual void derived_mark () const;
44
45   DECLARE_ACKNOWLEDGER (rest);
46   DECLARE_ACKNOWLEDGER (beam);
47   DECLARE_ACKNOWLEDGER (bar_line);
48   DECLARE_ACKNOWLEDGER (breathing_sign);
49   DECLARE_ACKNOWLEDGER (stem);
50   DECLARE_TRANSLATOR_LISTENER (beam_forbid);
51
52   void process_acknowledged ();
53
54 private:
55   bool test_moment (Direction, Moment, Moment);
56   void consider_begin (Moment, Moment);
57   void consider_end (Moment, Moment);
58   Spanner *create_beam ();
59   void begin_beam ();
60   void end_beam ();
61   void junk_beam ();
62   bool is_same_grace_state (Grob *e);
63   void recheck_beam ();
64   void typeset_beam ();
65   vector<Item *> *remove_end_stems (vsize);
66
67   Stream_event *forbid_;
68   /*
69     shortest_mom_ is the shortest note in the beam.
70   */
71   Moment shortest_mom_;
72   Spanner *finished_beam_;
73   vector<Item *> *stems_;
74
75   int process_acknowledged_count_;
76   Moment last_add_mom_;
77   /*
78     Projected ending of the  beam we're working on.
79   */
80   Moment extend_mom_;
81   Moment beam_start_moment_;
82   Moment beam_start_location_;
83   Context *beam_start_context_;
84
85   // We act as if beam were created, and start a grouping anyway.
86   Beaming_pattern *grouping_;
87   SCM beam_settings_;
88
89   Beaming_pattern *finished_grouping_;
90
91
92   Beaming_options beaming_options_;
93   Beaming_options finished_beaming_options_;
94
95
96   void check_bar_property ();
97 };
98
99 void
100 Auto_beam_engraver::derived_mark () const
101 {
102   scm_gc_mark (beam_settings_);
103 }
104
105 void
106 Auto_beam_engraver::check_bar_property ()
107 {
108   /* Duplicated from process_music (), since
109      Repeat_acknowledge_engraver::process_music () may also set whichBar.  */
110
111   Moment now = now_mom ();
112   if (scm_is_string (get_property ("whichBar"))
113       && beam_start_moment_ < now)
114     {
115       consider_end (measure_position (context ()), shortest_mom_);
116       junk_beam ();
117     }
118 }
119
120 void
121 Auto_beam_engraver::process_music ()
122 {
123   Moment now = now_mom ();
124   /*
125     don't beam over skips
126   */
127   if (stems_)
128     {
129       if (extend_mom_ < now)
130         end_beam ();
131     }
132
133   if (scm_is_string (get_property ("whichBar")))
134     {
135       consider_end (measure_position (context ()), shortest_mom_);
136       junk_beam ();
137     }
138
139   if (forbid_)
140     {
141       if (stems_)
142         end_beam ();
143       else
144         junk_beam ();
145     }
146 }
147
148 Auto_beam_engraver::Auto_beam_engraver ()
149 {
150   forbid_ = 0;
151   process_acknowledged_count_ = 0;
152   stems_ = 0;
153   shortest_mom_ = Moment (Rational (1, 4));
154   finished_beam_ = 0;
155   finished_grouping_ = 0;
156   grouping_ = 0;
157   beam_settings_ = SCM_EOL;
158 }
159
160 IMPLEMENT_TRANSLATOR_LISTENER (Auto_beam_engraver, beam_forbid);
161 void
162 Auto_beam_engraver::listen_beam_forbid (Stream_event *ev)
163 {
164   ASSIGN_EVENT_ONCE (forbid_, ev);
165 }
166
167 bool
168 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom, Moment dur)
169 {
170   return scm_call_4 (get_property ("autoBeamCheck"),
171                      context ()->self_scm (),
172                      scm_from_int (dir),
173                      test_mom.smobbed_copy (),
174                      dur.smobbed_copy ())
175     != SCM_BOOL_F;
176 }
177
178 void
179 Auto_beam_engraver::consider_begin (Moment test_mom, Moment dur)
180 {
181   bool on = to_boolean (get_property ("autoBeaming"));
182   if (!stems_ && on
183       && !forbid_)
184     {
185       bool b = test_moment (START, test_mom, dur);
186       if (b)
187         begin_beam ();
188     }
189 }
190
191 void
192 Auto_beam_engraver::consider_end (Moment test_mom, Moment dur)
193 {
194   if (stems_)
195     {
196       /* Allow already started autobeam to end:
197          don't check for autoBeaming */
198       bool b = test_moment (STOP, test_mom, dur);
199       if (b)
200         end_beam ();
201     }
202 }
203
204 Spanner *
205 Auto_beam_engraver::create_beam ()
206 {
207   if (to_boolean (get_property ("skipTypesetting")))
208     return 0;
209
210   for (vsize i = 0; i < stems_->size (); i++)
211     if (Stem::get_beam ((*stems_)[i]))
212       return 0;
213
214   /*
215     Can't use make_spanner () because we have to use
216     beam_settings_.
217   */
218   Spanner *beam = new Spanner (beam_settings_);
219
220   for (vsize i = 0; i < stems_->size (); i++)
221     Beam::add_stem (beam, (*stems_)[i]);
222
223   Grob_info i = make_grob_info (beam, (*stems_)[0]->self_scm ());
224   i.rerouting_daddy_context_ = beam_start_context_;
225   announce_grob (i);
226
227   return beam;
228 }
229
230 void
231 Auto_beam_engraver::begin_beam ()
232 {
233   if (stems_ || grouping_)
234     {
235       programming_error ("already have autobeam");
236       return;
237     }
238
239   stems_ = new vector<Item *>;
240   grouping_ = new Beaming_pattern ();
241   beaming_options_.from_context (context ());
242   beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
243
244   beam_start_context_ = context ()->get_parent_context ();
245   beam_start_moment_ = now_mom ();
246   beam_start_location_
247     = robust_scm2moment (get_property ("measurePosition"), Moment (0));
248 }
249
250 void
251 Auto_beam_engraver::junk_beam ()
252 {
253   if (!stems_)
254     return;
255
256   delete stems_;
257   stems_ = 0;
258   delete grouping_;
259   grouping_ = 0;
260   beam_settings_ = SCM_EOL;
261
262   shortest_mom_ = Moment (Rational (1, 4));
263 }
264
265 void
266 Auto_beam_engraver::end_beam ()
267 {
268   if (stems_->size () < 2)
269     junk_beam ();
270   else
271     {
272       finished_beam_ = create_beam ();
273
274       if (finished_beam_)
275         {
276           Grob_info i = make_grob_info (finished_beam_, SCM_EOL);
277           i.rerouting_daddy_context_ = beam_start_context_;
278
279           announce_end_grob (i);
280           finished_grouping_ = grouping_;
281           finished_beaming_options_ = beaming_options_;
282         }
283       delete stems_;
284       stems_ = 0;
285       grouping_ = 0;
286       beam_settings_ = SCM_EOL;
287     }
288
289   shortest_mom_ = Moment (Rational (1, 4));
290 }
291
292 void
293 Auto_beam_engraver::typeset_beam ()
294 {
295   if (finished_beam_)
296     {
297       if (!finished_beam_->get_bound (RIGHT))
298         finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
299
300       finished_grouping_->beamify (finished_beaming_options_);
301       Beam::set_beaming (finished_beam_, finished_grouping_);
302       finished_beam_ = 0;
303
304       delete finished_grouping_;
305       finished_grouping_ = 0;
306     }
307 }
308
309 void
310 Auto_beam_engraver::stop_translation_timestep ()
311 {
312   typeset_beam ();
313   process_acknowledged_count_ = 0;
314   forbid_ = 0;
315 }
316
317 void
318 Auto_beam_engraver::finalize ()
319 {
320   /* finished beams may be typeset */
321   typeset_beam ();
322   /* but unfinished may need another announce/acknowledge pass */
323   if (stems_)
324     junk_beam ();
325 }
326
327
328 void
329 Auto_beam_engraver::acknowledge_beam (Grob_info /* info */)
330 {
331   check_bar_property ();
332   if (stems_)
333     end_beam ();
334 }
335
336 void
337 Auto_beam_engraver::acknowledge_bar_line (Grob_info /* info */)
338 {
339   check_bar_property ();
340   if (stems_)
341     end_beam ();
342 }
343
344 void
345 Auto_beam_engraver::acknowledge_breathing_sign (Grob_info /* info */)
346 {
347   check_bar_property ();
348   if (stems_)
349     end_beam ();
350 }
351
352 void
353 Auto_beam_engraver::acknowledge_rest (Grob_info /* info */)
354 {
355   check_bar_property ();
356   if (stems_)
357     end_beam ();
358 }
359
360 void
361 Auto_beam_engraver::acknowledge_stem (Grob_info info)
362 {
363   check_bar_property ();
364   Item *stem = dynamic_cast<Item *> (info.grob ());
365   Stream_event *ev = info.ultimate_event_cause ();
366   if (!ev->in_event_class ("rhythmic-event"))
367     {
368       programming_error ("stem must have rhythmic structure");
369       return;
370     }
371
372   /*
373     Don't (start) auto-beam over empty stems; skips or rests
374   */
375   if (!Stem::head_count (stem))
376     {
377       if (stems_)
378         end_beam ();
379       return;
380     }
381
382   if (Stem::get_beam (stem))
383     {
384       if (stems_)
385         junk_beam ();
386       return;
387     }
388
389   int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
390
391   if (durlog <= 2)
392     {
393       if (stems_)
394         end_beam ();
395       return;
396     }
397
398   /*
399     ignore grace notes.
400   */
401   Moment now = now_mom ();
402   if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
403     return;
404
405   Moment dur = unsmob_duration (ev->get_property ("duration"))->get_length ();
406   Moment measure_now = measure_position (context ());
407   bool recheck_needed = false;
408
409   if (dur < shortest_mom_)
410     {
411     /* new shortest moment, so store it and set recheck_needed */
412     shortest_mom_ = dur;
413     recheck_needed = true;
414     }
415
416   /* end should be based on shortest_mom_, begin should be
417      based on current duration  */
418   consider_end (measure_now, shortest_mom_);
419   consider_begin (measure_now, dur);
420
421   if (!stems_)
422     return;
423
424   grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
425                        durlog - 2,
426                        Stem::is_invisible (stem));
427   stems_->push_back (stem);
428   last_add_mom_ = now;
429   extend_mom_ = max (extend_mom_, now) + get_event_length (ev, now);
430   if (recheck_needed)
431     recheck_beam ();
432 }
433
434 void
435 Auto_beam_engraver::recheck_beam ()
436 {
437   /*
438     Recheck the beam after the shortest duration has changed
439     If shorter duration has created a new break, typeset the
440     first part of the beam and reset the current beam to just
441     the last part of the beam
442   */
443   Beaming_pattern *new_grouping_ = 0;
444   vector<Item *> *new_stems_ = 0;
445   Moment temporary_shortest_mom;
446   SCM temporary_beam_settings;
447
448   bool found_end;
449
450
451   for (vsize i = 0; i < stems_->size () - 1;)
452     {
453       found_end = test_moment (STOP,
454                                grouping_->end_moment (i),
455                                shortest_mom_);
456       if (!found_end)
457         i++;
458       else
459         {
460           /*
461             Save the current beam settings and shortest_mom_
462             Necessary because end_beam destroys them
463           */
464           temporary_shortest_mom = shortest_mom_;
465           temporary_beam_settings = beam_settings_;
466
467           /* Eliminate (and save) the items no longer part of the first beam */
468
469           new_grouping_ = grouping_->split_pattern (i);
470           new_stems_ = remove_end_stems (i);
471
472           end_beam ();
473           typeset_beam ();
474
475           /* now recreate the unbeamed data structures */
476           stems_ = new_stems_;
477           grouping_ = new_grouping_;
478           shortest_mom_ = temporary_shortest_mom;
479           beam_settings_ = temporary_beam_settings;
480
481           i = 0;
482         }
483
484     }
485
486 }
487
488 /*
489   Remove all stems with an index greater than split_index
490   from stems_, and return a vector containing all of the
491   removed stems
492 */
493 vector <Item *> *
494 Auto_beam_engraver::remove_end_stems (vsize split_index)
495 {
496   vector <Item *> *removed_stems = 0;
497   removed_stems = new vector <Item *>;
498
499   for (vsize j = split_index + 1; j < stems_->size (); j++)
500     removed_stems->push_back ((*stems_).at (j));
501   for (vsize j = split_index + 1; j < stems_->size ();)
502     stems_->pop_back ();
503   return removed_stems;
504 }
505
506 void
507 Auto_beam_engraver::process_acknowledged ()
508 {
509   Moment now = now_mom();
510   if (extend_mom_ > now)
511     return;
512
513   if (!process_acknowledged_count_)
514     {
515       Moment measure_now = measure_position (context ());
516       consider_end (measure_now, shortest_mom_);
517     }
518   else if (process_acknowledged_count_ > 1)
519     {
520       if (stems_)
521         {
522           if ((extend_mom_ < now)
523               || ((extend_mom_ == now) && (last_add_mom_ != now)))
524             end_beam ();
525           else if (!stems_->size ())
526             junk_beam ();
527         }
528     }
529
530   process_acknowledged_count_++;
531 }
532
533 ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
534 ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
535 ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
536 ADD_ACKNOWLEDGER (Auto_beam_engraver, breathing_sign);
537 ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
538 ADD_TRANSLATOR (Auto_beam_engraver,
539                 /* doc */
540                 "Generate beams based on measure characteristics and observed"
541                 " Stems.  Uses @code{baseMoment}, @code{beatStructure},"
542                 " @code{beamExceptions}, @code{measureLength}, and"
543                 " @code{measurePosition} to decide when to start and stop a"
544                 " beam.  Overriding beaming is done through"
545                 " @ref{Stem_engraver} properties @code{stemLeftBeamCount} and"
546                 " @code{stemRightBeamCount}.",
547
548                 /* create */
549                 "Beam ",
550
551                 /* read */
552                 "autoBeaming "
553                 "baseMoment "
554                 "beamExceptions "
555                 "beatStructure "
556                 "subdivideBeams ",
557
558                 /* write */
559                 ""
560                 );