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"
20 class Auto_beam_engraver : public Engraver
22 TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
25 virtual void stop_translation_timestep ();
26 virtual void start_translation_timestep ();
27 virtual void process_music ();
28 virtual bool try_music (Music *);
29 virtual void finalize ();
30 virtual void acknowledge_grob (Grob_info);
31 virtual void process_acknowledged_grobs ();
34 bool test_moment (Direction, Moment);
35 void consider_begin (Moment);
36 void consider_end (Moment);
37 Spanner *create_beam ();
41 bool is_same_grace_state (Grob *e);
46 shortest_mom is the shortest note in the beam.
49 Spanner *finished_beam_;
50 Link_array<Item> *stems_;
55 Projected ending of the beam we're working on.
58 Moment beam_start_moment_;
59 Moment beam_start_location_;
61 bool subdivide_beams_;
64 // We act as if beam were created, and start a grouping anyway.
65 Beaming_info_list *grouping_;
66 SCM beam_settings_; // ugh. should protect ?
68 Beaming_info_list *finished_grouping_;
72 Auto_beam_engraver::process_music ()
74 if (scm_is_string (get_property ("whichBar")))
76 consider_end (shortest_mom_);
82 consider_end (shortest_mom_);
87 Auto_beam_engraver::Auto_beam_engraver ()
92 shortest_mom_ = Moment (Rational (1, 8));
94 finished_grouping_ = 0;
96 beam_settings_ = SCM_EOL;
100 Auto_beam_engraver::try_music (Music *m)
102 if (m->is_mus_type ("beam-forbid-event"))
112 Determine end moment for auto beaming (or begin moment, but mostly
113 0== anywhere) In order of increasing priority:
115 i. begin anywhere, end at every beat
116 ii. end * <num> <den>
117 iii. end <type> <num> <den>
125 [to be defined in config file]
126 i. easy catch-all rule
127 ii. exceptions for time signature
128 iii. exceptions for time signature, for specific duration type
132 v. override for specific duration type
135 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
137 Moment now = now_mom ();
144 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
147 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
149 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
151 Moment beat_length (1, 4);
152 if (Moment *m = unsmob_moment (get_property ("beatLength")))
156 Moment measure_length (1, 1);
158 if (Moment *m = unsmob_moment (get_property ("measureLength")))
160 num = int ((*m / beat_length).main_part_);
163 int den = beat_length.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 moment = robust_scm2moment (get_property ("beatLength"), moment);
187 /* second guess: property generic time exception */
188 SCM m = scm_assoc (ly_append3 (function, wild, time), settings);
190 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
191 moment = *unsmob_moment (scm_cdr (m));
193 /* third guess: property time exception, specific for duration type */
194 m = scm_assoc (ly_append3 (function, type, time), settings);
195 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
196 moment = *unsmob_moment (scm_cdr (m));
198 /* fourth guess [user override]: property plain generic */
199 m = scm_assoc (ly_append3 (function, wild, wild), settings);
200 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
201 moment = *unsmob_moment (scm_cdr (m));
203 /* fifth guess [user override]: property plain, specific for duration type */
204 m = scm_assoc (ly_append3 (function, type, wild), settings);
205 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
206 moment = *unsmob_moment (scm_cdr (m));
209 if (moment.to_bool ())
211 /* Ugh? measurePosition can be negative, when \partial
212 We may have to fix this elsewhere (timing translator)
213 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
216 if (Moment *m = unsmob_moment (get_property ("measurePosition")))
221 if (pos < Moment (0))
225 if (Moment *m = unsmob_moment (get_property ("measureLength")))
231 r = pos.main_part_.mod_rat (moment.main_part_);
236 /* if undefined, starting is ok */
239 /* but ending is not */
247 Auto_beam_engraver::consider_begin (Moment test_mom)
249 bool on = to_boolean (get_property ("autoBeaming"));
253 bool b = test_moment (START, test_mom);
260 Auto_beam_engraver::consider_end (Moment test_mom)
264 /* Allow already started autobeam to end:
265 don't check for autoBeaming */
266 bool b = test_moment (STOP, test_mom);
273 Auto_beam_engraver::create_beam ()
275 if (to_boolean (get_property ("skipTypesetting")))
278 Spanner *beam = new Spanner (beam_settings_, context ()->get_grob_key ("Beam"));
279 for (int i = 0; i < stems_->size (); i++)
282 watch out for stem tremolos and abbreviation beams
284 if (Stem::get_beam ((*stems_)[i]))
286 scm_gc_unprotect_object (beam->self_scm ());
289 Beam::add_stem (beam, (*stems_)[i]);
292 announce_grob (beam, (*stems_)[0]->self_scm ());
298 Auto_beam_engraver::begin_beam ()
300 if (stems_ || grouping_)
302 programming_error ("already have autobeam");
306 stems_ = new Link_array<Item>;
307 grouping_ = new Beaming_info_list;
308 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
310 beam_start_moment_ = now_mom ();
312 = robust_scm2moment (get_property ("measurePosition"), Moment (0));
313 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
314 beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
318 Auto_beam_engraver::junk_beam ()
327 beam_settings_ = SCM_EOL;
329 shortest_mom_ = Moment (Rational (1, 8));
333 Auto_beam_engraver::end_beam ()
335 if (stems_->size () < 2)
341 finished_beam_ = create_beam ();
343 finished_grouping_ = grouping_;
347 beam_settings_ = SCM_EOL;
350 shortest_mom_ = Moment (Rational (1, 8));
354 Auto_beam_engraver::typeset_beam ()
358 finished_grouping_->beamify (beat_length_, subdivide_beams_);
359 Beam::set_beaming (finished_beam_, finished_grouping_);
362 delete finished_grouping_;
363 finished_grouping_ = 0;
368 Auto_beam_engraver::start_translation_timestep ()
372 don't beam over skips
376 Moment now = now_mom ();
377 if (extend_mom_ < now)
386 Auto_beam_engraver::stop_translation_timestep ()
392 Auto_beam_engraver::finalize ()
394 /* finished beams may be typeset */
396 /* but unfinished may need another announce/acknowledge pass */
402 Auto_beam_engraver::acknowledge_grob (Grob_info info)
405 Duplicated from process_music(), since
406 Repeat_acknowledge_engraver::process_music() may also set whichBar
408 if (scm_is_string (get_property ("whichBar"))
409 && beam_start_moment_ < now_mom ())
411 consider_end (shortest_mom_);
417 if (Beam::has_interface (info.grob_))
421 else if (Bar_line::has_interface (info.grob_))
425 else if (Rest::has_interface (info.grob_))
431 if (Stem::has_interface (info.grob_))
433 Item *stem = dynamic_cast<Item *> (info.grob_);
434 Music *m = info.music_cause ();
435 if (!m->is_mus_type ("rhythmic-event"))
437 programming_error ("Stem must have rhythmic structure");
442 Don't (start) auto-beam over empty stems; skips or rests
444 if (!Stem::head_count (stem))
451 if (Stem::get_beam (stem))
458 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
470 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
473 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
476 This comment has been here since long:
478 if shortest duration would change
479 consider ending and beginning beam first.
481 but the code didn't match: */
484 consider_begin (dur);
486 if (dur < shortest_mom_)
489 /* I very much suspect that we wanted: */
491 consider_end (shortest_mom_);
492 if (dur < shortest_mom_)
495 consider_end (shortest_mom_);
497 consider_begin (shortest_mom_);
503 Moment now = now_mom ();
505 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
509 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
514 Auto_beam_engraver::process_acknowledged_grobs ()
518 consider_end (shortest_mom_);
519 consider_begin (shortest_mom_);
525 Moment now = now_mom ();
526 if ((extend_mom_ < now)
527 || ((extend_mom_ == now) && (last_add_mom_ != now)))
531 else if (!stems_->size ())
541 ADD_TRANSLATOR (Auto_beam_engraver,
542 /* descr */ "Generate beams based on measure characteristics and observed "
543 "Stems. Uses beatLength, measureLength and measurePosition to decide "
544 "when to start and stop a beam. Overriding beaming is done through "
545 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
546 "@code{stemRightBeamCount}. ",
548 /* accepts */ "beam-forbid-event",
549 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
550 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",