]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
Merge branch 'translation' into staging
[lilypond.git] / lily / auto-beam-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2012 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_music ();
42   virtual void finalize ();
43   virtual void derived_mark () const;
44
45   DECLARE_ACKNOWLEDGER (rest);
46   DECLARE_ACKNOWLEDGER (beam);
47   DECLARE_ACKNOWLEDGER (bar_line);
48   DECLARE_ACKNOWLEDGER (breathing_sign);
49   DECLARE_ACKNOWLEDGER (stem);
50   DECLARE_TRANSLATOR_LISTENER (beam_forbid);
51
52   void process_acknowledged ();
53
54 private:
55   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   bool is_same_grace_state (Grob *e);
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 void
267 Auto_beam_engraver::end_beam ()
268 {
269   if (stems_->size () < 2)
270     junk_beam ();
271   else
272     {
273       finished_beam_ = create_beam ();
274
275       if (finished_beam_)
276         {
277           Grob_info i = make_grob_info (finished_beam_, SCM_EOL);
278           i.rerouting_daddy_context_ = beam_start_context_.get_context ();
279
280           announce_end_grob (i);
281           finished_grouping_ = grouping_;
282           finished_beaming_options_ = beaming_options_;
283         }
284       delete stems_;
285       stems_ = 0;
286       grouping_ = 0;
287       beam_settings_ = SCM_EOL;
288     }
289
290   beam_start_context_.set_context (NULL);
291   shortest_mom_ = Moment (Rational (1, 4));
292 }
293
294 void
295 Auto_beam_engraver::typeset_beam ()
296 {
297   if (finished_beam_)
298     {
299       if (!finished_beam_->get_bound (RIGHT))
300         finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
301
302       finished_grouping_->beamify (finished_beaming_options_);
303       Beam::set_beaming (finished_beam_, finished_grouping_);
304       finished_beam_ = 0;
305
306       delete finished_grouping_;
307       finished_grouping_ = 0;
308     }
309 }
310
311 void
312 Auto_beam_engraver::stop_translation_timestep ()
313 {
314   typeset_beam ();
315   process_acknowledged_count_ = 0;
316   forbid_ = 0;
317 }
318
319 void
320 Auto_beam_engraver::finalize ()
321 {
322   /* finished beams may be typeset */
323   typeset_beam ();
324   /* but unfinished may need another announce/acknowledge pass */
325   if (stems_)
326     junk_beam ();
327 }
328
329 void
330 Auto_beam_engraver::acknowledge_beam (Grob_info /* info */)
331 {
332   check_bar_property ();
333   if (stems_)
334     end_beam ();
335 }
336
337 void
338 Auto_beam_engraver::acknowledge_bar_line (Grob_info /* info */)
339 {
340   check_bar_property ();
341   if (stems_)
342     end_beam ();
343 }
344
345 void
346 Auto_beam_engraver::acknowledge_breathing_sign (Grob_info /* info */)
347 {
348   check_bar_property ();
349   if (stems_)
350     end_beam ();
351 }
352
353 void
354 Auto_beam_engraver::acknowledge_rest (Grob_info /* info */)
355 {
356   check_bar_property ();
357   if (stems_)
358     end_beam ();
359 }
360
361 void
362 Auto_beam_engraver::acknowledge_stem (Grob_info info)
363 {
364   check_bar_property ();
365   Item *stem = dynamic_cast<Item *> (info.grob ());
366   Stream_event *ev = info.ultimate_event_cause ();
367   if (!ev->in_event_class ("rhythmic-event"))
368     {
369       programming_error ("stem must have rhythmic structure");
370       return;
371     }
372
373   /*
374     Don't (start) auto-beam over empty stems; skips or rests
375   */
376   if (!Stem::head_count (stem))
377     {
378       if (stems_)
379         end_beam ();
380       return;
381     }
382
383   if (Stem::get_beam (stem))
384     {
385       if (stems_)
386         junk_beam ();
387       return;
388     }
389
390   int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
391
392   if (durlog <= 2)
393     {
394       if (stems_)
395         end_beam ();
396       return;
397     }
398
399   /*
400     ignore grace notes.
401   */
402   Moment now = now_mom ();
403   if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
404     return;
405
406   Duration *stem_duration = unsmob_duration (ev->get_property ("duration"));
407   Moment dur = stem_duration->get_length ();
408
409   //Moment dur = unsmob_duration (ev->get_property ("duration"))->get_length ();
410   Moment measure_now = measure_position (context ());
411   bool recheck_needed = false;
412
413   if (dur < shortest_mom_)
414     {
415       /* new shortest moment, so store it and set recheck_needed */
416       shortest_mom_ = dur;
417       recheck_needed = true;
418     }
419
420   /* end should be based on shortest_mom_, begin should be
421      based on current duration  */
422   consider_end (measure_now, shortest_mom_);
423   consider_begin (measure_now, dur);
424
425   if (!stems_)
426     return;
427
428   grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
429                        durlog - 2,
430                        Stem::is_invisible (stem),
431                        stem_duration->factor (),
432                        (stem->get_property ("tuplet-start") == SCM_BOOL_T));
433   stems_->push_back (stem);
434   last_add_mom_ = now;
435   extend_mom_ = max (extend_mom_, now) + get_event_length (ev, now);
436   if (recheck_needed)
437     recheck_beam ();
438 }
439
440 void
441 Auto_beam_engraver::recheck_beam ()
442 {
443   /*
444     Recheck the beam after the shortest duration has changed
445     If shorter duration has created a new break, typeset the
446     first part of the beam and reset the current beam to just
447     the last part of the beam
448   */
449   Beaming_pattern *new_grouping_ = 0;
450   vector<Item *> *new_stems_ = 0;
451   Moment temporary_shortest_mom;
452   SCM temporary_beam_settings;
453
454   bool found_end;
455
456   for (vsize i = 0; i < stems_->size () - 1;)
457     {
458       found_end = test_moment (STOP,
459                                grouping_->end_moment (i),
460                                shortest_mom_);
461       if (!found_end)
462         i++;
463       else
464         {
465           /*
466             Save the current beam settings and shortest_mom_
467             Necessary because end_beam destroys them
468           */
469           temporary_shortest_mom = shortest_mom_;
470           temporary_beam_settings = beam_settings_;
471
472           /* Eliminate (and save) the items no longer part of the first beam */
473
474           new_grouping_ = grouping_->split_pattern (i);
475           new_stems_ = remove_end_stems (i);
476
477           end_beam ();
478           typeset_beam ();
479
480           /* now recreate the unbeamed data structures */
481           stems_ = new_stems_;
482           grouping_ = new_grouping_;
483           shortest_mom_ = temporary_shortest_mom;
484           beam_settings_ = temporary_beam_settings;
485
486           i = 0;
487         }
488
489     }
490
491 }
492
493 /*
494   Remove all stems with an index greater than split_index
495   from stems_, and return a vector containing all of the
496   removed stems
497 */
498 vector <Item *> *
499 Auto_beam_engraver::remove_end_stems (vsize split_index)
500 {
501   vector <Item *> *removed_stems = 0;
502   removed_stems = new vector <Item *>;
503
504   for (vsize j = split_index + 1; j < stems_->size (); j++)
505     removed_stems->push_back ((*stems_).at (j));
506   for (vsize j = split_index + 1; j < stems_->size ();)
507     stems_->pop_back ();
508   return removed_stems;
509 }
510
511 void
512 Auto_beam_engraver::process_acknowledged ()
513 {
514   Moment now = now_mom ();
515   if (extend_mom_ > now)
516     return;
517
518   if (!process_acknowledged_count_)
519     {
520       Moment measure_now = measure_position (context ());
521       consider_end (measure_now, shortest_mom_);
522     }
523   else if (process_acknowledged_count_ > 1)
524     {
525       if (stems_)
526         {
527           if ((extend_mom_ < now)
528               || ((extend_mom_ == now) && (last_add_mom_ != now)))
529             end_beam ();
530           else if (!stems_->size ())
531             junk_beam ();
532         }
533     }
534
535   process_acknowledged_count_++;
536 }
537
538 ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
539 ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
540 ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
541 ADD_ACKNOWLEDGER (Auto_beam_engraver, breathing_sign);
542 ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
543 ADD_TRANSLATOR (Auto_beam_engraver,
544                 /* doc */
545                 "Generate beams based on measure characteristics and observed"
546                 " Stems.  Uses @code{baseMoment}, @code{beatStructure},"
547                 " @code{beamExceptions}, @code{measureLength}, and"
548                 " @code{measurePosition} to decide when to start and stop a"
549                 " beam.  Overriding beaming is done through"
550                 " @ref{Stem_engraver} properties @code{stemLeftBeamCount} and"
551                 " @code{stemRightBeamCount}.",
552
553                 /* create */
554                 "Beam ",
555
556                 /* read */
557                 "autoBeaming "
558                 "baseMoment "
559                 "beamExceptions "
560                 "beamHalfMeasure "
561                 "beatStructure "
562                 "subdivideBeams ",
563
564                 /* write */
565                 ""
566                );