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"
27 class Auto_beam_engraver : public Engraver
29 TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
31 virtual void stop_translation_timestep ();
32 virtual void start_translation_timestep ();
33 virtual void process_music ();
34 virtual bool try_music (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 is_same_grace_state (Grob* e);
52 shortest_mom is the shortest note in the beam.
55 Spanner *finished_beam_;
56 Link_array<Item>* stems_;
62 Projected ending of the beam we're working on.
65 Moment beam_start_moment_;
66 Moment beam_start_location_;
68 bool subdivide_beams_;
71 // We act as if beam were created, and start a grouping anyway.
72 Beaming_info_list*grouping_;
73 SCM beam_settings_ ; // ugh. should protect ?
75 Beaming_info_list*finished_grouping_;
79 Auto_beam_engraver::process_music ()
81 if (ly_c_string_p (get_property ("whichBar")))
83 consider_end (shortest_mom_);
89 consider_end (shortest_mom_);
95 Auto_beam_engraver::Auto_beam_engraver ()
100 shortest_mom_ = Moment (Rational (1, 8));
102 finished_grouping_ = 0;
104 beam_settings_ = SCM_EOL;
109 Auto_beam_engraver::try_music (Music*m)
111 if (m->is_mus_type ("beam-forbid-event"))
121 Determine end moment for auto beaming (or begin moment, but mostly
122 0==anywhere) In order of increasing priority:
124 i. begin anywhere, end at every beat
125 ii. end * <num> <den>
126 iii. end <type> <num> <den>
134 [to be defined in config file]
135 i. easy catch-all rule
136 ii. exceptions for time signature
137 iii. exceptions for time signature, for specific duration type
141 v. override for specific duration type
145 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
147 Moment now = now_mom ();
154 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
157 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
159 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
161 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
162 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
163 int den = one_beat.den ();
164 SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
166 SCM type = scm_list_n (scm_int2num (test_mom.num ()),
167 scm_int2num (test_mom.den ()), SCM_UNDEFINED);
171 settings aren't grob-properties.
173 SCM settings = get_property ("autoBeamSettings");
177 /* begin beam at any position
178 (and fallback for end) */
181 /* end beam at end of beat */
184 SCM beat (get_property ("beatLength"));
186 if (unsmob_moment (beat))
187 moment = *unsmob_moment (beat);
190 /* second guess: property generic time exception */
191 SCM m = scm_assoc (ly_append3 (function, wild, time), settings);
193 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
194 moment = * unsmob_moment (ly_cdr (m));
196 /* third guess: property time exception, specific for duration type */
197 m = scm_assoc (ly_append3 (function, type, time), settings);
198 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
199 moment = * unsmob_moment (ly_cdr (m));
201 /* fourth guess [user override]: property plain generic */
202 m = scm_assoc (ly_append3 (function, wild, wild), settings);
203 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
204 moment = * unsmob_moment (ly_cdr (m));
206 /* fifth guess [user override]: property plain, specific for duration type */
207 m = scm_assoc (ly_append3 (function, type, wild), settings);
208 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
209 moment = * unsmob_moment (ly_cdr (m));
212 if (moment.to_bool ())
214 /* Ugh? measurePosition can be negative, when \partial
215 We may have to fix this elsewhere (timing translator)
216 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
218 Moment pos = * unsmob_moment (get_property ("measurePosition"));
219 if (pos < Moment (0))
221 Moment length = * unsmob_moment (get_property ("measureLength"));
224 r = pos.main_part_.mod_rat (moment.main_part_);
229 /* if undefined, starting is ok */
232 /* but ending is not */
240 Auto_beam_engraver::consider_begin (Moment test_mom)
242 bool on = to_boolean (get_property ("autoBeaming"));
246 bool b = test_moment (START, test_mom);
253 Auto_beam_engraver::consider_end (Moment test_mom)
257 /* Allow already started autobeam to end:
258 don't check for autoBeaming */
259 bool b = test_moment (STOP, test_mom);
266 Auto_beam_engraver::create_beam ()
268 if (to_boolean (get_property ("skipTypesetting")))
273 Spanner* beam = new Spanner (beam_settings_);
274 for (int i = 0; i < stems_->size (); i++)
277 watch out for stem tremolos and abbreviation beams
279 if (Stem::get_beam ((*stems_)[i]))
281 scm_gc_unprotect_object (beam->self_scm ());
284 Beam::add_stem (beam, (*stems_)[i]);
287 announce_grob (beam, (*stems_)[0]->self_scm ());
293 Auto_beam_engraver::begin_beam ()
295 if (stems_ || grouping_ )
297 programming_error ("already have autobeam");
301 stems_ = new Link_array<Item>;
302 grouping_ = new Beaming_info_list;
303 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
305 beam_start_moment_ = now_mom ();
306 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
307 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
308 beat_length_ = *unsmob_moment (get_property ("beatLength"));
312 Auto_beam_engraver::junk_beam ()
321 beam_settings_ = SCM_EOL;
323 shortest_mom_ = Moment (Rational (1, 8));
327 Auto_beam_engraver::end_beam ()
329 if (stems_->size () < 2)
335 finished_beam_ = create_beam ();
337 finished_grouping_ = grouping_;
341 beam_settings_ = SCM_EOL;
344 shortest_mom_ = Moment (Rational (1, 8));
348 Auto_beam_engraver::typeset_beam ()
352 finished_grouping_->beamify (beat_length_, subdivide_beams_);
353 Beam::set_beaming (finished_beam_, finished_grouping_);
354 typeset_grob (finished_beam_);
357 delete finished_grouping_;
358 finished_grouping_= 0;
363 Auto_beam_engraver::start_translation_timestep ()
367 don't beam over skips
371 Moment now = now_mom ();
372 if (extend_mom_ < now)
381 Auto_beam_engraver::stop_translation_timestep ()
387 Auto_beam_engraver::finalize ()
389 /* finished beams may be typeset */
391 /* but unfinished may need another announce/acknowledge pass */
398 Auto_beam_engraver::acknowledge_grob (Grob_info info)
402 if (Beam::has_interface (info.grob_))
406 else if (Bar_line::has_interface (info.grob_))
410 else if (Rest::has_interface (info.grob_))
416 if (Stem::has_interface (info.grob_))
418 Item* stem = dynamic_cast<Item *> (info.grob_);
419 Music* m = info.music_cause ();
420 if (!m->is_mus_type ("rhythmic-event"))
422 programming_error ("Stem must have rhythmic structure");
427 Don't (start) auto-beam over empty stems; skips or rests
429 if (!Stem::head_count (stem))
436 if (Stem::get_beam (stem))
443 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
456 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
460 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
463 This comment has been here since long:
465 if shortest duration would change
466 consider ending and beginning beam first.
468 but the code didn't match: */
471 consider_begin (dur);
473 if (dur < shortest_mom_)
476 /* I very much suspect that we wanted: */
478 consider_end (shortest_mom_);
479 if (dur < shortest_mom_)
482 consider_end (shortest_mom_);
484 consider_begin (shortest_mom_);
490 Moment now = now_mom ();
492 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
496 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
501 Auto_beam_engraver::process_acknowledged_grobs ()
505 consider_end (shortest_mom_);
506 consider_begin (shortest_mom_);
512 Moment now = now_mom ();
513 if ((extend_mom_ < now)
514 || ((extend_mom_ == now) && (last_add_mom_ != now)))
518 else if (!stems_->size ())
528 ENTER_DESCRIPTION (Auto_beam_engraver,
529 /* descr */ "Generate beams based on measure characteristics and observed "
530 "Stems. Uses beatLength, measureLength and measurePosition to decide "
531 "when to start and stop a beam. Overriding beaming is done through "
532 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
533 "@code{stemRightBeamCount}. "
536 /* accepts */ "beam-forbid-event",
537 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
538 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",