2 auto-beam-engraver.cc -- implement Auto_beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2007 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "beaming-pattern.hh"
13 #include "duration.hh"
14 #include "engraver.hh"
18 #include "stream-event.hh"
22 #include "translator.icc"
24 class Auto_beam_engraver : public Engraver
26 TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
29 void stop_translation_timestep ();
30 void start_translation_timestep ();
31 void process_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);
39 DECLARE_TRANSLATOR_LISTENER (beam_forbid);
41 void process_acknowledged ();
44 bool test_moment (Direction, Moment);
45 void consider_begin (Moment);
46 void consider_end (Moment);
47 Spanner *create_beam ();
51 bool is_same_grace_state (Grob *e);
54 Stream_event *forbid_;
56 shortest_mom is the shortest note in the beam.
59 Spanner *finished_beam_;
60 vector<Item*> *stems_;
62 int process_acknowledged_count_;
65 Projected ending of the beam we're working on.
68 Moment beam_start_moment_;
69 Moment beam_start_location_;
71 // We act as if beam were created, and start a grouping anyway.
72 Beaming_pattern *grouping_;
75 Beaming_pattern *finished_grouping_;
78 Beaming_options beaming_options_;
79 Beaming_options finished_beaming_options_;
82 void check_bar_property ();
86 Auto_beam_engraver::derived_mark () const
88 scm_gc_mark (beam_settings_);
92 Auto_beam_engraver::check_bar_property ()
94 /* Duplicated from process_music (), since
95 Repeat_acknowledge_engraver::process_music () may also set whichBar. */
97 Moment now = now_mom ();
98 if (scm_is_string (get_property ("whichBar"))
99 && beam_start_moment_ < now)
101 consider_end (shortest_mom_);
107 Auto_beam_engraver::process_music ()
109 if (scm_is_string (get_property ("whichBar")))
111 consider_end (shortest_mom_);
117 consider_end (shortest_mom_);
122 Auto_beam_engraver::Auto_beam_engraver ()
125 process_acknowledged_count_ = 0;
127 shortest_mom_ = Moment (Rational (1, 8));
129 finished_grouping_ = 0;
131 beam_settings_ = SCM_EOL;
134 IMPLEMENT_TRANSLATOR_LISTENER (Auto_beam_engraver, beam_forbid);
136 Auto_beam_engraver::listen_beam_forbid (Stream_event *ev)
138 ASSIGN_EVENT_ONCE (forbid_, ev);
142 Auto_beam_engraver::test_moment (Direction dir, Moment test)
144 return scm_call_3 (get_property ("autoBeamCheck"),
145 context ()->self_scm (),
147 test.smobbed_copy ())
152 Auto_beam_engraver::consider_begin (Moment test_mom)
154 bool on = to_boolean (get_property ("autoBeaming"));
158 bool b = test_moment (START, test_mom);
165 Auto_beam_engraver::consider_end (Moment test_mom)
169 /* Allow already started autobeam to end:
170 don't check for autoBeaming */
171 bool b = test_moment (STOP, test_mom);
178 Auto_beam_engraver::create_beam ()
180 if (to_boolean (get_property ("skipTypesetting")))
183 for (vsize i = 0; i < stems_->size (); i++)
184 if (Stem::get_beam ((*stems_)[i]))
188 Can't use make_spanner_from_properties() because we have to use
191 Spanner *beam = new Spanner (beam_settings_);
193 for (vsize i = 0; i < stems_->size (); i++)
194 Beam::add_stem (beam, (*stems_)[i]);
196 announce_grob (beam, (*stems_)[0]->self_scm ());
202 Auto_beam_engraver::begin_beam ()
204 if (stems_ || grouping_)
206 programming_error ("already have autobeam");
210 stems_ = new vector<Item*>;
211 grouping_ = new Beaming_pattern ();
212 beaming_options_.from_context (context ());
213 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
215 beam_start_moment_ = now_mom ();
217 = robust_scm2moment (get_property ("measurePosition"), Moment (0));
221 Auto_beam_engraver::junk_beam ()
230 beam_settings_ = SCM_EOL;
232 shortest_mom_ = Moment (Rational (1, 8));
236 Auto_beam_engraver::end_beam ()
238 if (stems_->size () < 2)
242 finished_beam_ = create_beam ();
246 announce_end_grob (finished_beam_, SCM_EOL);
247 finished_grouping_ = grouping_;
248 finished_beaming_options_ = beaming_options_;
253 beam_settings_ = SCM_EOL;
256 shortest_mom_ = Moment (Rational (1, 8));
260 Auto_beam_engraver::typeset_beam ()
264 if (!finished_beam_->get_bound (RIGHT))
265 finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
267 finished_grouping_->beamify (finished_beaming_options_);
268 Beam::set_beaming (finished_beam_, finished_grouping_);
271 delete finished_grouping_;
272 finished_grouping_ = 0;
277 Auto_beam_engraver::start_translation_timestep ()
279 process_acknowledged_count_ = 0;
281 don't beam over skips
285 Moment now = now_mom ();
286 if (extend_mom_ < now)
293 Auto_beam_engraver::stop_translation_timestep ()
299 Auto_beam_engraver::finalize ()
301 /* finished beams may be typeset */
303 /* but unfinished may need another announce/acknowledge pass */
310 Auto_beam_engraver::acknowledge_beam (Grob_info info)
313 check_bar_property ();
319 Auto_beam_engraver::acknowledge_bar_line (Grob_info info)
322 check_bar_property ();
328 Auto_beam_engraver::acknowledge_rest (Grob_info info)
331 check_bar_property ();
337 Auto_beam_engraver::acknowledge_stem (Grob_info info)
339 check_bar_property ();
340 Item *stem = dynamic_cast<Item *> (info.grob ());
341 Stream_event *ev = info.ultimate_event_cause ();
342 if (!ev->in_event_class ("rhythmic-event"))
344 programming_error ("stem must have rhythmic structure");
349 Don't (start) auto-beam over empty stems; skips or rests
351 if (!Stem::head_count (stem))
358 if (Stem::get_beam (stem))
365 int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
377 Moment now = now_mom ();
378 if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
381 Moment dur = unsmob_duration (ev->get_property ("duration"))->get_length ();
384 consider_begin (dur);
386 if (dur < shortest_mom_)
392 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
394 stems_->push_back (stem);
396 extend_mom_ = max (extend_mom_, now) + get_event_length (ev);
400 Auto_beam_engraver::process_acknowledged ()
402 if (extend_mom_ > now_mom ())
405 if (!process_acknowledged_count_)
407 consider_end (shortest_mom_);
408 consider_begin (shortest_mom_);
410 else if (process_acknowledged_count_ > 1)
414 Moment now = now_mom ();
415 if ((extend_mom_ < now)
416 || ((extend_mom_ == now) && (last_add_mom_ != now)))
418 else if (!stems_->size ())
423 process_acknowledged_count_++;
426 ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
427 ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
428 ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
429 ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
430 ADD_TRANSLATOR (Auto_beam_engraver,
431 /* doc */ "Generate beams based on measure characteristics and observed "
432 "Stems. Uses beatLength, measureLength and measurePosition to decide "
433 "when to start and stop a beam. Overriding beaming is done through "
434 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
435 "@code{stemRightBeamCount}. ",