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++)
201 Beam::add_stem (beam, (*stems_)[i]);
204 announce_grob (beam, (*stems_)[0]->self_scm ());
210 Auto_beam_engraver::begin_beam ()
212 if (stems_ || grouping_)
214 programming_error ("already have autobeam");
218 stems_ = new Link_array<Item>;
219 grouping_ = new Beaming_info_list;
220 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
222 beam_start_moment_ = now_mom ();
224 = robust_scm2moment (get_property ("measurePosition"), Moment (0));
225 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
226 beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
230 Auto_beam_engraver::junk_beam ()
239 beam_settings_ = SCM_EOL;
241 shortest_mom_ = Moment (Rational (1, 8));
245 Auto_beam_engraver::end_beam ()
247 if (stems_->size () < 2)
253 finished_beam_ = create_beam ();
255 finished_grouping_ = grouping_;
259 beam_settings_ = SCM_EOL;
262 shortest_mom_ = Moment (Rational (1, 8));
266 Auto_beam_engraver::typeset_beam ()
270 finished_grouping_->beamify (beat_length_, subdivide_beams_);
271 Beam::set_beaming (finished_beam_, finished_grouping_);
274 delete finished_grouping_;
275 finished_grouping_ = 0;
280 Auto_beam_engraver::start_translation_timestep ()
282 process_acknowledged_count_ = 0;
284 don't beam over skips
288 Moment now = now_mom ();
289 if (extend_mom_ < now)
298 Auto_beam_engraver::stop_translation_timestep ()
304 Auto_beam_engraver::finalize ()
306 /* finished beams may be typeset */
308 /* but unfinished may need another announce/acknowledge pass */
317 Auto_beam_engraver::acknowledge_beam (Grob_info info)
320 check_bar_property ();
328 Auto_beam_engraver::acknowledge_bar_line (Grob_info info)
331 check_bar_property ();
337 Auto_beam_engraver::acknowledge_rest (Grob_info info)
340 check_bar_property ();
346 Auto_beam_engraver::acknowledge_stem (Grob_info info)
348 check_bar_property ();
349 Item *stem = dynamic_cast<Item *> (info.grob ());
350 Music *m = info.music_cause ();
351 if (!m->is_mus_type ("rhythmic-event"))
353 programming_error ("stem must have rhythmic structure");
358 Don't (start) auto-beam over empty stems; skips or rests
360 if (!Stem::head_count (stem))
367 if (Stem::get_beam (stem))
374 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
386 Moment now = now_mom ();
387 if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
390 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
393 consider_begin (dur);
395 if (dur < shortest_mom_)
401 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
405 extend_mom_ = max (extend_mom_, now) + m->get_length ();
409 Auto_beam_engraver::process_acknowledged ()
411 if (extend_mom_ > now_mom ())
414 if (!process_acknowledged_count_)
416 consider_end (shortest_mom_);
417 consider_begin (shortest_mom_);
419 else if (process_acknowledged_count_ > 1)
423 Moment now = now_mom ();
424 if ((extend_mom_ < now)
425 || ((extend_mom_ == now) && (last_add_mom_ != now)))
429 else if (!stems_->size ())
436 process_acknowledged_count_++;
439 ADD_ACKNOWLEDGER (Auto_beam_engraver,stem);
440 ADD_ACKNOWLEDGER (Auto_beam_engraver,bar_line);
441 ADD_ACKNOWLEDGER (Auto_beam_engraver,beam);
442 ADD_ACKNOWLEDGER (Auto_beam_engraver,rest);
443 ADD_TRANSLATOR (Auto_beam_engraver,
444 /* descr */ "Generate beams based on measure characteristics and observed "
445 "Stems. Uses beatLength, measureLength and measurePosition to decide "
446 "when to start and stop a beam. Overriding beaming is done through "
447 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
448 "@code{stemRightBeamCount}. ",
450 /* accepts */ "beam-forbid-event",
451 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",