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