]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / auto-beam-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2011 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 "bar-line.hh"
21 #include "beaming-pattern.hh"
22 #include "beam.hh"
23 #include "context.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   Context *beam_start_context_;
84
85   // We act as if beam were created, and start a grouping anyway.
86   Beaming_pattern *grouping_;
87   SCM beam_settings_;
88
89   Beaming_pattern *finished_grouping_;
90
91   Beaming_options beaming_options_;
92   Beaming_options finished_beaming_options_;
93
94   void check_bar_property ();
95 };
96
97 void
98 Auto_beam_engraver::derived_mark () const
99 {
100   scm_gc_mark (beam_settings_);
101 }
102
103 void
104 Auto_beam_engraver::check_bar_property ()
105 {
106   /* Duplicated from process_music (), since
107      Repeat_acknowledge_engraver::process_music () may also set whichBar.  */
108
109   Moment now = now_mom ();
110   if (scm_is_string (get_property ("whichBar"))
111       && beam_start_moment_ < now)
112     {
113       consider_end (measure_position (context ()), shortest_mom_);
114       junk_beam ();
115     }
116 }
117
118 void
119 Auto_beam_engraver::process_music ()
120 {
121   Moment now = now_mom ();
122   /*
123     don't beam over skips
124   */
125   if (stems_)
126     {
127       if (extend_mom_ < now)
128         end_beam ();
129     }
130
131   if (scm_is_string (get_property ("whichBar")))
132     {
133       consider_end (measure_position (context ()), shortest_mom_);
134       junk_beam ();
135     }
136
137   if (forbid_)
138     {
139       if (stems_)
140         end_beam ();
141       else
142         junk_beam ();
143     }
144 }
145
146 Auto_beam_engraver::Auto_beam_engraver ()
147 {
148   forbid_ = 0;
149   process_acknowledged_count_ = 0;
150   stems_ = 0;
151   shortest_mom_ = Moment (Rational (1, 4));
152   finished_beam_ = 0;
153   finished_grouping_ = 0;
154   grouping_ = 0;
155   beam_settings_ = SCM_EOL;
156 }
157
158 IMPLEMENT_TRANSLATOR_LISTENER (Auto_beam_engraver, beam_forbid);
159 void
160 Auto_beam_engraver::listen_beam_forbid (Stream_event *ev)
161 {
162   ASSIGN_EVENT_ONCE (forbid_, ev);
163 }
164
165 bool
166 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom, Moment dur)
167 {
168   return scm_call_4 (get_property ("autoBeamCheck"),
169                      context ()->self_scm (),
170                      scm_from_int (dir),
171                      test_mom.smobbed_copy (),
172                      dur.smobbed_copy ())
173          != SCM_BOOL_F;
174 }
175
176 void
177 Auto_beam_engraver::consider_begin (Moment test_mom, Moment dur)
178 {
179   bool on = to_boolean (get_property ("autoBeaming"));
180   if (!stems_ && on
181       && !forbid_)
182     {
183       bool b = test_moment (START, test_mom, dur);
184       if (b)
185         begin_beam ();
186     }
187 }
188
189 void
190 Auto_beam_engraver::consider_end (Moment test_mom, Moment dur)
191 {
192   if (stems_)
193     {
194       /* Allow already started autobeam to end:
195          don't check for autoBeaming */
196       bool b = test_moment (STOP, test_mom, dur);
197       if (b)
198         end_beam ();
199     }
200 }
201
202 Spanner *
203 Auto_beam_engraver::create_beam ()
204 {
205   if (to_boolean (get_property ("skipTypesetting")))
206     return 0;
207
208   for (vsize i = 0; i < stems_->size (); i++)
209     if (Stem::get_beam ((*stems_)[i]))
210       return 0;
211
212   /*
213     Can't use make_spanner () because we have to use
214     beam_settings_.
215   */
216   Spanner *beam = new Spanner (beam_settings_);
217
218   for (vsize i = 0; i < stems_->size (); i++)
219     Beam::add_stem (beam, (*stems_)[i]);
220
221   Grob_info i = make_grob_info (beam, (*stems_)[0]->self_scm ());
222   i.rerouting_daddy_context_ = beam_start_context_;
223   announce_grob (i);
224
225   return beam;
226 }
227
228 void
229 Auto_beam_engraver::begin_beam ()
230 {
231   if (stems_ || grouping_)
232     {
233       programming_error ("already have autobeam");
234       return;
235     }
236
237   stems_ = new vector<Item *>;
238   grouping_ = new Beaming_pattern ();
239   beaming_options_.from_context (context ());
240   beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
241
242   beam_start_context_ = context ()->get_parent_context ();
243   beam_start_moment_ = now_mom ();
244   beam_start_location_
245     = robust_scm2moment (get_property ("measurePosition"), Moment (0));
246 }
247
248 void
249 Auto_beam_engraver::junk_beam ()
250 {
251   if (!stems_)
252     return;
253
254   delete stems_;
255   stems_ = 0;
256   delete grouping_;
257   grouping_ = 0;
258   beam_settings_ = SCM_EOL;
259
260   shortest_mom_ = Moment (Rational (1, 4));
261 }
262
263 void
264 Auto_beam_engraver::end_beam ()
265 {
266   if (stems_->size () < 2)
267     junk_beam ();
268   else
269     {
270       finished_beam_ = create_beam ();
271
272       if (finished_beam_)
273         {
274           Grob_info i = make_grob_info (finished_beam_, SCM_EOL);
275           i.rerouting_daddy_context_ = beam_start_context_;
276
277           announce_end_grob (i);
278           finished_grouping_ = grouping_;
279           finished_beaming_options_ = beaming_options_;
280         }
281       delete stems_;
282       stems_ = 0;
283       grouping_ = 0;
284       beam_settings_ = SCM_EOL;
285     }
286
287   shortest_mom_ = Moment (Rational (1, 4));
288 }
289
290 void
291 Auto_beam_engraver::typeset_beam ()
292 {
293   if (finished_beam_)
294     {
295       if (!finished_beam_->get_bound (RIGHT))
296         finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
297
298       finished_grouping_->beamify (finished_beaming_options_);
299       Beam::set_beaming (finished_beam_, finished_grouping_);
300       finished_beam_ = 0;
301
302       delete finished_grouping_;
303       finished_grouping_ = 0;
304     }
305 }
306
307 void
308 Auto_beam_engraver::stop_translation_timestep ()
309 {
310   typeset_beam ();
311   process_acknowledged_count_ = 0;
312   forbid_ = 0;
313 }
314
315 void
316 Auto_beam_engraver::finalize ()
317 {
318   /* finished beams may be typeset */
319   typeset_beam ();
320   /* but unfinished may need another announce/acknowledge pass */
321   if (stems_)
322     junk_beam ();
323 }
324
325 void
326 Auto_beam_engraver::acknowledge_beam (Grob_info /* info */)
327 {
328   check_bar_property ();
329   if (stems_)
330     end_beam ();
331 }
332
333 void
334 Auto_beam_engraver::acknowledge_bar_line (Grob_info /* info */)
335 {
336   check_bar_property ();
337   if (stems_)
338     end_beam ();
339 }
340
341 void
342 Auto_beam_engraver::acknowledge_breathing_sign (Grob_info /* info */)
343 {
344   check_bar_property ();
345   if (stems_)
346     end_beam ();
347 }
348
349 void
350 Auto_beam_engraver::acknowledge_rest (Grob_info /* info */)
351 {
352   check_bar_property ();
353   if (stems_)
354     end_beam ();
355 }
356
357 void
358 Auto_beam_engraver::acknowledge_stem (Grob_info info)
359 {
360   check_bar_property ();
361   Item *stem = dynamic_cast<Item *> (info.grob ());
362   Stream_event *ev = info.ultimate_event_cause ();
363   if (!ev->in_event_class ("rhythmic-event"))
364     {
365       programming_error ("stem must have rhythmic structure");
366       return;
367     }
368
369   /*
370     Don't (start) auto-beam over empty stems; skips or rests
371   */
372   if (!Stem::head_count (stem))
373     {
374       if (stems_)
375         end_beam ();
376       return;
377     }
378
379   if (Stem::get_beam (stem))
380     {
381       if (stems_)
382         junk_beam ();
383       return;
384     }
385
386   int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
387
388   if (durlog <= 2)
389     {
390       if (stems_)
391         end_beam ();
392       return;
393     }
394
395   /*
396     ignore grace notes.
397   */
398   Moment now = now_mom ();
399   if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
400     return;
401
402   Moment dur = unsmob_duration (ev->get_property ("duration"))->get_length ();
403   Moment measure_now = measure_position (context ());
404   bool recheck_needed = false;
405
406   if (dur < shortest_mom_)
407     {
408       /* new shortest moment, so store it and set recheck_needed */
409       shortest_mom_ = dur;
410       recheck_needed = true;
411     }
412
413   /* end should be based on shortest_mom_, begin should be
414      based on current duration  */
415   consider_end (measure_now, shortest_mom_);
416   consider_begin (measure_now, dur);
417
418   if (!stems_)
419     return;
420
421   grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
422                        durlog - 2,
423                        Stem::is_invisible (stem));
424   stems_->push_back (stem);
425   last_add_mom_ = now;
426   extend_mom_ = max (extend_mom_, now) + get_event_length (ev, now);
427   if (recheck_needed)
428     recheck_beam ();
429 }
430
431 void
432 Auto_beam_engraver::recheck_beam ()
433 {
434   /*
435     Recheck the beam after the shortest duration has changed
436     If shorter duration has created a new break, typeset the
437     first part of the beam and reset the current beam to just
438     the last part of the beam
439   */
440   Beaming_pattern *new_grouping_ = 0;
441   vector<Item *> *new_stems_ = 0;
442   Moment temporary_shortest_mom;
443   SCM temporary_beam_settings;
444
445   bool found_end;
446
447   for (vsize i = 0; i < stems_->size () - 1;)
448     {
449       found_end = test_moment (STOP,
450                                grouping_->end_moment (i),
451                                shortest_mom_);
452       if (!found_end)
453         i++;
454       else
455         {
456           /*
457             Save the current beam settings and shortest_mom_
458             Necessary because end_beam destroys them
459           */
460           temporary_shortest_mom = shortest_mom_;
461           temporary_beam_settings = beam_settings_;
462
463           /* Eliminate (and save) the items no longer part of the first beam */
464
465           new_grouping_ = grouping_->split_pattern (i);
466           new_stems_ = remove_end_stems (i);
467
468           end_beam ();
469           typeset_beam ();
470
471           /* now recreate the unbeamed data structures */
472           stems_ = new_stems_;
473           grouping_ = new_grouping_;
474           shortest_mom_ = temporary_shortest_mom;
475           beam_settings_ = temporary_beam_settings;
476
477           i = 0;
478         }
479
480     }
481
482 }
483
484 /*
485   Remove all stems with an index greater than split_index
486   from stems_, and return a vector containing all of the
487   removed stems
488 */
489 vector <Item *> *
490 Auto_beam_engraver::remove_end_stems (vsize split_index)
491 {
492   vector <Item *> *removed_stems = 0;
493   removed_stems = new vector <Item *>;
494
495   for (vsize j = split_index + 1; j < stems_->size (); j++)
496     removed_stems->push_back ((*stems_).at (j));
497   for (vsize j = split_index + 1; j < stems_->size ();)
498     stems_->pop_back ();
499   return removed_stems;
500 }
501
502 void
503 Auto_beam_engraver::process_acknowledged ()
504 {
505   Moment now = now_mom ();
506   if (extend_mom_ > now)
507     return;
508
509   if (!process_acknowledged_count_)
510     {
511       Moment measure_now = measure_position (context ());
512       consider_end (measure_now, shortest_mom_);
513     }
514   else if (process_acknowledged_count_ > 1)
515     {
516       if (stems_)
517         {
518           if ((extend_mom_ < now)
519               || ((extend_mom_ == now) && (last_add_mom_ != now)))
520             end_beam ();
521           else if (!stems_->size ())
522             junk_beam ();
523         }
524     }
525
526   process_acknowledged_count_++;
527 }
528
529 ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
530 ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
531 ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
532 ADD_ACKNOWLEDGER (Auto_beam_engraver, breathing_sign);
533 ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
534 ADD_TRANSLATOR (Auto_beam_engraver,
535                 /* doc */
536                 "Generate beams based on measure characteristics and observed"
537                 " Stems.  Uses @code{baseMoment}, @code{beatStructure},"
538                 " @code{beamExceptions}, @code{measureLength}, and"
539                 " @code{measurePosition} to decide when to start and stop a"
540                 " beam.  Overriding beaming is done through"
541                 " @ref{Stem_engraver} properties @code{stemLeftBeamCount} and"
542                 " @code{stemRightBeamCount}.",
543
544                 /* create */
545                 "Beam ",
546
547                 /* read */
548                 "autoBeaming "
549                 "baseMoment "
550                 "beamExceptions "
551                 "beatStructure "
552                 "subdivideBeams ",
553
554                 /* write */
555                 ""
556                );