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