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 create_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_;
67 // We act as if beam were created, and start a grouping anyway.
68 Beaming_info_list*grouping_p_;
69 Beaming_info_list*finished_grouping_p_;
74 Auto_beam_engraver::Auto_beam_engraver ()
78 shortest_mom_ = Moment (Rational (1, 8));
80 finished_grouping_p_ = 0;
85 Determine end moment for auto beaming (or begin moment, but mostly
86 0==anywhere) In order of increasing priority:
88 i. begin anywhere, end at every beat
90 iii. end <type> <num> <den>
98 [to be defined in config file]
99 i. easy catch-all rule
100 ii. exceptions for time signature
101 iii. exceptions for time signature, for specific duration type
105 v. override for specific duration type
109 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
111 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
114 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
116 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
118 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
119 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
120 int den = one_beat.den ();
121 SCM time = scm_list_n (gh_int2scm (num), gh_int2scm (den), SCM_UNDEFINED);
123 SCM type = scm_list_n (gh_int2scm (test_mom.num ()),
124 gh_int2scm (test_mom.den ()), SCM_UNDEFINED);
126 SCM settings = get_property ("autoBeamSettings");
130 /* begin beam at any position
131 (and fallback for end) */
134 /* end beam at end of beat */
137 SCM beat (get_property ("beatLength"));
139 if (unsmob_moment (beat))
140 moment = *unsmob_moment (beat);
143 /* second guess: property generic time exception */
144 SCM m = gh_assoc (gh_append3 (function, wild, time), settings);
146 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
147 moment = * unsmob_moment (ly_cdr (m));
149 /* third guess: property time exception, specific for duration type */
150 m = gh_assoc (gh_append3 (function, type, time), settings);
151 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
152 moment = * unsmob_moment (ly_cdr (m));
154 /* fourth guess [user override]: property plain generic */
155 m = gh_assoc (gh_append3 (function, wild, wild), settings);
156 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
157 moment = * unsmob_moment (ly_cdr (m));
159 /* fifth guess [user override]: property plain, specific for duration type */
160 m = gh_assoc (gh_append3 (function, type, wild), settings);
161 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
162 moment = * unsmob_moment (ly_cdr (m));
165 if (moment.to_bool ())
167 /* Ugh? measurePosition can be negative, when \partial
168 We may have to fix this elsewhere (timing translator)
169 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
171 Moment pos = * unsmob_moment (get_property ("measurePosition"));
172 if (pos < Moment (0))
174 Moment length = * unsmob_moment (get_property ("measureLength"));
177 r = pos.main_part_.mod_rat (moment.main_part_);
182 /* if undefined, starting is ok */
185 /* but ending is not */
192 Auto_beam_engraver::consider_begin (Moment test_mom)
194 bool on = to_boolean (get_property ("autoBeaming"));
195 if (!stem_l_arr_p_ && on)
197 bool b = test_moment (START, test_mom);
204 Auto_beam_engraver::consider_end (Moment test_mom)
208 /* Allow already started autobeam to end:
209 don't check for autoBeaming */
210 bool b = test_moment (STOP, test_mom);
217 Auto_beam_engraver::create_beam_p ()
219 if (to_boolean (get_property ("skipTypesetting")))
224 Spanner* beam_p = new Spanner (get_property ("Beam"));
225 for (int i = 0; i < stem_l_arr_p_->size (); i++)
228 watch out for stem tremolos and abbreviation beams
230 if (Stem::beam_l ((*stem_l_arr_p_)[i]))
232 scm_gc_unprotect_object (beam_p->self_scm ());
235 Beam::add_stem (beam_p, (*stem_l_arr_p_)[i]);
238 announce_grob(beam_p, SCM_EOL);
244 Auto_beam_engraver::begin_beam ()
246 assert (!stem_l_arr_p_);
247 stem_l_arr_p_ = new Link_array<Item>;
248 assert (!grouping_p_);
249 grouping_p_ = new Beaming_info_list;
250 beam_start_moment_ = now_mom ();
251 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
252 subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
257 Auto_beam_engraver::junk_beam ()
259 assert (stem_l_arr_p_);
261 delete stem_l_arr_p_;
266 shortest_mom_ = Moment (Rational (1, 8));
270 Auto_beam_engraver::end_beam ()
272 if (stem_l_arr_p_->size () < 2)
279 finished_beam_p_ = create_beam_p ();
280 if (finished_beam_p_)
281 finished_grouping_p_ = grouping_p_;
282 delete stem_l_arr_p_;
287 shortest_mom_ = Moment (Rational (1, 8));
291 Auto_beam_engraver::typeset_beam ()
293 if (finished_beam_p_)
295 finished_grouping_p_->beamify(*unsmob_moment (get_property ("beatLength")),
297 Beam::set_beaming (finished_beam_p_, finished_grouping_p_);
298 typeset_grob (finished_beam_p_);
299 finished_beam_p_ = 0;
301 delete finished_grouping_p_;
302 finished_grouping_p_= 0;
307 Auto_beam_engraver::start_translation_timestep ()
311 don't beam over skips
315 Moment now = now_mom ();
316 if (extend_mom_ < now)
324 Auto_beam_engraver::stop_translation_timestep ()
330 Auto_beam_engraver::finalize ()
332 /* finished beams may be typeset */
334 /* but unfinished may need another announce/acknowledge pass */
341 Auto_beam_engraver::acknowledge_grob (Grob_info info)
345 if (Beam::has_interface (info.grob_l_))
349 else if (Bar_line::has_interface (info.grob_l_))
353 else if (Rest::has_interface (info.grob_l_))
359 if (Stem::has_interface (info.grob_l_))
361 Item* stem_l = dynamic_cast<Item *> (info.grob_l_);
363 Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.music_cause ());
366 programming_error ("Stem must have rhythmic structure");
371 Don't (start) auto-beam over empty stems; skips or rests
373 if (!Stem::head_count (stem_l))
380 if (Stem::beam_l (stem_l))
387 int durlog = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->duration_log ();
400 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
404 Moment dur = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->length_mom ();
407 This comment has been here since long:
409 if shortest duration would change
410 consider ending and beginning beam first.
412 but the code didn't match: */
415 consider_begin (dur);
417 if (dur < shortest_mom_)
420 /* I very much suspect that we wanted: */
422 consider_end (shortest_mom_);
423 if (dur < shortest_mom_)
426 consider_end (shortest_mom_);
428 consider_begin (shortest_mom_);
434 Moment now = now_mom ();
436 grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
438 stem_l_arr_p_->push (stem_l);
440 extend_mom_ = (extend_mom_ >? now) + rhythmic_req->length_mom ();
445 Auto_beam_engraver::create_grobs ()
449 consider_end (shortest_mom_);
450 consider_begin (shortest_mom_);
452 else if (count_i_ > 1)
456 Moment now = now_mom ();
457 if ((extend_mom_ < now)
458 || ((extend_mom_ == now) && (last_add_mom_ != now)))
462 else if (!stem_l_arr_p_->size ())
472 auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
473 count_i_ = count_i_ + 1;
475 ENTER_DESCRIPTION(Auto_beam_engraver,
476 /* descr */ "Generate beams based on measure characteristics and observed
477 Stems. Uses beatLength, measureLength and measurePosition to decide
478 when to start and stop a beam. Overriding beaming is done through
479 @ref{Stem_engraver} properties stemLeftBeamCount and
483 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
484 /* reads */ "autoBeaming autoBeamSettings subdivideBeams",