2 auto-beam-engraver.cc -- implement Auto_beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2005 Jan Nieuwenhuizen <janneke@gnu.org>
14 #include "bar-line.hh"
19 #include "duration.hh"
21 #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 bool try_music (Music *);
33 virtual void finalize ();
34 virtual void derived_mark () const;
36 DECLARE_ACKNOWLEDGER (rest);
37 DECLARE_ACKNOWLEDGER (beam);
38 DECLARE_ACKNOWLEDGER (bar_line);
39 DECLARE_ACKNOWLEDGER (stem);
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);
56 shortest_mom is the shortest note in the beam.
59 Spanner *finished_beam_;
60 Link_array<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 bool subdivide_beams_;
74 // We act as if beam were created, and start a grouping anyway.
75 Beaming_info_list *grouping_;
78 Beaming_info_list *finished_grouping_;
80 void check_bar_property ();
85 Auto_beam_engraver::derived_mark () const
87 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;
135 Auto_beam_engraver::try_music (Music *m)
137 if (m->is_mus_type ("beam-forbid-event"))
147 Auto_beam_engraver::test_moment (Direction dir, Moment test)
149 return scm_call_3 (get_property ("autoBeamCheck"),
150 context ()->self_scm (),
152 test.smobbed_copy ())
157 Auto_beam_engraver::consider_begin (Moment test_mom)
159 bool on = to_boolean (get_property ("autoBeaming"));
163 bool b = test_moment (START, test_mom);
170 Auto_beam_engraver::consider_end (Moment test_mom)
174 /* Allow already started autobeam to end:
175 don't check for autoBeaming */
176 bool b = test_moment (STOP, test_mom);
183 Auto_beam_engraver::create_beam ()
185 if (to_boolean (get_property ("skipTypesetting")))
188 for (int i = 0; i < stems_->size (); i++)
189 if (Stem::get_beam ((*stems_)[i]))
193 Can't use make_spanner_from_properties() because we have to use
196 Spanner *beam = new Spanner (beam_settings_,
197 context ()->get_grob_key ("Beam"));
199 for (int i = 0; i < stems_->size (); i++)
200 Beam::add_stem (beam, (*stems_)[i]);
202 announce_grob (beam, (*stems_)[0]->self_scm ());
208 Auto_beam_engraver::begin_beam ()
210 if (stems_ || grouping_)
212 programming_error ("already have autobeam");
216 stems_ = new Link_array<Item>;
217 grouping_ = new Beaming_info_list;
218 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
220 beam_start_moment_ = now_mom ();
222 = robust_scm2moment (get_property ("measurePosition"), Moment (0));
223 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
224 beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
228 Auto_beam_engraver::junk_beam ()
237 beam_settings_ = SCM_EOL;
239 shortest_mom_ = Moment (Rational (1, 8));
243 Auto_beam_engraver::end_beam ()
245 if (stems_->size () < 2)
251 finished_beam_ = create_beam ();
253 finished_grouping_ = grouping_;
257 beam_settings_ = SCM_EOL;
260 shortest_mom_ = Moment (Rational (1, 8));
264 Auto_beam_engraver::typeset_beam ()
268 finished_grouping_->beamify (beat_length_, subdivide_beams_);
269 Beam::set_beaming (finished_beam_, finished_grouping_);
272 delete finished_grouping_;
273 finished_grouping_ = 0;
278 Auto_beam_engraver::start_translation_timestep ()
280 process_acknowledged_count_ = 0;
282 don't beam over skips
286 Moment now = now_mom ();
287 if (extend_mom_ < now)
296 Auto_beam_engraver::stop_translation_timestep ()
302 Auto_beam_engraver::finalize ()
304 /* finished beams may be typeset */
306 /* but unfinished may need another announce/acknowledge pass */
315 Auto_beam_engraver::acknowledge_beam (Grob_info info)
318 check_bar_property ();
326 Auto_beam_engraver::acknowledge_bar_line (Grob_info info)
329 check_bar_property ();
335 Auto_beam_engraver::acknowledge_rest (Grob_info info)
338 check_bar_property ();
344 Auto_beam_engraver::acknowledge_stem (Grob_info info)
346 check_bar_property ();
347 Item *stem = dynamic_cast<Item *> (info.grob ());
348 Music *m = info.music_cause ();
349 if (!m->is_mus_type ("rhythmic-event"))
351 programming_error ("stem must have rhythmic structure");
356 Don't (start) auto-beam over empty stems; skips or rests
358 if (!Stem::head_count (stem))
365 if (Stem::get_beam (stem))
372 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
384 Moment now = now_mom ();
385 if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
388 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
391 consider_begin (dur);
393 if (dur < shortest_mom_)
399 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
403 extend_mom_ = max (extend_mom_, now) + m->get_length ();
407 Auto_beam_engraver::process_acknowledged ()
409 if (extend_mom_ > now_mom ())
412 if (!process_acknowledged_count_)
414 consider_end (shortest_mom_);
415 consider_begin (shortest_mom_);
417 else if (process_acknowledged_count_ > 1)
421 Moment now = now_mom ();
422 if ((extend_mom_ < now)
423 || ((extend_mom_ == now) && (last_add_mom_ != now)))
427 else if (!stems_->size ())
434 process_acknowledged_count_++;
437 ADD_ACKNOWLEDGER (Auto_beam_engraver,stem);
438 ADD_ACKNOWLEDGER (Auto_beam_engraver,bar_line);
439 ADD_ACKNOWLEDGER (Auto_beam_engraver,beam);
440 ADD_ACKNOWLEDGER (Auto_beam_engraver,rest);
441 ADD_TRANSLATOR (Auto_beam_engraver,
442 /* doc */ "Generate beams based on measure characteristics and observed "
443 "Stems. Uses beatLength, measureLength and measurePosition to decide "
444 "when to start and stop a beam. Overriding beaming is done through "
445 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
446 "@code{stemRightBeamCount}. ",
448 /* accept */ "beam-forbid-event",
449 /* read */ "autoBeaming autoBeamSettings beatLength subdivideBeams",