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"
25 TODO: figure what to do in grace?
29 class Auto_beam_engraver : public Engraver
31 TRANSLATOR_DECLARATIONS(Auto_beam_engraver);
33 virtual void stop_translation_timestep ();
34 virtual void start_translation_timestep ();
35 virtual void process_music ();
36 virtual void finalize ();
37 virtual void acknowledge_grob (Grob_info);
38 virtual void process_acknowledged_grobs ();
41 bool test_moment (Direction, Moment);
42 void consider_begin (Moment);
43 void consider_end (Moment);
44 Spanner* create_beam ();
48 bool same_grace_state_b (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 (gh_string_p (get_property ("whichBar")))
83 consider_end (shortest_mom_);
89 Auto_beam_engraver::Auto_beam_engraver ()
93 shortest_mom_ = Moment (Rational (1, 8));
95 finished_grouping_ = 0;
97 beam_settings_ = SCM_EOL;
101 Determine end moment for auto beaming (or begin moment, but mostly
102 0==anywhere) In order of increasing priority:
104 i. begin anywhere, end at every beat
105 ii. end * <num> <den>
106 iii. end <type> <num> <den>
114 [to be defined in config file]
115 i. easy catch-all rule
116 ii. exceptions for time signature
117 iii. exceptions for time signature, for specific duration type
121 v. override for specific duration type
125 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
127 Moment now = now_mom();
134 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
137 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
139 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
141 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
142 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
143 int den = one_beat.den ();
144 SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
146 SCM type = scm_list_n (scm_int2num (test_mom.num ()),
147 scm_int2num (test_mom.den ()), SCM_UNDEFINED);
151 settings aren't grob-properties.
153 SCM settings = get_property ("autoBeamSettings");
157 /* begin beam at any position
158 (and fallback for end) */
161 /* end beam at end of beat */
164 SCM beat (get_property ("beatLength"));
166 if (unsmob_moment (beat))
167 moment = *unsmob_moment (beat);
170 /* second guess: property generic time exception */
171 SCM m = scm_assoc (gh_append3 (function, wild, time), settings);
173 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
174 moment = * unsmob_moment (ly_cdr (m));
176 /* third guess: property time exception, specific for duration type */
177 m = scm_assoc (gh_append3 (function, type, time), settings);
178 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
179 moment = * unsmob_moment (ly_cdr (m));
181 /* fourth guess [user override]: property plain generic */
182 m = scm_assoc (gh_append3 (function, wild, wild), settings);
183 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
184 moment = * unsmob_moment (ly_cdr (m));
186 /* fifth guess [user override]: property plain, specific for duration type */
187 m = scm_assoc (gh_append3 (function, type, wild), settings);
188 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
189 moment = * unsmob_moment (ly_cdr (m));
192 if (moment.to_bool ())
194 /* Ugh? measurePosition can be negative, when \partial
195 We may have to fix this elsewhere (timing translator)
196 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
198 Moment pos = * unsmob_moment (get_property ("measurePosition"));
199 if (pos < Moment (0))
201 Moment length = * unsmob_moment (get_property ("measureLength"));
204 r = pos.main_part_.mod_rat (moment.main_part_);
209 /* if undefined, starting is ok */
212 /* but ending is not */
220 Auto_beam_engraver::consider_begin (Moment test_mom)
222 bool on = to_boolean (get_property ("autoBeaming"));
225 bool b = test_moment (START, test_mom);
232 Auto_beam_engraver::consider_end (Moment test_mom)
236 /* Allow already started autobeam to end:
237 don't check for autoBeaming */
238 bool b = test_moment (STOP, test_mom);
245 Auto_beam_engraver::create_beam ()
247 if (to_boolean (get_property ("skipTypesetting")))
252 Spanner* beam = new Spanner (beam_settings_);
253 for (int i = 0; i < stems_->size (); i++)
256 watch out for stem tremolos and abbreviation beams
258 if (Stem::get_beam ((*stems_)[i]))
260 scm_gc_unprotect_object (beam->self_scm ());
263 Beam::add_stem (beam, (*stems_)[i]);
266 announce_grob(beam, SCM_EOL);
272 Auto_beam_engraver::begin_beam ()
274 if (stems_ || grouping_ )
276 programming_error ("already have autobeam");
280 stems_ = new Link_array<Item>;
281 grouping_ = new Beaming_info_list;
282 beam_settings_ = updated_grob_properties (daddy_context_, ly_symbol2scm ("Beam"));
284 beam_start_moment_ = now_mom ();
285 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
286 subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
287 beat_length_ = *unsmob_moment (get_property ("beatLength"));
291 Auto_beam_engraver::junk_beam ()
300 beam_settings_ = SCM_EOL;
302 shortest_mom_ = Moment (Rational (1, 8));
306 Auto_beam_engraver::end_beam ()
308 if (stems_->size () < 2)
314 finished_beam_ = create_beam ();
316 finished_grouping_ = grouping_;
320 beam_settings_ = SCM_EOL;
323 shortest_mom_ = Moment (Rational (1, 8));
327 Auto_beam_engraver::typeset_beam ()
331 finished_grouping_->beamify(beat_length_, subdivide_beams_);
332 Beam::set_beaming (finished_beam_, finished_grouping_);
333 typeset_grob (finished_beam_);
336 delete finished_grouping_;
337 finished_grouping_= 0;
342 Auto_beam_engraver::start_translation_timestep ()
346 don't beam over skips
350 Moment now = now_mom ();
351 if (extend_mom_ < now)
359 Auto_beam_engraver::stop_translation_timestep ()
365 Auto_beam_engraver::finalize ()
367 /* finished beams may be typeset */
369 /* but unfinished may need another announce/acknowledge pass */
376 Auto_beam_engraver::acknowledge_grob (Grob_info info)
380 if (Beam::has_interface (info.grob_))
384 else if (Bar_line::has_interface (info.grob_))
388 else if (Rest::has_interface (info.grob_))
394 if (Stem::has_interface (info.grob_))
396 Item* stem = dynamic_cast<Item *> (info.grob_);
397 Music* m = info.music_cause ();
398 if (!m->is_mus_type ("rhythmic-event"))
400 programming_error ("Stem must have rhythmic structure");
405 Don't (start) auto-beam over empty stems; skips or rests
407 if (!Stem::head_count (stem))
414 if (Stem::get_beam (stem))
421 int durlog = unsmob_duration (m->get_mus_property ("duration"))->duration_log ();
434 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
438 Moment dur = unsmob_duration (m->get_mus_property ("duration"))->get_length ();
441 This comment has been here since long:
443 if shortest duration would change
444 consider ending and beginning beam first.
446 but the code didn't match: */
449 consider_begin (dur);
451 if (dur < shortest_mom_)
454 /* I very much suspect that we wanted: */
456 consider_end (shortest_mom_);
457 if (dur < shortest_mom_)
460 consider_end (shortest_mom_);
462 consider_begin (shortest_mom_);
468 Moment now = now_mom ();
470 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
474 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
479 Auto_beam_engraver::process_acknowledged_grobs ()
483 consider_end (shortest_mom_);
484 consider_begin (shortest_mom_);
490 Moment now = now_mom ();
491 if ((extend_mom_ < now)
492 || ((extend_mom_ == now) && (last_add_mom_ != now)))
496 else if (!stems_->size ())
506 ENTER_DESCRIPTION (Auto_beam_engraver,
507 /* descr */ "Generate beams based on measure characteristics and observed "
508 "Stems. Uses beatLength, measureLength and measurePosition to decide "
509 "when to start and stop a beam. Overriding beaming is done through "
510 "@ref{Stem_engraver} properties stemLeftBeamCount and "
511 "stemRightBeamCount. "
515 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
516 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",