2 auto-beam-engraver.cc -- implement Auto_beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2003 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 TODO: figure what to do in grace?
28 class Auto_beam_engraver : public Engraver
30 TRANSLATOR_DECLARATIONS(Auto_beam_engraver);
32 virtual void stop_translation_timestep ();
33 virtual void start_translation_timestep ();
34 virtual void finalize ();
35 virtual void acknowledge_grob (Grob_info);
36 virtual void process_acknowledged_grobs ();
39 bool test_moment (Direction, Moment);
40 void consider_begin (Moment);
41 void consider_end (Moment);
42 Spanner* create_beam ();
46 bool same_grace_state_b (Grob* e);
50 shortest_mom is the shortest note in the beam.
53 Spanner *finished_beam_;
54 Link_array<Item>* stems_;
60 Projected ending of the beam we're working on.
63 Moment beam_start_moment_;
64 Moment beam_start_location_;
66 bool subdivide_beams_;
69 // We act as if beam were created, and start a grouping anyway.
70 Beaming_info_list*grouping_;
71 SCM beam_settings_ ; // ugh. should protect ?
73 Beaming_info_list*finished_grouping_;
78 Auto_beam_engraver::Auto_beam_engraver ()
82 shortest_mom_ = Moment (Rational (1, 8));
84 finished_grouping_ = 0;
86 beam_settings_ = SCM_EOL;
90 Determine end moment for auto beaming (or begin moment, but mostly
91 0==anywhere) In order of increasing priority:
93 i. begin anywhere, end at every beat
95 iii. end <type> <num> <den>
103 [to be defined in config file]
104 i. easy catch-all rule
105 ii. exceptions for time signature
106 iii. exceptions for time signature, for specific duration type
110 v. override for specific duration type
114 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
116 Moment now = now_mom();
123 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
126 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
128 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
130 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
131 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
132 int den = one_beat.den ();
133 SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
135 SCM type = scm_list_n (scm_int2num (test_mom.num ()),
136 scm_int2num (test_mom.den ()), SCM_UNDEFINED);
138 SCM settings = get_property ("autoBeamSettings");
142 /* begin beam at any position
143 (and fallback for end) */
146 /* end beam at end of beat */
149 SCM beat (get_property ("beatLength"));
151 if (unsmob_moment (beat))
152 moment = *unsmob_moment (beat);
155 /* second guess: property generic time exception */
156 SCM m = scm_assoc (gh_append3 (function, wild, time), settings);
158 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
159 moment = * unsmob_moment (ly_cdr (m));
161 /* third guess: property time exception, specific for duration type */
162 m = scm_assoc (gh_append3 (function, type, time), settings);
163 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
164 moment = * unsmob_moment (ly_cdr (m));
166 /* fourth guess [user override]: property plain generic */
167 m = scm_assoc (gh_append3 (function, wild, wild), settings);
168 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
169 moment = * unsmob_moment (ly_cdr (m));
171 /* fifth guess [user override]: property plain, specific for duration type */
172 m = scm_assoc (gh_append3 (function, type, wild), settings);
173 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
174 moment = * unsmob_moment (ly_cdr (m));
177 if (moment.to_bool ())
179 /* Ugh? measurePosition can be negative, when \partial
180 We may have to fix this elsewhere (timing translator)
181 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
183 Moment pos = * unsmob_moment (get_property ("measurePosition"));
184 if (pos < Moment (0))
186 Moment length = * unsmob_moment (get_property ("measureLength"));
189 r = pos.main_part_.mod_rat (moment.main_part_);
194 /* if undefined, starting is ok */
197 /* but ending is not */
205 Auto_beam_engraver::consider_begin (Moment test_mom)
207 bool on = to_boolean (get_property ("autoBeaming"));
210 bool b = test_moment (START, test_mom);
217 Auto_beam_engraver::consider_end (Moment test_mom)
221 /* Allow already started autobeam to end:
222 don't check for autoBeaming */
223 bool b = test_moment (STOP, test_mom);
230 Auto_beam_engraver::create_beam ()
232 if (to_boolean (get_property ("skipTypesetting")))
237 Spanner* beam = new Spanner (beam_settings_);
238 for (int i = 0; i < stems_->size (); i++)
241 watch out for stem tremolos and abbreviation beams
243 if (Stem::get_beam ((*stems_)[i]))
245 scm_gc_unprotect_object (beam->self_scm ());
248 Beam::add_stem (beam, (*stems_)[i]);
251 announce_grob(beam, SCM_EOL);
257 Auto_beam_engraver::begin_beam ()
260 stems_ = new Link_array<Item>;
262 grouping_ = new Beaming_info_list;
263 beam_settings_ = get_property ("Beam");
265 beam_start_moment_ = now_mom ();
266 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
267 subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
268 beat_length_ = *unsmob_moment (get_property ("beatLength"));
273 Auto_beam_engraver::junk_beam ()
281 beam_settings_ = SCM_EOL;
283 shortest_mom_ = Moment (Rational (1, 8));
287 Auto_beam_engraver::end_beam ()
289 if (stems_->size () < 2)
296 finished_beam_ = create_beam ();
298 finished_grouping_ = grouping_;
302 beam_settings_ = SCM_EOL;
305 shortest_mom_ = Moment (Rational (1, 8));
309 Auto_beam_engraver::typeset_beam ()
313 finished_grouping_->beamify(beat_length_, subdivide_beams_);
314 Beam::set_beaming (finished_beam_, finished_grouping_);
315 typeset_grob (finished_beam_);
318 delete finished_grouping_;
319 finished_grouping_= 0;
324 Auto_beam_engraver::start_translation_timestep ()
328 don't beam over skips
332 Moment now = now_mom ();
333 if (extend_mom_ < now)
341 Auto_beam_engraver::stop_translation_timestep ()
347 Auto_beam_engraver::finalize ()
349 /* finished beams may be typeset */
351 /* but unfinished may need another announce/acknowledge pass */
358 Auto_beam_engraver::acknowledge_grob (Grob_info info)
362 if (Beam::has_interface (info.grob_))
366 else if (Bar_line::has_interface (info.grob_))
370 else if (Rest::has_interface (info.grob_))
376 if (Stem::has_interface (info.grob_))
378 Item* stem = dynamic_cast<Item *> (info.grob_);
379 Music* m = info.music_cause ();
380 if (!m->is_mus_type ("rhythmic-event"))
382 programming_error ("Stem must have rhythmic structure");
387 Don't (start) auto-beam over empty stems; skips or rests
389 if (!Stem::head_count (stem))
396 if (Stem::get_beam (stem))
403 int durlog = unsmob_duration (m->get_mus_property ("duration"))->duration_log ();
416 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
420 Moment dur = unsmob_duration (m->get_mus_property ("duration"))->get_length ();
423 This comment has been here since long:
425 if shortest duration would change
426 consider ending and beginning beam first.
428 but the code didn't match: */
431 consider_begin (dur);
433 if (dur < shortest_mom_)
436 /* I very much suspect that we wanted: */
438 consider_end (shortest_mom_);
439 if (dur < shortest_mom_)
442 consider_end (shortest_mom_);
444 consider_begin (shortest_mom_);
450 Moment now = now_mom ();
452 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
456 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
461 Auto_beam_engraver::process_acknowledged_grobs ()
465 consider_end (shortest_mom_);
466 consider_begin (shortest_mom_);
472 Moment now = now_mom ();
473 if ((extend_mom_ < now)
474 || ((extend_mom_ == now) && (last_add_mom_ != now)))
478 else if (!stems_->size ())
488 auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
492 ENTER_DESCRIPTION (Auto_beam_engraver,
493 /* descr */ "Generate beams based on measure characteristics and observed "
494 "Stems. Uses beatLength, measureLength and measurePosition to decide "
495 "when to start and stop a beam. Overriding beaming is done through "
496 "@ref{Stem_engraver} properties stemLeftBeamCount and "
497 "stemRightBeamCount. "
501 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
502 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",