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 (ly_c_string_p (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 one_beat = *unsmob_moment (get_property ("beatLength"));
159 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
160 int den = one_beat.den ();
161 SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
163 SCM type = scm_list_n (scm_int2num (test_mom.num ()),
164 scm_int2num (test_mom.den ()), SCM_UNDEFINED);
168 settings aren't grob-properties.
170 SCM settings = get_property ("autoBeamSettings");
174 /* begin beam at any position
175 (and fallback for end) */
178 /* end beam at end of beat */
181 SCM beat (get_property ("beatLength"));
183 if (unsmob_moment (beat))
184 moment = *unsmob_moment (beat);
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 (ly_cdr (m)))
191 moment = * unsmob_moment (ly_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 (ly_cdr (m)))
196 moment = * unsmob_moment (ly_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 (ly_cdr (m)))
201 moment = * unsmob_moment (ly_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 (ly_cdr (m)))
206 moment = * unsmob_moment (ly_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);
215 Moment pos = * unsmob_moment (get_property ("measurePosition"));
216 if (pos < Moment (0))
218 Moment length = * unsmob_moment (get_property ("measureLength"));
221 r = pos.main_part_.mod_rat (moment.main_part_);
226 /* if undefined, starting is ok */
229 /* but ending is not */
237 Auto_beam_engraver::consider_begin (Moment test_mom)
239 bool on = to_boolean (get_property ("autoBeaming"));
243 bool b = test_moment (START, test_mom);
250 Auto_beam_engraver::consider_end (Moment test_mom)
254 /* Allow already started autobeam to end:
255 don't check for autoBeaming */
256 bool b = test_moment (STOP, test_mom);
263 Auto_beam_engraver::create_beam ()
265 if (to_boolean (get_property ("skipTypesetting")))
270 Spanner* beam = new Spanner (beam_settings_);
271 for (int i = 0; i < stems_->size (); i++)
274 watch out for stem tremolos and abbreviation beams
276 if (Stem::get_beam ((*stems_)[i]))
278 scm_gc_unprotect_object (beam->self_scm ());
281 Beam::add_stem (beam, (*stems_)[i]);
284 announce_grob (beam, (*stems_)[0]->self_scm ());
290 Auto_beam_engraver::begin_beam ()
292 if (stems_ || grouping_ )
294 programming_error ("already have autobeam");
298 stems_ = new Link_array<Item>;
299 grouping_ = new Beaming_info_list;
300 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
302 beam_start_moment_ = now_mom ();
303 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
304 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
305 beat_length_ = *unsmob_moment (get_property ("beatLength"));
309 Auto_beam_engraver::junk_beam ()
318 beam_settings_ = SCM_EOL;
320 shortest_mom_ = Moment (Rational (1, 8));
324 Auto_beam_engraver::end_beam ()
326 if (stems_->size () < 2)
332 finished_beam_ = create_beam ();
334 finished_grouping_ = grouping_;
338 beam_settings_ = SCM_EOL;
341 shortest_mom_ = Moment (Rational (1, 8));
345 Auto_beam_engraver::typeset_beam ()
349 finished_grouping_->beamify (beat_length_, subdivide_beams_);
350 Beam::set_beaming (finished_beam_, finished_grouping_);
353 delete finished_grouping_;
354 finished_grouping_= 0;
359 Auto_beam_engraver::start_translation_timestep ()
363 don't beam over skips
367 Moment now = now_mom ();
368 if (extend_mom_ < now)
377 Auto_beam_engraver::stop_translation_timestep ()
383 Auto_beam_engraver::finalize ()
385 /* finished beams may be typeset */
387 /* but unfinished may need another announce/acknowledge pass */
394 Auto_beam_engraver::acknowledge_grob (Grob_info info)
398 if (Beam::has_interface (info.grob_))
402 else if (Bar_line::has_interface (info.grob_))
406 else if (Rest::has_interface (info.grob_))
412 if (Stem::has_interface (info.grob_))
414 Item* stem = dynamic_cast<Item *> (info.grob_);
415 Music* m = info.music_cause ();
416 if (!m->is_mus_type ("rhythmic-event"))
418 programming_error ("Stem must have rhythmic structure");
423 Don't (start) auto-beam over empty stems; skips or rests
425 if (!Stem::head_count (stem))
432 if (Stem::get_beam (stem))
439 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
452 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
456 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
459 This comment has been here since long:
461 if shortest duration would change
462 consider ending and beginning beam first.
464 but the code didn't match: */
467 consider_begin (dur);
469 if (dur < shortest_mom_)
472 /* I very much suspect that we wanted: */
474 consider_end (shortest_mom_);
475 if (dur < shortest_mom_)
478 consider_end (shortest_mom_);
480 consider_begin (shortest_mom_);
486 Moment now = now_mom ();
488 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
492 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
497 Auto_beam_engraver::process_acknowledged_grobs ()
501 consider_end (shortest_mom_);
502 consider_begin (shortest_mom_);
508 Moment now = now_mom ();
509 if ((extend_mom_ < now)
510 || ((extend_mom_ == now) && (last_add_mom_ != now)))
514 else if (!stems_->size ())
524 ENTER_DESCRIPTION (Auto_beam_engraver,
525 /* descr */ "Generate beams based on measure characteristics and observed "
526 "Stems. Uses beatLength, measureLength and measurePosition to decide "
527 "when to start and stop a beam. Overriding beaming is done through "
528 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
529 "@code{stemRightBeamCount}. "
532 /* accepts */ "beam-forbid-event",
533 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
534 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",