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