]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
Issue 4887: Remove delegating listener stubs
[lilypond.git] / lily / auto-beam-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2015 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 "beaming-pattern.hh"
21 #include "beam.hh"
22 #include "context.hh"
23 #include "context-handle.hh"
24 #include "duration.hh"
25 #include "engraver.hh"
26 #include "grob-properties.hh"
27 #include "item.hh"
28 #include "rest.hh"
29 #include "spanner.hh"
30 #include "stream-event.hh"
31 #include "stem.hh"
32 #include "warn.hh"
33
34 #include "translator.icc"
35
36 class Auto_beam_engraver : public Engraver
37 {
38   TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
39
40 protected:
41   void stop_translation_timestep ();
42   void process_acknowledged ();
43
44   virtual void process_music ();
45   virtual void finalize ();
46   virtual void derived_mark () const;
47
48 public:
49   void acknowledge_rest (Grob_info);
50   void acknowledge_beam (Grob_info);
51   void acknowledge_bar_line (Grob_info);
52   void acknowledge_breathing_sign (Grob_info);
53   void acknowledge_stem (Grob_info);
54   void listen_beam_forbid (Stream_event *);
55
56 private:
57   virtual bool test_moment (Direction, Moment, Moment);
58   void consider_begin (Moment, Moment);
59   void consider_end (Moment, Moment);
60   Spanner *create_beam ();
61   void begin_beam ();
62   void end_beam ();
63   void junk_beam ();
64   virtual bool is_same_grace_state (Moment, Moment);
65   void recheck_beam ();
66   void typeset_beam ();
67   vector<Item *> *remove_end_stems (vsize);
68
69   Stream_event *forbid_;
70   /*
71     shortest_mom_ is the shortest note in the beam.
72   */
73   Moment shortest_mom_;
74   Spanner *finished_beam_;
75   vector<Item *> *stems_;
76
77   int process_acknowledged_count_;
78   Moment last_add_mom_;
79   /*
80     Projected ending of the  beam we're working on.
81   */
82   Moment extend_mom_;
83   Moment beam_start_moment_;
84   Moment beam_start_location_;
85   /*
86     Handle on the starting staff keeps it alive until beam is comlete
87   */
88   Context_handle beam_start_context_;
89
90   // We act as if beam were created, and start a grouping anyway.
91   Beaming_pattern *grouping_;
92   SCM beam_settings_;
93
94   Beaming_pattern *finished_grouping_;
95
96   Beaming_options beaming_options_;
97   Beaming_options finished_beaming_options_;
98
99   void check_bar_property ();
100 };
101
102 void
103 Auto_beam_engraver::derived_mark () const
104 {
105   scm_gc_mark (beam_settings_);
106 }
107
108 void
109 Auto_beam_engraver::check_bar_property ()
110 {
111   /* Duplicated from process_music (), since
112      Repeat_acknowledge_engraver::process_music () may also set whichBar.  */
113
114   Moment now = now_mom ();
115   if (scm_is_string (get_property ("whichBar"))
116       && beam_start_moment_ < now)
117     {
118       consider_end (measure_position (context ()), shortest_mom_);
119       junk_beam ();
120     }
121 }
122
123 void
124 Auto_beam_engraver::process_music ()
125 {
126   Moment now = now_mom ();
127   /*
128     don't beam over skips
129   */
130   if (stems_)
131     {
132       if (extend_mom_ < now)
133         end_beam ();
134     }
135
136   if (scm_is_string (get_property ("whichBar")))
137     {
138       consider_end (measure_position (context ()), shortest_mom_);
139       junk_beam ();
140     }
141
142   if (forbid_)
143     {
144       if (stems_)
145         end_beam ();
146       else
147         junk_beam ();
148     }
149 }
150
151 Auto_beam_engraver::Auto_beam_engraver ()
152 {
153   forbid_ = 0;
154   process_acknowledged_count_ = 0;
155   stems_ = 0;
156   shortest_mom_ = Moment (Rational (1, 4));
157   extend_mom_ = Moment (-1);
158   finished_beam_ = 0;
159   finished_grouping_ = 0;
160   grouping_ = 0;
161   beam_settings_ = SCM_EOL;
162 }
163
164 void
165 Auto_beam_engraver::listen_beam_forbid (Stream_event *ev)
166 {
167   ASSIGN_EVENT_ONCE (forbid_, ev);
168 }
169
170 bool
171 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom, Moment dur)
172 {
173   return scm_is_true (scm_call_4 (get_property ("autoBeamCheck"),
174                                     context ()->self_scm (),
175                                     scm_from_int (dir),
176                                     test_mom.smobbed_copy (),
177                                     dur.smobbed_copy ()));
178 }
179
180 void
181 Auto_beam_engraver::consider_begin (Moment test_mom, Moment dur)
182 {
183   bool on = to_boolean (get_property ("autoBeaming"));
184   if (!stems_ && on
185       && !forbid_)
186     {
187       bool b = test_moment (START, test_mom, dur);
188       if (b)
189         begin_beam ();
190     }
191 }
192
193 void
194 Auto_beam_engraver::consider_end (Moment test_mom, Moment dur)
195 {
196   if (stems_)
197     {
198       /* Allow already started autobeam to end:
199          don't check for autoBeaming */
200       bool b = test_moment (STOP, test_mom, dur);
201       if (b)
202         end_beam ();
203     }
204 }
205
206 Spanner *
207 Auto_beam_engraver::create_beam ()
208 {
209   if (to_boolean (get_property ("skipTypesetting")))
210     return 0;
211
212   for (vsize i = 0; i < stems_->size (); i++)
213     if (Stem::get_beam ((*stems_)[i]))
214       return 0;
215
216   /*
217     Can't use make_spanner () because we have to use
218     beam_settings_.
219   */
220   Spanner *beam = new Spanner (beam_settings_);
221
222   for (vsize i = 0; i < stems_->size (); i++)
223     Beam::add_stem (beam, (*stems_)[i]);
224
225   Grob_info i = make_grob_info (beam, (*stems_)[0]->self_scm ());
226   announce_grob (i, beam_start_context_.get_context ());
227
228   return beam;
229 }
230
231 void
232 Auto_beam_engraver::begin_beam ()
233 {
234   if (stems_ || grouping_)
235     {
236       programming_error ("already have autobeam");
237       return;
238     }
239
240   stems_ = new vector<Item *>;
241   grouping_ = new Beaming_pattern ();
242   beaming_options_.from_context (context ());
243   beam_settings_ = Grob_property_info (context (), ly_symbol2scm ("Beam")).updated ();
244
245   beam_start_context_.set_context (context ()->get_parent_context ());
246   beam_start_moment_ = now_mom ();
247   beam_start_location_
248     = robust_scm2moment (get_property ("measurePosition"), Moment (0));
249 }
250
251 void
252 Auto_beam_engraver::junk_beam ()
253 {
254   if (!stems_)
255     return;
256
257   delete stems_;
258   stems_ = 0;
259   delete grouping_;
260   grouping_ = 0;
261   beam_settings_ = SCM_EOL;
262
263   shortest_mom_ = Moment (Rational (1, 4));
264 }
265
266 bool
267 Auto_beam_engraver::is_same_grace_state (Moment start, Moment now)
268 {
269   return bool (start.grace_part_) == bool (now.grace_part_);
270 }
271
272
273 void
274 Auto_beam_engraver::end_beam ()
275 {
276   if (stems_->size () < 2)
277     junk_beam ();
278   else
279     {
280       finished_beam_ = create_beam ();
281
282       if (finished_beam_)
283         {
284           Grob_info i = make_grob_info (finished_beam_, SCM_EOL);
285
286           announce_end_grob (i, beam_start_context_.get_context ());
287           finished_grouping_ = grouping_;
288           finished_beaming_options_ = beaming_options_;
289         }
290       delete stems_;
291       stems_ = 0;
292       grouping_ = 0;
293       beam_settings_ = SCM_EOL;
294     }
295
296   beam_start_context_.set_context (NULL);
297   shortest_mom_ = Moment (Rational (1, 4));
298 }
299
300 void
301 Auto_beam_engraver::typeset_beam ()
302 {
303   if (finished_beam_)
304     {
305       if (!finished_beam_->get_bound (RIGHT))
306         finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
307
308       finished_grouping_->beamify (finished_beaming_options_);
309       Beam::set_beaming (finished_beam_, finished_grouping_);
310       finished_beam_ = 0;
311
312       delete finished_grouping_;
313       finished_grouping_ = 0;
314     }
315 }
316
317 void
318 Auto_beam_engraver::stop_translation_timestep ()
319 {
320   typeset_beam ();
321   process_acknowledged_count_ = 0;
322   forbid_ = 0;
323 }
324
325 void
326 Auto_beam_engraver::finalize ()
327 {
328   /* finished beams may be typeset */
329   typeset_beam ();
330   /* but unfinished may need another announce/acknowledge pass */
331   if (stems_)
332     junk_beam ();
333 }
334
335 void
336 Auto_beam_engraver::acknowledge_beam (Grob_info /* info */)
337 {
338   check_bar_property ();
339   if (stems_)
340     end_beam ();
341 }
342
343 void
344 Auto_beam_engraver::acknowledge_bar_line (Grob_info /* info */)
345 {
346   check_bar_property ();
347   if (stems_)
348     end_beam ();
349 }
350
351 void
352 Auto_beam_engraver::acknowledge_breathing_sign (Grob_info /* info */)
353 {
354   check_bar_property ();
355   if (stems_)
356     end_beam ();
357 }
358
359 void
360 Auto_beam_engraver::acknowledge_rest (Grob_info /* info */)
361 {
362   check_bar_property ();
363   if (stems_)
364     end_beam ();
365 }
366
367 void
368 Auto_beam_engraver::acknowledge_stem (Grob_info info)
369 {
370   check_bar_property ();
371   Item *stem = dynamic_cast<Item *> (info.grob ());
372   Stream_event *ev = info.ultimate_event_cause ();
373   if (!ev->in_event_class ("rhythmic-event"))
374     {
375       programming_error ("stem must have rhythmic structure");
376       return;
377     }
378
379   /*
380     Don't (start) auto-beam over empty stems; skips or rests
381   */
382   if (!Stem::head_count (stem))
383     {
384       if (stems_)
385         end_beam ();
386       return;
387     }
388
389   if (Stem::get_beam (stem))
390     {
391       if (stems_)
392         junk_beam ();
393       return;
394     }
395
396   int durlog = unsmob<Duration> (ev->get_property ("duration"))->duration_log ();
397
398   if (durlog <= 2)
399     {
400       if (stems_)
401         end_beam ();
402       return;
403     }
404
405   /*
406     ignore interspersed grace notes.
407   */
408   Moment now = now_mom ();
409   if (!is_same_grace_state (beam_start_location_, now))
410     return;
411
412   Duration *stem_duration = unsmob<Duration> (ev->get_property ("duration"));
413   Moment dur = stem_duration->get_length ();
414
415   //Moment dur = unsmob<Duration> (ev->get_property ("duration"))->get_length ();
416   Moment measure_now = measure_position (context ());
417   bool recheck_needed = false;
418
419   if (dur < shortest_mom_)
420     {
421       /* new shortest moment, so store it and set recheck_needed */
422       shortest_mom_ = dur;
423       recheck_needed = true;
424     }
425
426   /* end should be based on shortest_mom_, begin should be
427      based on current duration  */
428   consider_end (measure_now, shortest_mom_);
429   consider_begin (measure_now, dur);
430
431   if (!stems_)
432     return;
433
434   grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
435                        durlog - 2,
436                        Stem::is_invisible (stem),
437                        stem_duration->factor (),
438                        (to_boolean (stem->get_property ("tuplet-start"))));
439   stems_->push_back (stem);
440   last_add_mom_ = now;
441   extend_mom_ = max (extend_mom_, now) + get_event_length (ev, now);
442   if (recheck_needed)
443     recheck_beam ();
444 }
445
446 void
447 Auto_beam_engraver::recheck_beam ()
448 {
449   /*
450     Recheck the beam after the shortest duration has changed
451     If shorter duration has created a new break, typeset the
452     first part of the beam and reset the current beam to just
453     the last part of the beam
454   */
455   Beaming_pattern *new_grouping_ = 0;
456   vector<Item *> *new_stems_ = 0;
457   Moment temporary_shortest_mom;
458   SCM temporary_beam_settings;
459
460   bool found_end;
461
462   for (vsize i = 0; i < stems_->size () - 1;)
463     {
464       found_end = test_moment (STOP,
465                                grouping_->end_moment (i),
466                                shortest_mom_);
467       if (!found_end)
468         i++;
469       else
470         {
471           /*
472             Save the current beam settings and shortest_mom_
473             Necessary because end_beam destroys them
474           */
475           temporary_shortest_mom = shortest_mom_;
476           temporary_beam_settings = beam_settings_;
477
478           /* Eliminate (and save) the items no longer part of the first beam */
479
480           new_grouping_ = grouping_->split_pattern (i);
481           new_stems_ = remove_end_stems (i);
482
483           end_beam ();
484           typeset_beam ();
485
486           /* now recreate the unbeamed data structures */
487           stems_ = new_stems_;
488           grouping_ = new_grouping_;
489           shortest_mom_ = temporary_shortest_mom;
490           beam_settings_ = temporary_beam_settings;
491
492           i = 0;
493         }
494
495     }
496
497 }
498
499 /*
500   Remove all stems with an index greater than split_index
501   from stems_, and return a vector containing all of the
502   removed stems
503 */
504 vector <Item *> *
505 Auto_beam_engraver::remove_end_stems (vsize split_index)
506 {
507   vector <Item *> *removed_stems = 0;
508   removed_stems = new vector <Item *>;
509
510   for (vsize j = split_index + 1; j < stems_->size (); j++)
511     removed_stems->push_back ((*stems_).at (j));
512   for (vsize j = split_index + 1; j < stems_->size ();)
513     stems_->pop_back ();
514   return removed_stems;
515 }
516
517 void
518 Auto_beam_engraver::process_acknowledged ()
519 {
520   Moment now = now_mom ();
521   if (extend_mom_ > now)
522     return;
523
524   if (!process_acknowledged_count_)
525     {
526       Moment measure_now = measure_position (context ());
527       consider_end (measure_now, shortest_mom_);
528     }
529   else if (process_acknowledged_count_ > 1)
530     {
531       if (stems_)
532         {
533           if ((extend_mom_ < now)
534               || ((extend_mom_ == now) && (last_add_mom_ != now)))
535             end_beam ();
536           else if (!stems_->size ())
537             junk_beam ();
538         }
539     }
540
541   process_acknowledged_count_++;
542 }
543
544 void
545 Auto_beam_engraver::boot ()
546 {
547   ADD_LISTENER (Auto_beam_engraver, beam_forbid);
548   ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
549   ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
550   ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
551   ADD_ACKNOWLEDGER (Auto_beam_engraver, breathing_sign);
552   ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
553 }
554
555 ADD_TRANSLATOR (Auto_beam_engraver,
556                 /* doc */
557                 "Generate beams based on measure characteristics and observed"
558                 " Stems.  Uses @code{baseMoment}, @code{beatStructure},"
559                 " @code{beamExceptions}, @code{measureLength}, and"
560                 " @code{measurePosition} to decide when to start and stop a"
561                 " beam.  Overriding beaming is done through"
562                 " @ref{Stem_engraver} properties @code{stemLeftBeamCount} and"
563                 " @code{stemRightBeamCount}.",
564
565                 /* create */
566                 "Beam ",
567
568                 /* read */
569                 "autoBeaming "
570                 "baseMoment "
571                 "beamExceptions "
572                 "beamHalfMeasure "
573                 "beatStructure "
574                 "subdivideBeams ",
575
576                 /* write */
577                 ""
578                );
579
580 class Grace_auto_beam_engraver : public Auto_beam_engraver
581 {
582   TRANSLATOR_DECLARATIONS (Grace_auto_beam_engraver);
583   TRANSLATOR_INHERIT (Auto_beam_engraver);
584
585 private:
586   Moment last_grace_start_; // Full starting time of last grace group
587   Moment last_grace_position_; // Measure position of same
588   virtual void process_music ();
589   virtual bool is_same_grace_state (Moment, Moment);
590   virtual bool test_moment (Direction, Moment, Moment);
591 };
592
593 Grace_auto_beam_engraver::Grace_auto_beam_engraver ()
594 {
595   last_grace_start_.main_part_.set_infinite (-1);
596   // grace_part_ is zero -> test_moment is false, last_grace_position_
597   // not considered.
598 }
599
600 bool
601 Grace_auto_beam_engraver::is_same_grace_state (Moment, Moment)
602 {
603   // This is for ignoring interspersed grace notes in main note
604   // beaming.  We never want to ignore something inside of grace note
605   // beaming, so return true.
606   return true;
607 }
608
609 void
610 Grace_auto_beam_engraver::process_music ()
611 {
612   Moment now = now_mom ();
613   // Update last_grace_start_ and last_grace_position_ only when the
614   // main time advances.
615   if (now.main_part_ > last_grace_start_.main_part_)
616     {
617       last_grace_start_ = now;
618       last_grace_position_ = measure_position (context ());
619     }
620
621   Auto_beam_engraver::process_music ();
622 }
623
624 bool
625 Grace_auto_beam_engraver::test_moment (Direction dir, Moment test_mom, Moment)
626 {
627   // If no grace group started this main moment, we have no business
628   // beaming.  Same if we have left the original main time step.
629   if (!last_grace_start_.grace_part_
630       || last_grace_position_.main_part_ != test_mom.main_part_)
631     return false;
632   // Autobeam start only when at the start of the grace group.
633   if (dir == START)
634       return last_grace_position_ == test_mom;
635   // Autobeam end only when the grace part is finished.
636   return !test_mom.grace_part_;
637 }
638
639 void
640 Grace_auto_beam_engraver::boot ()
641 {
642   ADD_LISTENER (Auto_beam_engraver, beam_forbid);
643   ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
644   ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
645   ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
646   ADD_ACKNOWLEDGER (Auto_beam_engraver, breathing_sign);
647   ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
648 }
649
650 ADD_TRANSLATOR (Grace_auto_beam_engraver,
651                 /* doc */
652                 "Generates one autobeam group across an entire grace phrase. "
653                 " As usual, any manual beaming or @code{\\noBeam} will block"
654                 " autobeaming, just like setting the context property"
655                 " @samp{autoBeaming} to @code{##f}.",
656
657                 /* create */
658                 "Beam ",
659
660                 /* read */
661                 "autoBeaming ",
662
663                 /* write */
664                 ""
665                );