2 auto-beam-engraver.cc -- implement Auto_beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2006 Jan Nieuwenhuizen <janneke@gnu.org>
14 #include "bar-line.hh"
19 #include "duration.hh"
21 #include "translator.icc"
23 class Auto_beam_engraver : public Engraver
25 TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
28 void stop_translation_timestep ();
29 void start_translation_timestep ();
30 void process_music ();
31 virtual bool try_music (Music *);
32 virtual void finalize ();
33 virtual void derived_mark () const;
35 DECLARE_ACKNOWLEDGER (rest);
36 DECLARE_ACKNOWLEDGER (beam);
37 DECLARE_ACKNOWLEDGER (bar_line);
38 DECLARE_ACKNOWLEDGER (stem);
40 void process_acknowledged ();
43 bool test_moment (Direction, Moment);
44 void consider_begin (Moment);
45 void consider_end (Moment);
46 Spanner *create_beam ();
50 bool is_same_grace_state (Grob *e);
55 shortest_mom is the shortest note in the beam.
58 Spanner *finished_beam_;
59 vector<Item*> *stems_;
61 int process_acknowledged_count_;
64 Projected ending of the beam we're working on.
67 Moment beam_start_moment_;
68 Moment beam_start_location_;
70 bool subdivide_beams_;
73 // We act as if beam were created, and start a grouping anyway.
74 Beaming_info_list *grouping_;
77 Beaming_info_list *finished_grouping_;
79 void check_bar_property ();
83 Auto_beam_engraver::derived_mark () const
85 scm_gc_mark (beam_settings_);
89 Auto_beam_engraver::check_bar_property ()
91 /* Duplicated from process_music (), since
92 Repeat_acknowledge_engraver::process_music () may also set whichBar. */
94 Moment now = now_mom ();
95 if (scm_is_string (get_property ("whichBar"))
96 && beam_start_moment_ < now)
98 consider_end (shortest_mom_);
104 Auto_beam_engraver::process_music ()
106 if (scm_is_string (get_property ("whichBar")))
108 consider_end (shortest_mom_);
114 consider_end (shortest_mom_);
119 Auto_beam_engraver::Auto_beam_engraver ()
122 process_acknowledged_count_ = 0;
124 shortest_mom_ = Moment (Rational (1, 8));
126 finished_grouping_ = 0;
128 beam_settings_ = SCM_EOL;
132 Auto_beam_engraver::try_music (Music *m)
134 if (m->is_mus_type ("beam-forbid-event"))
144 Auto_beam_engraver::test_moment (Direction dir, Moment test)
146 return scm_call_3 (get_property ("autoBeamCheck"),
147 context ()->self_scm (),
149 test.smobbed_copy ())
154 Auto_beam_engraver::consider_begin (Moment test_mom)
156 bool on = to_boolean (get_property ("autoBeaming"));
160 bool b = test_moment (START, test_mom);
167 Auto_beam_engraver::consider_end (Moment test_mom)
171 /* Allow already started autobeam to end:
172 don't check for autoBeaming */
173 bool b = test_moment (STOP, test_mom);
180 Auto_beam_engraver::create_beam ()
182 if (to_boolean (get_property ("skipTypesetting")))
185 for (vsize i = 0; i < stems_->size (); i++)
186 if (Stem::get_beam ((*stems_)[i]))
190 Can't use make_spanner_from_properties() because we have to use
193 Spanner *beam = new Spanner (beam_settings_,
194 context ()->get_grob_key ("Beam"));
196 for (vsize i = 0; i < stems_->size (); i++)
197 Beam::add_stem (beam, (*stems_)[i]);
199 announce_grob (beam, (*stems_)[0]->self_scm ());
205 Auto_beam_engraver::begin_beam ()
207 if (stems_ || grouping_)
209 programming_error ("already have autobeam");
213 stems_ = new vector<Item*>;
214 grouping_ = make_beaming_info_list (context ());
215 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
217 beam_start_moment_ = now_mom ();
219 = robust_scm2moment (get_property ("measurePosition"), Moment (0));
223 Auto_beam_engraver::junk_beam ()
232 beam_settings_ = SCM_EOL;
234 shortest_mom_ = Moment (Rational (1, 8));
238 Auto_beam_engraver::end_beam ()
240 if (stems_->size () < 2)
244 finished_beam_ = create_beam ();
246 finished_grouping_ = grouping_;
250 beam_settings_ = SCM_EOL;
253 shortest_mom_ = Moment (Rational (1, 8));
257 Auto_beam_engraver::typeset_beam ()
261 if (!finished_beam_->get_bound (RIGHT))
262 finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
264 finished_grouping_->beamify ();
265 Beam::set_beaming (finished_beam_, finished_grouping_);
268 delete finished_grouping_;
269 finished_grouping_ = 0;
274 Auto_beam_engraver::start_translation_timestep ()
276 process_acknowledged_count_ = 0;
278 don't beam over skips
282 Moment now = now_mom ();
283 if (extend_mom_ < now)
290 Auto_beam_engraver::stop_translation_timestep ()
296 Auto_beam_engraver::finalize ()
298 /* finished beams may be typeset */
300 /* but unfinished may need another announce/acknowledge pass */
307 Auto_beam_engraver::acknowledge_beam (Grob_info info)
310 check_bar_property ();
316 Auto_beam_engraver::acknowledge_bar_line (Grob_info info)
319 check_bar_property ();
325 Auto_beam_engraver::acknowledge_rest (Grob_info info)
328 check_bar_property ();
334 Auto_beam_engraver::acknowledge_stem (Grob_info info)
336 check_bar_property ();
337 Item *stem = dynamic_cast<Item *> (info.grob ());
338 Music *m = info.ultimate_music_cause ();
339 if (!m->is_mus_type ("rhythmic-event"))
341 programming_error ("stem must have rhythmic structure");
346 Don't (start) auto-beam over empty stems; skips or rests
348 if (!Stem::head_count (stem))
355 if (Stem::get_beam (stem))
362 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
374 Moment now = now_mom ();
375 if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
378 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
381 consider_begin (dur);
383 if (dur < shortest_mom_)
389 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
391 stems_->push_back (stem);
393 extend_mom_ = max (extend_mom_, now) + m->get_length ();
397 Auto_beam_engraver::process_acknowledged ()
399 if (extend_mom_ > now_mom ())
402 if (!process_acknowledged_count_)
404 consider_end (shortest_mom_);
405 consider_begin (shortest_mom_);
407 else if (process_acknowledged_count_ > 1)
411 Moment now = now_mom ();
412 if ((extend_mom_ < now)
413 || ((extend_mom_ == now) && (last_add_mom_ != now)))
415 else if (!stems_->size ())
420 process_acknowledged_count_++;
423 ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
424 ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
425 ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
426 ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
427 ADD_TRANSLATOR (Auto_beam_engraver,
428 /* doc */ "Generate beams based on measure characteristics and observed "
429 "Stems. Uses beatLength, measureLength and measurePosition to decide "
430 "when to start and stop a beam. Overriding beaming is done through "
431 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
432 "@code{stemRightBeamCount}. ",
434 /* accept */ "beam-forbid-event",
435 /* read */ "autoBeaming autoBeamSettings beatLength subdivideBeams",