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 PRECOMPUTED_VIRTUAL void stop_translation_timestep ();
30 PRECOMPUTED_VIRTUAL void start_translation_timestep ();
31 PRECOMPUTED_VIRTUAL void process_music ();
32 virtual bool try_music (Music *);
33 virtual void finalize ();
34 virtual void acknowledge_grob (Grob_info);
35 PRECOMPUTED_VIRTUAL void process_acknowledged ();
38 bool test_moment (Direction, Moment);
39 void consider_begin (Moment);
40 void consider_end (Moment);
41 Spanner *create_beam ();
45 bool is_same_grace_state (Grob *e);
50 shortest_mom is the shortest note in the beam.
53 Spanner *finished_beam_;
54 Link_array<Item> *stems_;
56 int process_acknowledged_count_;
59 Projected ending of the beam we're working on.
62 Moment beam_start_moment_;
63 Moment beam_start_location_;
65 bool subdivide_beams_;
68 // We act as if beam were created, and start a grouping anyway.
69 Beaming_info_list *grouping_;
70 SCM beam_settings_; // ugh. should protect ?
72 Beaming_info_list *finished_grouping_;
76 Auto_beam_engraver::process_music ()
78 if (scm_is_string (get_property ("whichBar")))
80 consider_end (shortest_mom_);
86 consider_end (shortest_mom_);
91 Auto_beam_engraver::Auto_beam_engraver ()
94 process_acknowledged_count_ = 0;
96 shortest_mom_ = Moment (Rational (1, 8));
98 finished_grouping_ = 0;
100 beam_settings_ = SCM_EOL;
104 Auto_beam_engraver::try_music (Music *m)
106 if (m->is_mus_type ("beam-forbid-event"))
116 Auto_beam_engraver::test_moment (Direction dir, Moment test)
118 return scm_call_3 (get_property ("autoBeamCheck"),
119 context ()->self_scm (),
121 test.smobbed_copy ())
126 Auto_beam_engraver::consider_begin (Moment test_mom)
128 bool on = to_boolean (get_property ("autoBeaming"));
132 bool b = test_moment (START, test_mom);
139 Auto_beam_engraver::consider_end (Moment test_mom)
143 /* Allow already started autobeam to end:
144 don't check for autoBeaming */
145 bool b = test_moment (STOP, test_mom);
152 Auto_beam_engraver::create_beam ()
154 if (to_boolean (get_property ("skipTypesetting")))
157 Spanner *beam = new Spanner (beam_settings_, context ()->get_grob_key ("Beam"));
158 for (int i = 0; i < stems_->size (); i++)
161 watch out for stem tremolos and abbreviation beams
163 if (Stem::get_beam ((*stems_)[i]))
165 scm_gc_unprotect_object (beam->self_scm ());
168 Beam::add_stem (beam, (*stems_)[i]);
171 announce_grob (beam, (*stems_)[0]->self_scm ());
177 Auto_beam_engraver::begin_beam ()
179 if (stems_ || grouping_)
181 programming_error ("already have autobeam");
185 stems_ = new Link_array<Item>;
186 grouping_ = new Beaming_info_list;
187 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
189 beam_start_moment_ = now_mom ();
191 = robust_scm2moment (get_property ("measurePosition"), Moment (0));
192 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
193 beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
197 Auto_beam_engraver::junk_beam ()
206 beam_settings_ = SCM_EOL;
208 shortest_mom_ = Moment (Rational (1, 8));
212 Auto_beam_engraver::end_beam ()
214 if (stems_->size () < 2)
220 finished_beam_ = create_beam ();
222 finished_grouping_ = grouping_;
226 beam_settings_ = SCM_EOL;
229 shortest_mom_ = Moment (Rational (1, 8));
233 Auto_beam_engraver::typeset_beam ()
237 finished_grouping_->beamify (beat_length_, subdivide_beams_);
238 Beam::set_beaming (finished_beam_, finished_grouping_);
241 delete finished_grouping_;
242 finished_grouping_ = 0;
247 Auto_beam_engraver::start_translation_timestep ()
249 process_acknowledged_count_ = 0;
251 don't beam over skips
255 Moment now = now_mom ();
256 if (extend_mom_ < now)
265 Auto_beam_engraver::stop_translation_timestep ()
271 Auto_beam_engraver::finalize ()
273 /* finished beams may be typeset */
275 /* but unfinished may need another announce/acknowledge pass */
281 Auto_beam_engraver::acknowledge_grob (Grob_info info)
283 /* Duplicated from process_music (), since
284 Repeat_acknowledge_engraver::process_music () may also set whichBar. */
286 Moment now = now_mom ();
287 if (scm_is_string (get_property ("whichBar"))
288 && beam_start_moment_ < now)
290 consider_end (shortest_mom_);
296 if (Beam::has_interface (info.grob ()))
300 else if (Bar_line::has_interface (info.grob ()))
304 else if (Rest::has_interface (info.grob ()))
310 if (Stem::has_interface (info.grob ()))
312 Item *stem = dynamic_cast<Item *> (info.grob ());
313 Music *m = info.music_cause ();
314 if (!m->is_mus_type ("rhythmic-event"))
316 programming_error ("stem must have rhythmic structure");
321 Don't (start) auto-beam over empty stems; skips or rests
323 if (!Stem::head_count (stem))
330 if (Stem::get_beam (stem))
337 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
349 if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
352 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
355 consider_begin (dur);
357 if (dur < shortest_mom_)
363 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
367 extend_mom_ = max (extend_mom_, now) + m->get_length ();
372 Auto_beam_engraver::process_acknowledged ()
374 if (extend_mom_ > now_mom ())
377 if (!process_acknowledged_count_)
379 consider_end (shortest_mom_);
380 consider_begin (shortest_mom_);
382 else if (process_acknowledged_count_ > 1)
386 Moment now = now_mom ();
387 if ((extend_mom_ < now)
388 || ((extend_mom_ == now) && (last_add_mom_ != now)))
392 else if (!stems_->size ())
399 process_acknowledged_count_++;
402 ADD_TRANSLATOR (Auto_beam_engraver,
403 /* descr */ "Generate beams based on measure characteristics and observed "
404 "Stems. Uses beatLength, measureLength and measurePosition to decide "
405 "when to start and stop a beam. Overriding beaming is done through "
406 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
407 "@code{stemRightBeamCount}. ",
409 /* accepts */ "beam-forbid-event",
410 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
411 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",