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>
10 #include "engraver.hh"
15 #include "bar-line.hh"
21 class Auto_beam_engraver : public Engraver
23 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_;
56 Projected ending of the beam we're working on.
59 Moment beam_start_moment_;
60 Moment beam_start_location_;
62 bool subdivide_beams_;
65 // We act as if beam were created, and start a grouping anyway.
66 Beaming_info_list*grouping_;
67 SCM beam_settings_ ; // ugh. should protect ?
69 Beaming_info_list*finished_grouping_;
73 Auto_beam_engraver::process_music ()
75 if (scm_is_string (get_property ("whichBar")))
77 consider_end (shortest_mom_);
83 consider_end (shortest_mom_);
89 Auto_beam_engraver::Auto_beam_engraver ()
94 shortest_mom_ = Moment (Rational (1, 8));
96 finished_grouping_ = 0;
98 beam_settings_ = SCM_EOL;
103 Auto_beam_engraver::try_music (Music*m)
105 if (m->is_mus_type ("beam-forbid-event"))
115 Determine end moment for auto beaming (or begin moment, but mostly
116 0== anywhere) In order of increasing priority:
118 i. begin anywhere, end at every beat
119 ii. end * <num> <den>
120 iii. end <type> <num> <den>
128 [to be defined in config file]
129 i. easy catch-all rule
130 ii. exceptions for time signature
131 iii. exceptions for time signature, for specific duration type
135 v. override for specific duration type
139 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
141 Moment now = now_mom ();
148 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
151 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
153 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
155 Moment beat_length (1,4);
156 if (Moment * m = unsmob_moment (get_property ("beatLength")))
160 Moment measure_length (1,1);
162 if (Moment* m = unsmob_moment (get_property ("measureLength")))
164 num = int ((*m / beat_length).main_part_);
167 int den = beat_length.den ();
168 SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
170 SCM type = scm_list_n (scm_int2num (test_mom.num ()),
171 scm_int2num (test_mom.den ()), SCM_UNDEFINED);
175 settings aren't grob-properties.
177 SCM settings = get_property ("autoBeamSettings");
181 /* begin beam at any position
182 (and fallback for end) */
185 /* end beam at end of beat */
188 moment = robust_scm2moment (get_property ("beatLength"), moment);
191 /* second guess: property generic time exception */
192 SCM m = scm_assoc (ly_append3 (function, wild, time), settings);
194 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
195 moment = * unsmob_moment (scm_cdr (m));
197 /* third guess: property time exception, specific for duration type */
198 m = scm_assoc (ly_append3 (function, type, time), settings);
199 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
200 moment = * unsmob_moment (scm_cdr (m));
202 /* fourth guess [user override]: property plain generic */
203 m = scm_assoc (ly_append3 (function, wild, wild), settings);
204 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
205 moment = * unsmob_moment (scm_cdr (m));
207 /* fifth guess [user override]: property plain, specific for duration type */
208 m = scm_assoc (ly_append3 (function, type, wild), settings);
209 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
210 moment = * unsmob_moment (scm_cdr (m));
213 if (moment.to_bool ())
215 /* Ugh? measurePosition can be negative, when \partial
216 We may have to fix this elsewhere (timing translator)
217 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
220 if (Moment *m = unsmob_moment (get_property ("measurePosition")))
225 if (pos < Moment (0))
229 if ( Moment *m = unsmob_moment (get_property ("measureLength")))
235 r = pos.main_part_.mod_rat (moment.main_part_);
240 /* if undefined, starting is ok */
243 /* but ending is not */
251 Auto_beam_engraver::consider_begin (Moment test_mom)
253 bool on = to_boolean (get_property ("autoBeaming"));
257 bool b = test_moment (START, test_mom);
264 Auto_beam_engraver::consider_end (Moment test_mom)
268 /* Allow already started autobeam to end:
269 don't check for autoBeaming */
270 bool b = test_moment (STOP, test_mom);
277 Auto_beam_engraver::create_beam ()
279 if (to_boolean (get_property ("skipTypesetting")))
282 Spanner *beam = new Spanner (beam_settings_, context ()->get_grob_key ("Beam"));
283 for (int i = 0; i < stems_->size (); i++)
286 watch out for stem tremolos and abbreviation beams
288 if (Stem::get_beam ((*stems_)[i]))
290 scm_gc_unprotect_object (beam->self_scm ());
293 Beam::add_stem (beam, (*stems_)[i]);
296 announce_grob (beam, (*stems_)[0]->self_scm ());
302 Auto_beam_engraver::begin_beam ()
304 if (stems_ || grouping_ )
306 programming_error ("already have autobeam");
310 stems_ = new Link_array<Item>;
311 grouping_ = new Beaming_info_list;
312 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
314 beam_start_moment_ = now_mom ();
315 beam_start_location_ =
316 robust_scm2moment (get_property ("measurePosition"), Moment(0));
317 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
318 beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment(1,4));
322 Auto_beam_engraver::junk_beam ()
331 beam_settings_ = SCM_EOL;
333 shortest_mom_ = Moment (Rational (1, 8));
337 Auto_beam_engraver::end_beam ()
339 if (stems_->size () < 2)
345 finished_beam_ = create_beam ();
347 finished_grouping_ = grouping_;
351 beam_settings_ = SCM_EOL;
354 shortest_mom_ = Moment (Rational (1, 8));
358 Auto_beam_engraver::typeset_beam ()
362 finished_grouping_->beamify (beat_length_, subdivide_beams_);
363 Beam::set_beaming (finished_beam_, finished_grouping_);
366 delete finished_grouping_;
367 finished_grouping_ = 0;
372 Auto_beam_engraver::start_translation_timestep ()
376 don't beam over skips
380 Moment now = now_mom ();
381 if (extend_mom_ < now)
390 Auto_beam_engraver::stop_translation_timestep ()
396 Auto_beam_engraver::finalize ()
398 /* finished beams may be typeset */
400 /* but unfinished may need another announce/acknowledge pass */
407 Auto_beam_engraver::acknowledge_grob (Grob_info info)
411 if (Beam::has_interface (info.grob_))
415 else if (Bar_line::has_interface (info.grob_))
419 else if (Rest::has_interface (info.grob_))
425 if (Stem::has_interface (info.grob_))
427 Item* stem = dynamic_cast<Item *> (info.grob_);
428 Music* m = info.music_cause ();
429 if (!m->is_mus_type ("rhythmic-event"))
431 programming_error ("Stem must have rhythmic structure");
436 Don't (start) auto-beam over empty stems; skips or rests
438 if (!Stem::head_count (stem))
445 if (Stem::get_beam (stem))
452 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
465 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
469 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
472 This comment has been here since long:
474 if shortest duration would change
475 consider ending and beginning beam first.
477 but the code didn't match: */
480 consider_begin (dur);
482 if (dur < shortest_mom_)
485 /* I very much suspect that we wanted: */
487 consider_end (shortest_mom_);
488 if (dur < shortest_mom_)
491 consider_end (shortest_mom_);
493 consider_begin (shortest_mom_);
499 Moment now = now_mom ();
501 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
505 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
510 Auto_beam_engraver::process_acknowledged_grobs ()
514 consider_end (shortest_mom_);
515 consider_begin (shortest_mom_);
521 Moment now = now_mom ();
522 if ((extend_mom_ < now)
523 || ((extend_mom_ == now) && (last_add_mom_ != now)))
527 else if (!stems_->size ())
537 ENTER_DESCRIPTION (Auto_beam_engraver,
538 /* descr */ "Generate beams based on measure characteristics and observed "
539 "Stems. Uses beatLength, measureLength and measurePosition to decide "
540 "when to start and stop a beam. Overriding beaming is done through "
541 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
542 "@code{stemRightBeamCount}. "
545 /* accepts */ "beam-forbid-event",
546 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
547 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",