]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
Run grand-replace for 2012
[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 "bar-line.hh"
21 #include "beaming-pattern.hh"
22 #include "beam.hh"
23 #include "context.hh"
24 #include "context-handle.hh"
25 #include "duration.hh"
26 #include "engraver.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_music ();
43   virtual void finalize ();
44   virtual void derived_mark () const;
45
46   DECLARE_ACKNOWLEDGER (rest);
47   DECLARE_ACKNOWLEDGER (beam);
48   DECLARE_ACKNOWLEDGER (bar_line);
49   DECLARE_ACKNOWLEDGER (breathing_sign);
50   DECLARE_ACKNOWLEDGER (stem);
51   DECLARE_TRANSLATOR_LISTENER (beam_forbid);
52
53   void process_acknowledged ();
54
55 private:
56   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   bool is_same_grace_state (Grob *e);
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   finished_beam_ = 0;
157   finished_grouping_ = 0;
158   grouping_ = 0;
159   beam_settings_ = SCM_EOL;
160 }
161
162 IMPLEMENT_TRANSLATOR_LISTENER (Auto_beam_engraver, beam_forbid);
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_call_4 (get_property ("autoBeamCheck"),
173                      context ()->self_scm (),
174                      scm_from_int (dir),
175                      test_mom.smobbed_copy (),
176                      dur.smobbed_copy ())
177          != SCM_BOOL_F;
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_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
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 void
268 Auto_beam_engraver::end_beam ()
269 {
270   if (stems_->size () < 2)
271     junk_beam ();
272   else
273     {
274       finished_beam_ = create_beam ();
275
276       if (finished_beam_)
277         {
278           Grob_info i = make_grob_info (finished_beam_, SCM_EOL);
279           i.rerouting_daddy_context_ = beam_start_context_.get_context ();
280
281           announce_end_grob (i);
282           finished_grouping_ = grouping_;
283           finished_beaming_options_ = beaming_options_;
284         }
285       delete stems_;
286       stems_ = 0;
287       grouping_ = 0;
288       beam_settings_ = SCM_EOL;
289     }
290
291   beam_start_context_.set_context (NULL);
292   shortest_mom_ = Moment (Rational (1, 4));
293 }
294
295 void
296 Auto_beam_engraver::typeset_beam ()
297 {
298   if (finished_beam_)
299     {
300       if (!finished_beam_->get_bound (RIGHT))
301         finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
302
303       finished_grouping_->beamify (finished_beaming_options_);
304       Beam::set_beaming (finished_beam_, finished_grouping_);
305       finished_beam_ = 0;
306
307       delete finished_grouping_;
308       finished_grouping_ = 0;
309     }
310 }
311
312 void
313 Auto_beam_engraver::stop_translation_timestep ()
314 {
315   typeset_beam ();
316   process_acknowledged_count_ = 0;
317   forbid_ = 0;
318 }
319
320 void
321 Auto_beam_engraver::finalize ()
322 {
323   /* finished beams may be typeset */
324   typeset_beam ();
325   /* but unfinished may need another announce/acknowledge pass */
326   if (stems_)
327     junk_beam ();
328 }
329
330 void
331 Auto_beam_engraver::acknowledge_beam (Grob_info /* info */)
332 {
333   check_bar_property ();
334   if (stems_)
335     end_beam ();
336 }
337
338 void
339 Auto_beam_engraver::acknowledge_bar_line (Grob_info /* info */)
340 {
341   check_bar_property ();
342   if (stems_)
343     end_beam ();
344 }
345
346 void
347 Auto_beam_engraver::acknowledge_breathing_sign (Grob_info /* info */)
348 {
349   check_bar_property ();
350   if (stems_)
351     end_beam ();
352 }
353
354 void
355 Auto_beam_engraver::acknowledge_rest (Grob_info /* info */)
356 {
357   check_bar_property ();
358   if (stems_)
359     end_beam ();
360 }
361
362 void
363 Auto_beam_engraver::acknowledge_stem (Grob_info info)
364 {
365   check_bar_property ();
366   Item *stem = dynamic_cast<Item *> (info.grob ());
367   Stream_event *ev = info.ultimate_event_cause ();
368   if (!ev->in_event_class ("rhythmic-event"))
369     {
370       programming_error ("stem must have rhythmic structure");
371       return;
372     }
373
374   /*
375     Don't (start) auto-beam over empty stems; skips or rests
376   */
377   if (!Stem::head_count (stem))
378     {
379       if (stems_)
380         end_beam ();
381       return;
382     }
383
384   if (Stem::get_beam (stem))
385     {
386       if (stems_)
387         junk_beam ();
388       return;
389     }
390
391   int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
392
393   if (durlog <= 2)
394     {
395       if (stems_)
396         end_beam ();
397       return;
398     }
399
400   /*
401     ignore grace notes.
402   */
403   Moment now = now_mom ();
404   if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
405     return;
406
407   Duration *stem_duration = unsmob_duration (ev->get_property ("duration"));
408   Moment dur = stem_duration->get_length ();
409
410   //Moment dur = unsmob_duration (ev->get_property ("duration"))->get_length ();
411   Moment measure_now = measure_position (context ());
412   bool recheck_needed = false;
413
414   if (dur < shortest_mom_)
415     {
416       /* new shortest moment, so store it and set recheck_needed */
417       shortest_mom_ = dur;
418       recheck_needed = true;
419     }
420
421   /* end should be based on shortest_mom_, begin should be
422      based on current duration  */
423   consider_end (measure_now, shortest_mom_);
424   consider_begin (measure_now, dur);
425
426   if (!stems_)
427     return;
428
429   grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
430                        durlog - 2,
431                        Stem::is_invisible (stem),
432                        stem_duration->factor ());
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                 "beatStructure "
561                 "subdivideBeams ",
562
563                 /* write */
564                 ""
565                );