2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1999--2011 Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
20 #include "bar-line.hh"
21 #include "beaming-pattern.hh"
24 #include "duration.hh"
25 #include "engraver.hh"
29 #include "stream-event.hh"
33 #include "translator.icc"
35 class Auto_beam_engraver : public Engraver
37 TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
40 void stop_translation_timestep ();
41 void process_music ();
42 virtual void finalize ();
43 virtual void derived_mark () const;
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);
52 void process_acknowledged ();
55 bool test_moment (Direction, Moment, Moment);
56 void consider_begin (Moment, Moment);
57 void consider_end (Moment, Moment);
58 Spanner *create_beam ();
62 bool is_same_grace_state (Grob *e);
65 vector<Item *> *remove_end_stems (vsize);
67 Stream_event *forbid_;
69 shortest_mom_ is the shortest note in the beam.
72 Spanner *finished_beam_;
73 vector<Item *> *stems_;
75 int process_acknowledged_count_;
78 Projected ending of the beam we're working on.
81 Moment beam_start_moment_;
82 Moment beam_start_location_;
83 Context *beam_start_context_;
85 // We act as if beam were created, and start a grouping anyway.
86 Beaming_pattern *grouping_;
89 Beaming_pattern *finished_grouping_;
92 Beaming_options beaming_options_;
93 Beaming_options finished_beaming_options_;
96 void check_bar_property ();
100 Auto_beam_engraver::derived_mark () const
102 scm_gc_mark (beam_settings_);
106 Auto_beam_engraver::check_bar_property ()
108 /* Duplicated from process_music (), since
109 Repeat_acknowledge_engraver::process_music () may also set whichBar. */
111 Moment now = now_mom ();
112 if (scm_is_string (get_property ("whichBar"))
113 && beam_start_moment_ < now)
115 consider_end (measure_position (context ()), shortest_mom_);
121 Auto_beam_engraver::process_music ()
123 Moment now = now_mom ();
125 don't beam over skips
129 if (extend_mom_ < now)
133 if (scm_is_string (get_property ("whichBar")))
135 consider_end (measure_position (context ()), shortest_mom_);
148 Auto_beam_engraver::Auto_beam_engraver ()
151 process_acknowledged_count_ = 0;
153 shortest_mom_ = Moment (Rational (1, 4));
155 finished_grouping_ = 0;
157 beam_settings_ = SCM_EOL;
160 IMPLEMENT_TRANSLATOR_LISTENER (Auto_beam_engraver, beam_forbid);
162 Auto_beam_engraver::listen_beam_forbid (Stream_event *ev)
164 ASSIGN_EVENT_ONCE (forbid_, ev);
168 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom, Moment dur)
170 return scm_call_4 (get_property ("autoBeamCheck"),
171 context ()->self_scm (),
173 test_mom.smobbed_copy (),
179 Auto_beam_engraver::consider_begin (Moment test_mom, Moment dur)
181 bool on = to_boolean (get_property ("autoBeaming"));
185 bool b = test_moment (START, test_mom, dur);
192 Auto_beam_engraver::consider_end (Moment test_mom, Moment dur)
196 /* Allow already started autobeam to end:
197 don't check for autoBeaming */
198 bool b = test_moment (STOP, test_mom, dur);
205 Auto_beam_engraver::create_beam ()
207 if (to_boolean (get_property ("skipTypesetting")))
210 for (vsize i = 0; i < stems_->size (); i++)
211 if (Stem::get_beam ((*stems_)[i]))
215 Can't use make_spanner () because we have to use
218 Spanner *beam = new Spanner (beam_settings_);
220 for (vsize i = 0; i < stems_->size (); i++)
221 Beam::add_stem (beam, (*stems_)[i]);
223 Grob_info i = make_grob_info (beam, (*stems_)[0]->self_scm ());
224 i.rerouting_daddy_context_ = beam_start_context_;
231 Auto_beam_engraver::begin_beam ()
233 if (stems_ || grouping_)
235 programming_error ("already have autobeam");
239 stems_ = new vector<Item *>;
240 grouping_ = new Beaming_pattern ();
241 beaming_options_.from_context (context ());
242 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
244 beam_start_context_ = context ()->get_parent_context ();
245 beam_start_moment_ = now_mom ();
247 = robust_scm2moment (get_property ("measurePosition"), Moment (0));
251 Auto_beam_engraver::junk_beam ()
260 beam_settings_ = SCM_EOL;
262 shortest_mom_ = Moment (Rational (1, 4));
266 Auto_beam_engraver::end_beam ()
268 if (stems_->size () < 2)
272 finished_beam_ = create_beam ();
276 Grob_info i = make_grob_info (finished_beam_, SCM_EOL);
277 i.rerouting_daddy_context_ = beam_start_context_;
279 announce_end_grob (i);
280 finished_grouping_ = grouping_;
281 finished_beaming_options_ = beaming_options_;
286 beam_settings_ = SCM_EOL;
289 shortest_mom_ = Moment (Rational (1, 4));
293 Auto_beam_engraver::typeset_beam ()
297 if (!finished_beam_->get_bound (RIGHT))
298 finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
300 finished_grouping_->beamify (finished_beaming_options_);
301 Beam::set_beaming (finished_beam_, finished_grouping_);
304 delete finished_grouping_;
305 finished_grouping_ = 0;
310 Auto_beam_engraver::stop_translation_timestep ()
313 process_acknowledged_count_ = 0;
318 Auto_beam_engraver::finalize ()
320 /* finished beams may be typeset */
322 /* but unfinished may need another announce/acknowledge pass */
329 Auto_beam_engraver::acknowledge_beam (Grob_info /* info */)
331 check_bar_property ();
337 Auto_beam_engraver::acknowledge_bar_line (Grob_info /* info */)
339 check_bar_property ();
345 Auto_beam_engraver::acknowledge_breathing_sign (Grob_info /* info */)
347 check_bar_property ();
353 Auto_beam_engraver::acknowledge_rest (Grob_info /* info */)
355 check_bar_property ();
361 Auto_beam_engraver::acknowledge_stem (Grob_info info)
363 check_bar_property ();
364 Item *stem = dynamic_cast<Item *> (info.grob ());
365 Stream_event *ev = info.ultimate_event_cause ();
366 if (!ev->in_event_class ("rhythmic-event"))
368 programming_error ("stem must have rhythmic structure");
373 Don't (start) auto-beam over empty stems; skips or rests
375 if (!Stem::head_count (stem))
382 if (Stem::get_beam (stem))
389 int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
401 Moment now = now_mom ();
402 if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
405 Moment dur = unsmob_duration (ev->get_property ("duration"))->get_length ();
406 Moment measure_now = measure_position (context ());
407 bool recheck_needed = false;
409 if (dur < shortest_mom_)
411 /* new shortest moment, so store it and set recheck_needed */
413 recheck_needed = true;
416 /* end should be based on shortest_mom_, begin should be
417 based on current duration */
418 consider_end (measure_now, shortest_mom_);
419 consider_begin (measure_now, dur);
424 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
426 Stem::is_invisible (stem));
427 stems_->push_back (stem);
429 extend_mom_ = max (extend_mom_, now) + get_event_length (ev, now);
435 Auto_beam_engraver::recheck_beam ()
438 Recheck the beam after the shortest duration has changed
439 If shorter duration has created a new break, typeset the
440 first part of the beam and reset the current beam to just
441 the last part of the beam
443 Beaming_pattern *new_grouping_ = 0;
444 vector<Item *> *new_stems_ = 0;
445 Moment temporary_shortest_mom;
446 SCM temporary_beam_settings;
451 for (vsize i = 0; i < stems_->size () - 1;)
453 found_end = test_moment (STOP,
454 grouping_->end_moment (i),
461 Save the current beam settings and shortest_mom_
462 Necessary because end_beam destroys them
464 temporary_shortest_mom = shortest_mom_;
465 temporary_beam_settings = beam_settings_;
467 /* Eliminate (and save) the items no longer part of the first beam */
469 new_grouping_ = grouping_->split_pattern (i);
470 new_stems_ = remove_end_stems (i);
475 /* now recreate the unbeamed data structures */
477 grouping_ = new_grouping_;
478 shortest_mom_ = temporary_shortest_mom;
479 beam_settings_ = temporary_beam_settings;
489 Remove all stems with an index greater than split_index
490 from stems_, and return a vector containing all of the
494 Auto_beam_engraver::remove_end_stems (vsize split_index)
496 vector <Item *> *removed_stems = 0;
497 removed_stems = new vector <Item *>;
499 for (vsize j = split_index + 1; j < stems_->size (); j++)
500 removed_stems->push_back ((*stems_).at (j));
501 for (vsize j = split_index + 1; j < stems_->size ();)
503 return removed_stems;
507 Auto_beam_engraver::process_acknowledged ()
509 Moment now = now_mom();
510 if (extend_mom_ > now)
513 if (!process_acknowledged_count_)
515 Moment measure_now = measure_position (context ());
516 consider_end (measure_now, shortest_mom_);
518 else if (process_acknowledged_count_ > 1)
522 if ((extend_mom_ < now)
523 || ((extend_mom_ == now) && (last_add_mom_ != now)))
525 else if (!stems_->size ())
530 process_acknowledged_count_++;
533 ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
534 ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
535 ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
536 ADD_ACKNOWLEDGER (Auto_beam_engraver, breathing_sign);
537 ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
538 ADD_TRANSLATOR (Auto_beam_engraver,
540 "Generate beams based on measure characteristics and observed"
541 " Stems. Uses @code{baseMoment}, @code{beatStructure},"
542 " @code{beamExceptions}, @code{measureLength}, and"
543 " @code{measurePosition} to decide when to start and stop a"
544 " beam. Overriding beaming is done through"
545 " @ref{Stem_engraver} properties @code{stemLeftBeamCount} and"
546 " @code{stemRightBeamCount}.",