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 ();
35 DECLARE_ACKNOWLEDGER(rest);
36 DECLARE_ACKNOWLEDGER(beam);
37 DECLARE_ACKNOWLEDGER(bar_line);
38 DECLARE_ACKNOWLEDGER(stem);
40 PRECOMPUTED_VIRTUAL 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 Link_array<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_;
75 SCM beam_settings_; // ugh. should protect ?
77 Beaming_info_list *finished_grouping_;
79 void check_bar_property ();
84 Auto_beam_engraver::check_bar_property ()
86 /* Duplicated from process_music (), since
87 Repeat_acknowledge_engraver::process_music () may also set whichBar. */
89 Moment now = now_mom ();
90 if (scm_is_string (get_property ("whichBar"))
91 && beam_start_moment_ < now)
93 consider_end (shortest_mom_);
99 Auto_beam_engraver::process_music ()
101 if (scm_is_string (get_property ("whichBar")))
103 consider_end (shortest_mom_);
109 consider_end (shortest_mom_);
114 Auto_beam_engraver::Auto_beam_engraver ()
117 process_acknowledged_count_ = 0;
119 shortest_mom_ = Moment (Rational (1, 8));
121 finished_grouping_ = 0;
123 beam_settings_ = SCM_EOL;
127 Auto_beam_engraver::try_music (Music *m)
129 if (m->is_mus_type ("beam-forbid-event"))
139 Auto_beam_engraver::test_moment (Direction dir, Moment test)
141 return scm_call_3 (get_property ("autoBeamCheck"),
142 context ()->self_scm (),
144 test.smobbed_copy ())
149 Auto_beam_engraver::consider_begin (Moment test_mom)
151 bool on = to_boolean (get_property ("autoBeaming"));
155 bool b = test_moment (START, test_mom);
162 Auto_beam_engraver::consider_end (Moment test_mom)
166 /* Allow already started autobeam to end:
167 don't check for autoBeaming */
168 bool b = test_moment (STOP, test_mom);
175 Auto_beam_engraver::create_beam ()
177 if (to_boolean (get_property ("skipTypesetting")))
180 Spanner *beam = new Spanner (beam_settings_, context ()->get_grob_key ("Beam"));
181 for (int i = 0; i < stems_->size (); i++)
184 watch out for stem tremolos and abbreviation beams
186 if (Stem::get_beam ((*stems_)[i]))
188 scm_gc_unprotect_object (beam->self_scm ());
191 Beam::add_stem (beam, (*stems_)[i]);
194 announce_grob (beam, (*stems_)[0]->self_scm ());
200 Auto_beam_engraver::begin_beam ()
202 if (stems_ || grouping_)
204 programming_error ("already have autobeam");
208 stems_ = new Link_array<Item>;
209 grouping_ = new Beaming_info_list;
210 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
212 beam_start_moment_ = now_mom ();
214 = robust_scm2moment (get_property ("measurePosition"), Moment (0));
215 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
216 beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
220 Auto_beam_engraver::junk_beam ()
229 beam_settings_ = SCM_EOL;
231 shortest_mom_ = Moment (Rational (1, 8));
235 Auto_beam_engraver::end_beam ()
237 if (stems_->size () < 2)
243 finished_beam_ = create_beam ();
245 finished_grouping_ = grouping_;
249 beam_settings_ = SCM_EOL;
252 shortest_mom_ = Moment (Rational (1, 8));
256 Auto_beam_engraver::typeset_beam ()
260 finished_grouping_->beamify (beat_length_, subdivide_beams_);
261 Beam::set_beaming (finished_beam_, finished_grouping_);
264 delete finished_grouping_;
265 finished_grouping_ = 0;
270 Auto_beam_engraver::start_translation_timestep ()
272 process_acknowledged_count_ = 0;
274 don't beam over skips
278 Moment now = now_mom ();
279 if (extend_mom_ < now)
288 Auto_beam_engraver::stop_translation_timestep ()
294 Auto_beam_engraver::finalize ()
296 /* finished beams may be typeset */
298 /* but unfinished may need another announce/acknowledge pass */
307 Auto_beam_engraver::acknowledge_beam (Grob_info info)
309 check_bar_property ();
317 Auto_beam_engraver::acknowledge_bar_line (Grob_info info)
319 check_bar_property ();
325 Auto_beam_engraver::acknowledge_rest (Grob_info info)
327 check_bar_property ();
333 Auto_beam_engraver::acknowledge_stem (Grob_info info)
335 check_bar_property ();
336 Item *stem = dynamic_cast<Item *> (info.grob ());
337 Music *m = info.music_cause ();
338 if (!m->is_mus_type ("rhythmic-event"))
340 programming_error ("stem must have rhythmic structure");
345 Don't (start) auto-beam over empty stems; skips or rests
347 if (!Stem::head_count (stem))
354 if (Stem::get_beam (stem))
361 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
373 Moment now = now_mom ();
374 if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
377 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
380 consider_begin (dur);
382 if (dur < shortest_mom_)
388 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
392 extend_mom_ = max (extend_mom_, now) + m->get_length ();
396 Auto_beam_engraver::process_acknowledged ()
398 if (extend_mom_ > now_mom ())
401 if (!process_acknowledged_count_)
403 consider_end (shortest_mom_);
404 consider_begin (shortest_mom_);
406 else if (process_acknowledged_count_ > 1)
410 Moment now = now_mom ();
411 if ((extend_mom_ < now)
412 || ((extend_mom_ == now) && (last_add_mom_ != now)))
416 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 /* descr */ "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}. ",
437 /* accepts */ "beam-forbid-event",
439 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",