2 auto-beam-engraver.cc -- implement Auto_beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2002 Jan Nieuwenhuizen <janneke@gnu.org>
11 #include "musical-request.hh"
15 #include "engraver-group-engraver.hh"
16 #include "bar-line.hh"
18 #include "engraver.hh"
23 TODO: figure what to do in grace?
27 class Auto_beam_engraver : public Engraver
29 TRANSLATOR_DECLARATIONS(Auto_beam_engraver);
31 virtual void stop_translation_timestep ();
32 virtual void start_translation_timestep ();
33 virtual void finalize ();
34 virtual void acknowledge_grob (Grob_info);
35 virtual void process_acknowledged_grobs ();
38 bool test_moment (Direction, Moment);
39 void consider_begin (Moment);
40 void consider_end (Moment);
41 Spanner* create_beam_p ();
45 bool same_grace_state_b (Grob* e);
49 shortest_mom is the shortest note in the beam.
52 Spanner *finished_beam_p_;
53 Link_array<Item>* stem_l_arr_p_;
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_p_;
70 Beaming_info_list*finished_grouping_p_;
75 Auto_beam_engraver::Auto_beam_engraver ()
79 shortest_mom_ = Moment (Rational (1, 8));
81 finished_grouping_p_ = 0;
86 Determine end moment for auto beaming (or begin moment, but mostly
87 0==anywhere) In order of increasing priority:
89 i. begin anywhere, end at every beat
91 iii. end <type> <num> <den>
99 [to be defined in config file]
100 i. easy catch-all rule
101 ii. exceptions for time signature
102 iii. exceptions for time signature, for specific duration type
106 v. override for specific duration type
110 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
112 Moment now = now_mom();
119 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
122 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
124 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
126 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
127 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
128 int den = one_beat.den ();
129 SCM time = scm_list_n (gh_int2scm (num), gh_int2scm (den), SCM_UNDEFINED);
131 SCM type = scm_list_n (gh_int2scm (test_mom.num ()),
132 gh_int2scm (test_mom.den ()), SCM_UNDEFINED);
134 SCM settings = get_property ("autoBeamSettings");
138 /* begin beam at any position
139 (and fallback for end) */
142 /* end beam at end of beat */
145 SCM beat (get_property ("beatLength"));
147 if (unsmob_moment (beat))
148 moment = *unsmob_moment (beat);
151 /* second guess: property generic time exception */
152 SCM m = gh_assoc (gh_append3 (function, wild, time), settings);
154 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
155 moment = * unsmob_moment (ly_cdr (m));
157 /* third guess: property time exception, specific for duration type */
158 m = gh_assoc (gh_append3 (function, type, time), settings);
159 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
160 moment = * unsmob_moment (ly_cdr (m));
162 /* fourth guess [user override]: property plain generic */
163 m = gh_assoc (gh_append3 (function, wild, wild), settings);
164 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
165 moment = * unsmob_moment (ly_cdr (m));
167 /* fifth guess [user override]: property plain, specific for duration type */
168 m = gh_assoc (gh_append3 (function, type, wild), settings);
169 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
170 moment = * unsmob_moment (ly_cdr (m));
173 if (moment.to_bool ())
175 /* Ugh? measurePosition can be negative, when \partial
176 We may have to fix this elsewhere (timing translator)
177 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
179 Moment pos = * unsmob_moment (get_property ("measurePosition"));
180 if (pos < Moment (0))
182 Moment length = * unsmob_moment (get_property ("measureLength"));
185 r = pos.main_part_.mod_rat (moment.main_part_);
190 /* if undefined, starting is ok */
193 /* but ending is not */
201 Auto_beam_engraver::consider_begin (Moment test_mom)
203 bool on = to_boolean (get_property ("autoBeaming"));
204 if (!stem_l_arr_p_ && on)
206 bool b = test_moment (START, test_mom);
213 Auto_beam_engraver::consider_end (Moment test_mom)
217 /* Allow already started autobeam to end:
218 don't check for autoBeaming */
219 bool b = test_moment (STOP, test_mom);
226 Auto_beam_engraver::create_beam_p ()
228 if (to_boolean (get_property ("skipTypesetting")))
233 Spanner* beam_p = new Spanner (get_property ("Beam"));
234 for (int i = 0; i < stem_l_arr_p_->size (); i++)
237 watch out for stem tremolos and abbreviation beams
239 if (Stem::beam_l ((*stem_l_arr_p_)[i]))
241 scm_gc_unprotect_object (beam_p->self_scm ());
244 Beam::add_stem (beam_p, (*stem_l_arr_p_)[i]);
247 announce_grob(beam_p, SCM_EOL);
253 Auto_beam_engraver::begin_beam ()
255 assert (!stem_l_arr_p_);
256 stem_l_arr_p_ = new Link_array<Item>;
257 assert (!grouping_p_);
258 grouping_p_ = new Beaming_info_list;
259 beam_start_moment_ = now_mom ();
260 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
261 subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
262 beat_length_ = *unsmob_moment (get_property ("beatLength"));
267 Auto_beam_engraver::junk_beam ()
269 assert (stem_l_arr_p_);
271 delete stem_l_arr_p_;
276 shortest_mom_ = Moment (Rational (1, 8));
280 Auto_beam_engraver::end_beam ()
282 if (stem_l_arr_p_->size () < 2)
289 finished_beam_p_ = create_beam_p ();
290 if (finished_beam_p_)
291 finished_grouping_p_ = grouping_p_;
292 delete stem_l_arr_p_;
297 shortest_mom_ = Moment (Rational (1, 8));
301 Auto_beam_engraver::typeset_beam ()
303 if (finished_beam_p_)
305 finished_grouping_p_->beamify(beat_length_, subdivide_beams_);
306 Beam::set_beaming (finished_beam_p_, finished_grouping_p_);
307 typeset_grob (finished_beam_p_);
308 finished_beam_p_ = 0;
310 delete finished_grouping_p_;
311 finished_grouping_p_= 0;
316 Auto_beam_engraver::start_translation_timestep ()
320 don't beam over skips
324 Moment now = now_mom ();
325 if (extend_mom_ < now)
333 Auto_beam_engraver::stop_translation_timestep ()
339 Auto_beam_engraver::finalize ()
341 /* finished beams may be typeset */
343 /* but unfinished may need another announce/acknowledge pass */
350 Auto_beam_engraver::acknowledge_grob (Grob_info info)
354 if (Beam::has_interface (info.grob_l_))
358 else if (Bar_line::has_interface (info.grob_l_))
362 else if (Rest::has_interface (info.grob_l_))
368 if (Stem::has_interface (info.grob_l_))
370 Item* stem_l = dynamic_cast<Item *> (info.grob_l_);
372 Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.music_cause ());
375 programming_error ("Stem must have rhythmic structure");
380 Don't (start) auto-beam over empty stems; skips or rests
382 if (!Stem::head_count (stem_l))
389 if (Stem::beam_l (stem_l))
396 int durlog = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->duration_log ();
409 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
413 Moment dur = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->length_mom ();
416 This comment has been here since long:
418 if shortest duration would change
419 consider ending and beginning beam first.
421 but the code didn't match: */
424 consider_begin (dur);
426 if (dur < shortest_mom_)
429 /* I very much suspect that we wanted: */
431 consider_end (shortest_mom_);
432 if (dur < shortest_mom_)
435 consider_end (shortest_mom_);
437 consider_begin (shortest_mom_);
443 Moment now = now_mom ();
445 grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
447 stem_l_arr_p_->push (stem_l);
449 extend_mom_ = (extend_mom_ >? now) + rhythmic_req->length_mom ();
454 Auto_beam_engraver::process_acknowledged_grobs ()
458 consider_end (shortest_mom_);
459 consider_begin (shortest_mom_);
461 else if (count_i_ > 1)
465 Moment now = now_mom ();
466 if ((extend_mom_ < now)
467 || ((extend_mom_ == now) && (last_add_mom_ != now)))
471 else if (!stem_l_arr_p_->size ())
481 auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
482 count_i_ = count_i_ + 1;
484 ENTER_DESCRIPTION(Auto_beam_engraver,
485 /* descr */ "Generate beams based on measure characteristics and observed
486 Stems. Uses beatLength, measureLength and measurePosition to decide
487 when to start and stop a beam. Overriding beaming is done through
488 @ref{Stem_engraver} properties stemLeftBeamCount and
492 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
493 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",