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"
20 class Auto_beam_engraver : public Engraver
22 TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
25 virtual void stop_translation_timestep ();
26 virtual void start_translation_timestep ();
27 virtual void process_music ();
28 virtual bool try_music (Music *);
29 virtual void finalize ();
30 virtual void acknowledge_grob (Grob_info);
31 virtual void process_acknowledged_grobs ();
34 bool test_moment (Direction, Moment);
35 void consider_begin (Moment);
36 void consider_end (Moment);
37 Spanner *create_beam ();
41 bool is_same_grace_state (Grob *e);
46 shortest_mom is the shortest note in the beam.
49 Spanner *finished_beam_;
50 Link_array<Item> *stems_;
55 Projected ending of the beam we're working on.
58 Moment beam_start_moment_;
59 Moment beam_start_location_;
61 bool subdivide_beams_;
64 // We act as if beam were created, and start a grouping anyway.
65 Beaming_info_list *grouping_;
66 SCM beam_settings_; // ugh. should protect ?
68 Beaming_info_list *finished_grouping_;
72 Auto_beam_engraver::process_music ()
74 if (scm_is_string (get_property ("whichBar")))
76 consider_end (shortest_mom_);
82 consider_end (shortest_mom_);
87 Auto_beam_engraver::Auto_beam_engraver ()
92 shortest_mom_ = Moment (Rational (1, 8));
94 finished_grouping_ = 0;
96 beam_settings_ = SCM_EOL;
100 Auto_beam_engraver::try_music (Music *m)
102 if (m->is_mus_type ("beam-forbid-event"))
112 Auto_beam_engraver::test_moment (Direction dir, Moment test)
114 return scm_call_3 (get_property ("autoBeamCheck"),
115 context ()->self_scm (),
117 test.smobbed_copy ())
122 Auto_beam_engraver::consider_begin (Moment test_mom)
124 bool on = to_boolean (get_property ("autoBeaming"));
128 bool b = test_moment (START, test_mom);
135 Auto_beam_engraver::consider_end (Moment test_mom)
139 /* Allow already started autobeam to end:
140 don't check for autoBeaming */
141 bool b = test_moment (STOP, test_mom);
148 Auto_beam_engraver::create_beam ()
150 if (to_boolean (get_property ("skipTypesetting")))
153 Spanner *beam = new Spanner (beam_settings_, context ()->get_grob_key ("Beam"));
154 for (int i = 0; i < stems_->size (); i++)
157 watch out for stem tremolos and abbreviation beams
159 if (Stem::get_beam ((*stems_)[i]))
161 scm_gc_unprotect_object (beam->self_scm ());
164 Beam::add_stem (beam, (*stems_)[i]);
167 announce_grob (beam, (*stems_)[0]->self_scm ());
173 Auto_beam_engraver::begin_beam ()
175 if (stems_ || grouping_)
177 programming_error ("already have autobeam");
181 stems_ = new Link_array<Item>;
182 grouping_ = new Beaming_info_list;
183 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
185 beam_start_moment_ = now_mom ();
187 = robust_scm2moment (get_property ("measurePosition"), Moment (0));
188 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
189 beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
193 Auto_beam_engraver::junk_beam ()
202 beam_settings_ = SCM_EOL;
204 shortest_mom_ = Moment (Rational (1, 8));
208 Auto_beam_engraver::end_beam ()
210 if (stems_->size () < 2)
216 finished_beam_ = create_beam ();
218 finished_grouping_ = grouping_;
222 beam_settings_ = SCM_EOL;
225 shortest_mom_ = Moment (Rational (1, 8));
229 Auto_beam_engraver::typeset_beam ()
233 finished_grouping_->beamify (beat_length_, subdivide_beams_);
234 Beam::set_beaming (finished_beam_, finished_grouping_);
237 delete finished_grouping_;
238 finished_grouping_ = 0;
243 Auto_beam_engraver::start_translation_timestep ()
247 don't beam over skips
251 Moment now = now_mom ();
252 if (extend_mom_ < now)
261 Auto_beam_engraver::stop_translation_timestep ()
267 Auto_beam_engraver::finalize ()
269 /* finished beams may be typeset */
271 /* but unfinished may need another announce/acknowledge pass */
277 Auto_beam_engraver::acknowledge_grob (Grob_info info)
279 /* Duplicated from process_music (), since
280 Repeat_acknowledge_engraver::process_music () may also set whichBar. */
281 if (scm_is_string (get_property ("whichBar"))
282 && beam_start_moment_ < now_mom ())
284 consider_end (shortest_mom_);
290 if (Beam::has_interface (info.grob ()))
294 else if (Bar_line::has_interface (info.grob ()))
298 else if (Rest::has_interface (info.grob ()))
304 if (Stem::has_interface (info.grob ()))
306 Item *stem = dynamic_cast<Item *> (info.grob ());
307 Music *m = info.music_cause ();
308 if (!m->is_mus_type ("rhythmic-event"))
310 programming_error ("stem must have rhythmic structure");
315 Don't (start) auto-beam over empty stems; skips or rests
317 if (!Stem::head_count (stem))
324 if (Stem::get_beam (stem))
331 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
343 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
346 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
349 This comment has been here since long:
351 if shortest duration would change
352 consider ending and beginning beam first.
354 but the code didn't match: */
357 consider_begin (dur);
359 if (dur < shortest_mom_)
362 /* I very much suspect that we wanted: */
364 consider_end (shortest_mom_);
365 if (dur < shortest_mom_)
368 consider_end (shortest_mom_);
370 consider_begin (shortest_mom_);
376 Moment now = now_mom ();
378 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
382 extend_mom_ = max (extend_mom_, now) + m->get_length ();
387 Auto_beam_engraver::process_acknowledged_grobs ()
391 consider_end (shortest_mom_);
392 consider_begin (shortest_mom_);
398 Moment now = now_mom ();
399 if ((extend_mom_ < now)
400 || ((extend_mom_ == now) && (last_add_mom_ != now)))
404 else if (!stems_->size ())
414 ADD_TRANSLATOR (Auto_beam_engraver,
415 /* descr */ "Generate beams based on measure characteristics and observed "
416 "Stems. Uses beatLength, measureLength and measurePosition to decide "
417 "when to start and stop a beam. Overriding beaming is done through "
418 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
419 "@code{stemRightBeamCount}. ",
421 /* accepts */ "beam-forbid-event",
422 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
423 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",