2 auto-beam-engraver.cc -- implement Auto_beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2004 Jan Nieuwenhuizen <janneke@gnu.org>
15 #include "engraver-group-engraver.hh"
16 #include "bar-line.hh"
18 #include "engraver.hh"
21 #include "duration.hh"
24 TODO: figure what to do in grace?
28 class Auto_beam_engraver : public Engraver
30 TRANSLATOR_DECLARATIONS(Auto_beam_engraver);
32 virtual void stop_translation_timestep ();
33 virtual void start_translation_timestep ();
34 virtual void process_music ();
35 virtual void finalize ();
36 virtual void acknowledge_grob (Grob_info);
37 virtual void process_acknowledged_grobs ();
40 bool test_moment (Direction, Moment);
41 void consider_begin (Moment);
42 void consider_end (Moment);
43 Spanner* create_beam ();
47 bool same_grace_state_b (Grob* e);
51 shortest_mom is the shortest note in the beam.
54 Spanner *finished_beam_;
55 Link_array<Item>* stems_;
61 Projected ending of the beam we're working on.
64 Moment beam_start_moment_;
65 Moment beam_start_location_;
67 bool subdivide_beams_;
70 // We act as if beam were created, and start a grouping anyway.
71 Beaming_info_list*grouping_;
72 SCM beam_settings_ ; // ugh. should protect ?
74 Beaming_info_list*finished_grouping_;
78 Auto_beam_engraver::process_music ()
80 if (gh_string_p (get_property ("whichBar")))
82 consider_end (shortest_mom_);
88 Auto_beam_engraver::Auto_beam_engraver ()
92 shortest_mom_ = Moment (Rational (1, 8));
94 finished_grouping_ = 0;
96 beam_settings_ = SCM_EOL;
100 Determine end moment for auto beaming (or begin moment, but mostly
101 0==anywhere) In order of increasing priority:
103 i. begin anywhere, end at every beat
104 ii. end * <num> <den>
105 iii. end <type> <num> <den>
113 [to be defined in config file]
114 i. easy catch-all rule
115 ii. exceptions for time signature
116 iii. exceptions for time signature, for specific duration type
120 v. override for specific duration type
124 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
126 Moment now = now_mom();
133 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
136 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
138 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
140 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
141 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
142 int den = one_beat.den ();
143 SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
145 SCM type = scm_list_n (scm_int2num (test_mom.num ()),
146 scm_int2num (test_mom.den ()), SCM_UNDEFINED);
148 SCM settings = get_property ("autoBeamSettings");
152 /* begin beam at any position
153 (and fallback for end) */
156 /* end beam at end of beat */
159 SCM beat (get_property ("beatLength"));
161 if (unsmob_moment (beat))
162 moment = *unsmob_moment (beat);
165 /* second guess: property generic time exception */
166 SCM m = scm_assoc (gh_append3 (function, wild, time), settings);
168 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
169 moment = * unsmob_moment (ly_cdr (m));
171 /* third guess: property time exception, specific for duration type */
172 m = scm_assoc (gh_append3 (function, type, time), settings);
173 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
174 moment = * unsmob_moment (ly_cdr (m));
176 /* fourth guess [user override]: property plain generic */
177 m = scm_assoc (gh_append3 (function, wild, wild), settings);
178 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
179 moment = * unsmob_moment (ly_cdr (m));
181 /* fifth guess [user override]: property plain, specific for duration type */
182 m = scm_assoc (gh_append3 (function, type, wild), settings);
183 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
184 moment = * unsmob_moment (ly_cdr (m));
187 if (moment.to_bool ())
189 /* Ugh? measurePosition can be negative, when \partial
190 We may have to fix this elsewhere (timing translator)
191 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
193 Moment pos = * unsmob_moment (get_property ("measurePosition"));
194 if (pos < Moment (0))
196 Moment length = * unsmob_moment (get_property ("measureLength"));
199 r = pos.main_part_.mod_rat (moment.main_part_);
204 /* if undefined, starting is ok */
207 /* but ending is not */
215 Auto_beam_engraver::consider_begin (Moment test_mom)
217 bool on = to_boolean (get_property ("autoBeaming"));
220 bool b = test_moment (START, test_mom);
227 Auto_beam_engraver::consider_end (Moment test_mom)
231 /* Allow already started autobeam to end:
232 don't check for autoBeaming */
233 bool b = test_moment (STOP, test_mom);
240 Auto_beam_engraver::create_beam ()
242 if (to_boolean (get_property ("skipTypesetting")))
247 Spanner* beam = new Spanner (beam_settings_);
248 for (int i = 0; i < stems_->size (); i++)
251 watch out for stem tremolos and abbreviation beams
253 if (Stem::get_beam ((*stems_)[i]))
255 scm_gc_unprotect_object (beam->self_scm ());
258 Beam::add_stem (beam, (*stems_)[i]);
261 announce_grob(beam, SCM_EOL);
267 Auto_beam_engraver::begin_beam ()
269 if (stems_ || grouping_ )
271 programming_error ("already have autobeam");
275 stems_ = new Link_array<Item>;
276 grouping_ = new Beaming_info_list;
277 beam_settings_ = updated_grob_properties (daddy_trans_, ly_symbol2scm ("Beam"));
279 beam_start_moment_ = now_mom ();
280 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
281 subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
282 beat_length_ = *unsmob_moment (get_property ("beatLength"));
286 Auto_beam_engraver::junk_beam ()
295 beam_settings_ = SCM_EOL;
297 shortest_mom_ = Moment (Rational (1, 8));
301 Auto_beam_engraver::end_beam ()
303 if (stems_->size () < 2)
309 finished_beam_ = create_beam ();
311 finished_grouping_ = grouping_;
315 beam_settings_ = SCM_EOL;
318 shortest_mom_ = Moment (Rational (1, 8));
322 Auto_beam_engraver::typeset_beam ()
326 finished_grouping_->beamify(beat_length_, subdivide_beams_);
327 Beam::set_beaming (finished_beam_, finished_grouping_);
328 typeset_grob (finished_beam_);
331 delete finished_grouping_;
332 finished_grouping_= 0;
337 Auto_beam_engraver::start_translation_timestep ()
341 don't beam over skips
345 Moment now = now_mom ();
346 if (extend_mom_ < now)
354 Auto_beam_engraver::stop_translation_timestep ()
360 Auto_beam_engraver::finalize ()
362 /* finished beams may be typeset */
364 /* but unfinished may need another announce/acknowledge pass */
371 Auto_beam_engraver::acknowledge_grob (Grob_info info)
375 if (Beam::has_interface (info.grob_))
379 else if (Bar_line::has_interface (info.grob_))
383 else if (Rest::has_interface (info.grob_))
389 if (Stem::has_interface (info.grob_))
391 Item* stem = dynamic_cast<Item *> (info.grob_);
392 Music* m = info.music_cause ();
393 if (!m->is_mus_type ("rhythmic-event"))
395 programming_error ("Stem must have rhythmic structure");
400 Don't (start) auto-beam over empty stems; skips or rests
402 if (!Stem::head_count (stem))
409 if (Stem::get_beam (stem))
416 int durlog = unsmob_duration (m->get_mus_property ("duration"))->duration_log ();
429 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
433 Moment dur = unsmob_duration (m->get_mus_property ("duration"))->get_length ();
436 This comment has been here since long:
438 if shortest duration would change
439 consider ending and beginning beam first.
441 but the code didn't match: */
444 consider_begin (dur);
446 if (dur < shortest_mom_)
449 /* I very much suspect that we wanted: */
451 consider_end (shortest_mom_);
452 if (dur < shortest_mom_)
455 consider_end (shortest_mom_);
457 consider_begin (shortest_mom_);
463 Moment now = now_mom ();
465 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
469 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
474 Auto_beam_engraver::process_acknowledged_grobs ()
478 consider_end (shortest_mom_);
479 consider_begin (shortest_mom_);
485 Moment now = now_mom ();
486 if ((extend_mom_ < now)
487 || ((extend_mom_ == now) && (last_add_mom_ != now)))
491 else if (!stems_->size ())
501 ENTER_DESCRIPTION (Auto_beam_engraver,
502 /* descr */ "Generate beams based on measure characteristics and observed "
503 "Stems. Uses beatLength, measureLength and measurePosition to decide "
504 "when to start and stop a beam. Overriding beaming is done through "
505 "@ref{Stem_engraver} properties stemLeftBeamCount and "
506 "stemRightBeamCount. "
510 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
511 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",