]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
d9a3dd8ff62e5d45483af0a5819924338b35a388
[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 "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   DECLARE_ACKNOWLEDGER (rest);
49   DECLARE_ACKNOWLEDGER (beam);
50   DECLARE_ACKNOWLEDGER (bar_line);
51   DECLARE_ACKNOWLEDGER (breathing_sign);
52   DECLARE_ACKNOWLEDGER (stem);
53   DECLARE_TRANSLATOR_LISTENER (beam_forbid);
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 IMPLEMENT_TRANSLATOR_LISTENER (Auto_beam_engraver, beam_forbid);
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_call_4 (get_property ("autoBeamCheck"),
174                      context ()->self_scm (),
175                      scm_from_int (dir),
176                      test_mom.smobbed_copy (),
177                      dur.smobbed_copy ())
178          != SCM_BOOL_F;
179 }
180
181 void
182 Auto_beam_engraver::consider_begin (Moment test_mom, Moment dur)
183 {
184   bool on = to_boolean (get_property ("autoBeaming"));
185   if (!stems_ && on
186       && !forbid_)
187     {
188       bool b = test_moment (START, test_mom, dur);
189       if (b)
190         begin_beam ();
191     }
192 }
193
194 void
195 Auto_beam_engraver::consider_end (Moment test_mom, Moment dur)
196 {
197   if (stems_)
198     {
199       /* Allow already started autobeam to end:
200          don't check for autoBeaming */
201       bool b = test_moment (STOP, test_mom, dur);
202       if (b)
203         end_beam ();
204     }
205 }
206
207 Spanner *
208 Auto_beam_engraver::create_beam ()
209 {
210   if (to_boolean (get_property ("skipTypesetting")))
211     return 0;
212
213   for (vsize i = 0; i < stems_->size (); i++)
214     if (Stem::get_beam ((*stems_)[i]))
215       return 0;
216
217   /*
218     Can't use make_spanner () because we have to use
219     beam_settings_.
220   */
221   Spanner *beam = new Spanner (beam_settings_);
222
223   for (vsize i = 0; i < stems_->size (); i++)
224     Beam::add_stem (beam, (*stems_)[i]);
225
226   Grob_info i = make_grob_info (beam, (*stems_)[0]->self_scm ());
227   i.rerouting_daddy_context_ = beam_start_context_.get_context ();
228   announce_grob (i);
229
230   return beam;
231 }
232
233 void
234 Auto_beam_engraver::begin_beam ()
235 {
236   if (stems_ || grouping_)
237     {
238       programming_error ("already have autobeam");
239       return;
240     }
241
242   stems_ = new vector<Item *>;
243   grouping_ = new Beaming_pattern ();
244   beaming_options_.from_context (context ());
245   beam_settings_ = Grob_property_info (context (), ly_symbol2scm ("Beam")).updated ();
246
247   beam_start_context_.set_context (context ()->get_parent_context ());
248   beam_start_moment_ = now_mom ();
249   beam_start_location_
250     = robust_scm2moment (get_property ("measurePosition"), Moment (0));
251 }
252
253 void
254 Auto_beam_engraver::junk_beam ()
255 {
256   if (!stems_)
257     return;
258
259   delete stems_;
260   stems_ = 0;
261   delete grouping_;
262   grouping_ = 0;
263   beam_settings_ = SCM_EOL;
264
265   shortest_mom_ = Moment (Rational (1, 4));
266 }
267
268 bool
269 Auto_beam_engraver::is_same_grace_state (Moment start, Moment now)
270 {
271   return bool (start.grace_part_) == bool (now.grace_part_);
272 }
273
274
275 void
276 Auto_beam_engraver::end_beam ()
277 {
278   if (stems_->size () < 2)
279     junk_beam ();
280   else
281     {
282       finished_beam_ = create_beam ();
283
284       if (finished_beam_)
285         {
286           Grob_info i = make_grob_info (finished_beam_, SCM_EOL);
287           i.rerouting_daddy_context_ = beam_start_context_.get_context ();
288
289           announce_end_grob (i);
290           finished_grouping_ = grouping_;
291           finished_beaming_options_ = beaming_options_;
292         }
293       delete stems_;
294       stems_ = 0;
295       grouping_ = 0;
296       beam_settings_ = SCM_EOL;
297     }
298
299   beam_start_context_.set_context (NULL);
300   shortest_mom_ = Moment (Rational (1, 4));
301 }
302
303 void
304 Auto_beam_engraver::typeset_beam ()
305 {
306   if (finished_beam_)
307     {
308       if (!finished_beam_->get_bound (RIGHT))
309         finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
310
311       finished_grouping_->beamify (finished_beaming_options_);
312       Beam::set_beaming (finished_beam_, finished_grouping_);
313       finished_beam_ = 0;
314
315       delete finished_grouping_;
316       finished_grouping_ = 0;
317     }
318 }
319
320 void
321 Auto_beam_engraver::stop_translation_timestep ()
322 {
323   typeset_beam ();
324   process_acknowledged_count_ = 0;
325   forbid_ = 0;
326 }
327
328 void
329 Auto_beam_engraver::finalize ()
330 {
331   /* finished beams may be typeset */
332   typeset_beam ();
333   /* but unfinished may need another announce/acknowledge pass */
334   if (stems_)
335     junk_beam ();
336 }
337
338 void
339 Auto_beam_engraver::acknowledge_beam (Grob_info /* info */)
340 {
341   check_bar_property ();
342   if (stems_)
343     end_beam ();
344 }
345
346 void
347 Auto_beam_engraver::acknowledge_bar_line (Grob_info /* info */)
348 {
349   check_bar_property ();
350   if (stems_)
351     end_beam ();
352 }
353
354 void
355 Auto_beam_engraver::acknowledge_breathing_sign (Grob_info /* info */)
356 {
357   check_bar_property ();
358   if (stems_)
359     end_beam ();
360 }
361
362 void
363 Auto_beam_engraver::acknowledge_rest (Grob_info /* info */)
364 {
365   check_bar_property ();
366   if (stems_)
367     end_beam ();
368 }
369
370 void
371 Auto_beam_engraver::acknowledge_stem (Grob_info info)
372 {
373   check_bar_property ();
374   Item *stem = dynamic_cast<Item *> (info.grob ());
375   Stream_event *ev = info.ultimate_event_cause ();
376   if (!ev->in_event_class ("rhythmic-event"))
377     {
378       programming_error ("stem must have rhythmic structure");
379       return;
380     }
381
382   /*
383     Don't (start) auto-beam over empty stems; skips or rests
384   */
385   if (!Stem::head_count (stem))
386     {
387       if (stems_)
388         end_beam ();
389       return;
390     }
391
392   if (Stem::get_beam (stem))
393     {
394       if (stems_)
395         junk_beam ();
396       return;
397     }
398
399   int durlog = Duration::unsmob (ev->get_property ("duration"))->duration_log ();
400
401   if (durlog <= 2)
402     {
403       if (stems_)
404         end_beam ();
405       return;
406     }
407
408   /*
409     ignore interspersed grace notes.
410   */
411   Moment now = now_mom ();
412   if (!is_same_grace_state (beam_start_location_, now))
413     return;
414
415   Duration *stem_duration = Duration::unsmob (ev->get_property ("duration"));
416   Moment dur = stem_duration->get_length ();
417
418   //Moment dur = Duration::unsmob (ev->get_property ("duration"))->get_length ();
419   Moment measure_now = measure_position (context ());
420   bool recheck_needed = false;
421
422   if (dur < shortest_mom_)
423     {
424       /* new shortest moment, so store it and set recheck_needed */
425       shortest_mom_ = dur;
426       recheck_needed = true;
427     }
428
429   /* end should be based on shortest_mom_, begin should be
430      based on current duration  */
431   consider_end (measure_now, shortest_mom_);
432   consider_begin (measure_now, dur);
433
434   if (!stems_)
435     return;
436
437   grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
438                        durlog - 2,
439                        Stem::is_invisible (stem),
440                        stem_duration->factor (),
441                        (stem->get_property ("tuplet-start") == SCM_BOOL_T));
442   stems_->push_back (stem);
443   last_add_mom_ = now;
444   extend_mom_ = max (extend_mom_, now) + get_event_length (ev, now);
445   if (recheck_needed)
446     recheck_beam ();
447 }
448
449 void
450 Auto_beam_engraver::recheck_beam ()
451 {
452   /*
453     Recheck the beam after the shortest duration has changed
454     If shorter duration has created a new break, typeset the
455     first part of the beam and reset the current beam to just
456     the last part of the beam
457   */
458   Beaming_pattern *new_grouping_ = 0;
459   vector<Item *> *new_stems_ = 0;
460   Moment temporary_shortest_mom;
461   SCM temporary_beam_settings;
462
463   bool found_end;
464
465   for (vsize i = 0; i < stems_->size () - 1;)
466     {
467       found_end = test_moment (STOP,
468                                grouping_->end_moment (i),
469                                shortest_mom_);
470       if (!found_end)
471         i++;
472       else
473         {
474           /*
475             Save the current beam settings and shortest_mom_
476             Necessary because end_beam destroys them
477           */
478           temporary_shortest_mom = shortest_mom_;
479           temporary_beam_settings = beam_settings_;
480
481           /* Eliminate (and save) the items no longer part of the first beam */
482
483           new_grouping_ = grouping_->split_pattern (i);
484           new_stems_ = remove_end_stems (i);
485
486           end_beam ();
487           typeset_beam ();
488
489           /* now recreate the unbeamed data structures */
490           stems_ = new_stems_;
491           grouping_ = new_grouping_;
492           shortest_mom_ = temporary_shortest_mom;
493           beam_settings_ = temporary_beam_settings;
494
495           i = 0;
496         }
497
498     }
499
500 }
501
502 /*
503   Remove all stems with an index greater than split_index
504   from stems_, and return a vector containing all of the
505   removed stems
506 */
507 vector <Item *> *
508 Auto_beam_engraver::remove_end_stems (vsize split_index)
509 {
510   vector <Item *> *removed_stems = 0;
511   removed_stems = new vector <Item *>;
512
513   for (vsize j = split_index + 1; j < stems_->size (); j++)
514     removed_stems->push_back ((*stems_).at (j));
515   for (vsize j = split_index + 1; j < stems_->size ();)
516     stems_->pop_back ();
517   return removed_stems;
518 }
519
520 void
521 Auto_beam_engraver::process_acknowledged ()
522 {
523   Moment now = now_mom ();
524   if (extend_mom_ > now)
525     return;
526
527   if (!process_acknowledged_count_)
528     {
529       Moment measure_now = measure_position (context ());
530       consider_end (measure_now, shortest_mom_);
531     }
532   else if (process_acknowledged_count_ > 1)
533     {
534       if (stems_)
535         {
536           if ((extend_mom_ < now)
537               || ((extend_mom_ == now) && (last_add_mom_ != now)))
538             end_beam ();
539           else if (!stems_->size ())
540             junk_beam ();
541         }
542     }
543
544   process_acknowledged_count_++;
545 }
546
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 ADD_TRANSLATOR (Auto_beam_engraver,
553                 /* doc */
554                 "Generate beams based on measure characteristics and observed"
555                 " Stems.  Uses @code{baseMoment}, @code{beatStructure},"
556                 " @code{beamExceptions}, @code{measureLength}, and"
557                 " @code{measurePosition} to decide when to start and stop a"
558                 " beam.  Overriding beaming is done through"
559                 " @ref{Stem_engraver} properties @code{stemLeftBeamCount} and"
560                 " @code{stemRightBeamCount}.",
561
562                 /* create */
563                 "Beam ",
564
565                 /* read */
566                 "autoBeaming "
567                 "baseMoment "
568                 "beamExceptions "
569                 "beamHalfMeasure "
570                 "beatStructure "
571                 "subdivideBeams ",
572
573                 /* write */
574                 ""
575                );
576
577 class Grace_auto_beam_engraver : public Auto_beam_engraver
578 {
579   TRANSLATOR_DECLARATIONS (Grace_auto_beam_engraver);
580   DECLARE_TRANSLATOR_LISTENER (beam_forbid);
581
582 private:
583   Moment last_grace_start_; // Full starting time of last grace group
584   Moment last_grace_position_; // Measure position of same
585   virtual void process_music ();
586   virtual bool is_same_grace_state (Moment, Moment);
587   virtual bool test_moment (Direction, Moment, Moment);
588 };
589
590 Grace_auto_beam_engraver::Grace_auto_beam_engraver ()
591 {
592   last_grace_start_.main_part_.set_infinite (-1);
593   // grace_part_ is zero -> test_moment is false, last_grace_position_
594   // not considered.
595 }
596
597 IMPLEMENT_TRANSLATOR_LISTENER (Grace_auto_beam_engraver, beam_forbid);
598 void
599 Grace_auto_beam_engraver::listen_beam_forbid (Stream_event *ev)
600 {
601   Auto_beam_engraver::listen_beam_forbid (ev);
602 }
603
604 bool
605 Grace_auto_beam_engraver::is_same_grace_state (Moment, Moment)
606 {
607   // This is for ignoring interspersed grace notes in main note
608   // beaming.  We never want to ignore something inside of grace note
609   // beaming, so return true.
610   return true;
611 }
612
613 void
614 Grace_auto_beam_engraver::process_music ()
615 {
616   Moment now = now_mom ();
617   // Update last_grace_start_ and last_grace_position_ only when the
618   // main time advances.
619   if (now.main_part_ > last_grace_start_.main_part_)
620     {
621       last_grace_start_ = now;
622       last_grace_position_ = measure_position (context ());
623     }
624
625   Auto_beam_engraver::process_music ();
626 }
627
628 bool
629 Grace_auto_beam_engraver::test_moment (Direction dir, Moment test_mom, Moment)
630 {
631   // If no grace group started this main moment, we have no business
632   // beaming.  Same if we have left the original main time step.
633   if (!last_grace_start_.grace_part_
634       || last_grace_position_.main_part_ != test_mom.main_part_)
635     return false;
636   // Autobeam start only when at the start of the grace group.
637   if (dir == START)
638       return last_grace_position_ == test_mom;
639   // Autobeam end only when the grace part is finished.
640   return !test_mom.grace_part_;
641 }
642
643 ADD_ACKNOWLEDGER (Grace_auto_beam_engraver, stem);
644 ADD_ACKNOWLEDGER (Grace_auto_beam_engraver, bar_line);
645 ADD_ACKNOWLEDGER (Grace_auto_beam_engraver, beam);
646 ADD_ACKNOWLEDGER (Grace_auto_beam_engraver, breathing_sign);
647 ADD_ACKNOWLEDGER (Grace_auto_beam_engraver, rest);
648 ADD_TRANSLATOR (Grace_auto_beam_engraver,
649                 /* doc */
650                 "Generates one autobeam group across an entire grace phrase. "
651                 " As usual, any manual beaming or @code{\\noBeam} will block"
652                 " autobeaming, just like setting the context property"
653                 " @samp{autoBeaming} to @code{##f}.",
654
655                 /* create */
656                 "Beam ",
657
658                 /* read */
659                 "autoBeaming ",
660
661                 /* write */
662                 ""
663                );