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 class Auto_beam_engraver : public Engraver
26 TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
28 virtual void stop_translation_timestep ();
29 virtual void start_translation_timestep ();
30 virtual void process_music ();
31 virtual bool try_music (Music*);
32 virtual void finalize ();
33 virtual void acknowledge_grob (Grob_info);
34 virtual void process_acknowledged_grobs ();
37 bool test_moment (Direction, Moment);
38 void consider_begin (Moment);
39 void consider_end (Moment);
40 Spanner* create_beam ();
44 bool is_same_grace_state (Grob* e);
49 shortest_mom is the shortest note in the beam.
52 Spanner *finished_beam_;
53 Link_array<Item>* stems_;
59 Projected ending of the beam we're working on.
62 Moment beam_start_moment_;
63 Moment beam_start_location_;
65 bool subdivide_beams_;
68 // We act as if beam were created, and start a grouping anyway.
69 Beaming_info_list*grouping_;
70 SCM beam_settings_ ; // ugh. should protect ?
72 Beaming_info_list*finished_grouping_;
76 Auto_beam_engraver::process_music ()
78 if (scm_is_string (get_property ("whichBar")))
80 consider_end (shortest_mom_);
86 consider_end (shortest_mom_);
92 Auto_beam_engraver::Auto_beam_engraver ()
97 shortest_mom_ = Moment (Rational (1, 8));
99 finished_grouping_ = 0;
101 beam_settings_ = SCM_EOL;
106 Auto_beam_engraver::try_music (Music*m)
108 if (m->is_mus_type ("beam-forbid-event"))
118 Determine end moment for auto beaming (or begin moment, but mostly
119 0==anywhere) In order of increasing priority:
121 i. begin anywhere, end at every beat
122 ii. end * <num> <den>
123 iii. end <type> <num> <den>
131 [to be defined in config file]
132 i. easy catch-all rule
133 ii. exceptions for time signature
134 iii. exceptions for time signature, for specific duration type
138 v. override for specific duration type
142 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
144 Moment now = now_mom ();
151 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
154 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
156 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
158 Moment beat_length (1,4);
159 if (Moment * m = unsmob_moment (get_property ("beatLength")))
163 Moment measure_length (1,1);
165 if (Moment* m = unsmob_moment (get_property ("measureLength")))
167 num = int ((*m / beat_length).main_part_);
170 int den = beat_length.den ();
171 SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
173 SCM type = scm_list_n (scm_int2num (test_mom.num ()),
174 scm_int2num (test_mom.den ()), SCM_UNDEFINED);
178 settings aren't grob-properties.
180 SCM settings = get_property ("autoBeamSettings");
184 /* begin beam at any position
185 (and fallback for end) */
188 /* end beam at end of beat */
191 moment = robust_scm2moment (get_property ("beatLength"), moment);
194 /* second guess: property generic time exception */
195 SCM m = scm_assoc (ly_append3 (function, wild, time), settings);
197 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
198 moment = * unsmob_moment (scm_cdr (m));
200 /* third guess: property time exception, specific for duration type */
201 m = scm_assoc (ly_append3 (function, type, time), settings);
202 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
203 moment = * unsmob_moment (scm_cdr (m));
205 /* fourth guess [user override]: property plain generic */
206 m = scm_assoc (ly_append3 (function, wild, wild), settings);
207 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
208 moment = * unsmob_moment (scm_cdr (m));
210 /* fifth guess [user override]: property plain, specific for duration type */
211 m = scm_assoc (ly_append3 (function, type, wild), settings);
212 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
213 moment = * unsmob_moment (scm_cdr (m));
216 if (moment.to_bool ())
218 /* Ugh? measurePosition can be negative, when \partial
219 We may have to fix this elsewhere (timing translator)
220 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
223 if (Moment *m = unsmob_moment (get_property ("measurePosition")))
228 if (pos < Moment (0))
232 if ( Moment *m = unsmob_moment (get_property ("measureLength")))
238 r = pos.main_part_.mod_rat (moment.main_part_);
243 /* if undefined, starting is ok */
246 /* but ending is not */
254 Auto_beam_engraver::consider_begin (Moment test_mom)
256 bool on = to_boolean (get_property ("autoBeaming"));
260 bool b = test_moment (START, test_mom);
267 Auto_beam_engraver::consider_end (Moment test_mom)
271 /* Allow already started autobeam to end:
272 don't check for autoBeaming */
273 bool b = test_moment (STOP, test_mom);
280 Auto_beam_engraver::create_beam ()
282 if (to_boolean (get_property ("skipTypesetting")))
285 Spanner *beam = new Spanner (beam_settings_, context ()->get_grob_key ("Beam"));
286 for (int i = 0; i < stems_->size (); i++)
289 watch out for stem tremolos and abbreviation beams
291 if (Stem::get_beam ((*stems_)[i]))
293 scm_gc_unprotect_object (beam->self_scm ());
296 Beam::add_stem (beam, (*stems_)[i]);
299 announce_grob (beam, (*stems_)[0]->self_scm ());
305 Auto_beam_engraver::begin_beam ()
307 if (stems_ || grouping_ )
309 programming_error ("already have autobeam");
313 stems_ = new Link_array<Item>;
314 grouping_ = new Beaming_info_list;
315 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
317 beam_start_moment_ = now_mom ();
318 beam_start_location_ =
319 robust_scm2moment (get_property ("measurePosition"), Moment(0));
320 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
321 beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment(1,4));
325 Auto_beam_engraver::junk_beam ()
334 beam_settings_ = SCM_EOL;
336 shortest_mom_ = Moment (Rational (1, 8));
340 Auto_beam_engraver::end_beam ()
342 if (stems_->size () < 2)
348 finished_beam_ = create_beam ();
350 finished_grouping_ = grouping_;
354 beam_settings_ = SCM_EOL;
357 shortest_mom_ = Moment (Rational (1, 8));
361 Auto_beam_engraver::typeset_beam ()
365 finished_grouping_->beamify (beat_length_, subdivide_beams_);
366 Beam::set_beaming (finished_beam_, finished_grouping_);
369 delete finished_grouping_;
370 finished_grouping_= 0;
375 Auto_beam_engraver::start_translation_timestep ()
379 don't beam over skips
383 Moment now = now_mom ();
384 if (extend_mom_ < now)
393 Auto_beam_engraver::stop_translation_timestep ()
399 Auto_beam_engraver::finalize ()
401 /* finished beams may be typeset */
403 /* but unfinished may need another announce/acknowledge pass */
410 Auto_beam_engraver::acknowledge_grob (Grob_info info)
414 if (Beam::has_interface (info.grob_))
418 else if (Bar_line::has_interface (info.grob_))
422 else if (Rest::has_interface (info.grob_))
428 if (Stem::has_interface (info.grob_))
430 Item* stem = dynamic_cast<Item *> (info.grob_);
431 Music* m = info.music_cause ();
432 if (!m->is_mus_type ("rhythmic-event"))
434 programming_error ("Stem must have rhythmic structure");
439 Don't (start) auto-beam over empty stems; skips or rests
441 if (!Stem::head_count (stem))
448 if (Stem::get_beam (stem))
455 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
468 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
472 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
475 This comment has been here since long:
477 if shortest duration would change
478 consider ending and beginning beam first.
480 but the code didn't match: */
483 consider_begin (dur);
485 if (dur < shortest_mom_)
488 /* I very much suspect that we wanted: */
490 consider_end (shortest_mom_);
491 if (dur < shortest_mom_)
494 consider_end (shortest_mom_);
496 consider_begin (shortest_mom_);
502 Moment now = now_mom ();
504 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
508 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
513 Auto_beam_engraver::process_acknowledged_grobs ()
517 consider_end (shortest_mom_);
518 consider_begin (shortest_mom_);
524 Moment now = now_mom ();
525 if ((extend_mom_ < now)
526 || ((extend_mom_ == now) && (last_add_mom_ != now)))
530 else if (!stems_->size ())
540 ENTER_DESCRIPTION (Auto_beam_engraver,
541 /* descr */ "Generate beams based on measure characteristics and observed "
542 "Stems. Uses beatLength, measureLength and measurePosition to decide "
543 "when to start and stop a beam. Overriding beaming is done through "
544 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
545 "@code{stemRightBeamCount}. "
548 /* accepts */ "beam-forbid-event",
549 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
550 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",