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 ();
45 bool same_grace_state_b (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_;
77 Auto_beam_engraver::Auto_beam_engraver ()
81 shortest_mom_ = Moment (Rational (1, 8));
83 finished_grouping_ = 0;
85 beam_settings_ = SCM_EOL;
89 Determine end moment for auto beaming (or begin moment, but mostly
90 0==anywhere) In order of increasing priority:
92 i. begin anywhere, end at every beat
94 iii. end <type> <num> <den>
102 [to be defined in config file]
103 i. easy catch-all rule
104 ii. exceptions for time signature
105 iii. exceptions for time signature, for specific duration type
109 v. override for specific duration type
113 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
115 Moment now = now_mom();
122 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
125 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
127 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
129 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
130 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
131 int den = one_beat.den ();
132 SCM time = scm_list_n (gh_int2scm (num), gh_int2scm (den), SCM_UNDEFINED);
134 SCM type = scm_list_n (gh_int2scm (test_mom.num ()),
135 gh_int2scm (test_mom.den ()), SCM_UNDEFINED);
137 SCM settings = get_property ("autoBeamSettings");
141 /* begin beam at any position
142 (and fallback for end) */
145 /* end beam at end of beat */
148 SCM beat (get_property ("beatLength"));
150 if (unsmob_moment (beat))
151 moment = *unsmob_moment (beat);
154 /* second guess: property generic time exception */
155 SCM m = gh_assoc (gh_append3 (function, wild, time), settings);
157 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
158 moment = * unsmob_moment (ly_cdr (m));
160 /* third guess: property time exception, specific for duration type */
161 m = gh_assoc (gh_append3 (function, type, time), settings);
162 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
163 moment = * unsmob_moment (ly_cdr (m));
165 /* fourth guess [user override]: property plain generic */
166 m = gh_assoc (gh_append3 (function, wild, wild), settings);
167 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
168 moment = * unsmob_moment (ly_cdr (m));
170 /* fifth guess [user override]: property plain, specific for duration type */
171 m = gh_assoc (gh_append3 (function, type, wild), settings);
172 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
173 moment = * unsmob_moment (ly_cdr (m));
176 if (moment.to_bool ())
178 /* Ugh? measurePosition can be negative, when \partial
179 We may have to fix this elsewhere (timing translator)
180 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
182 Moment pos = * unsmob_moment (get_property ("measurePosition"));
183 if (pos < Moment (0))
185 Moment length = * unsmob_moment (get_property ("measureLength"));
188 r = pos.main_part_.mod_rat (moment.main_part_);
193 /* if undefined, starting is ok */
196 /* but ending is not */
204 Auto_beam_engraver::consider_begin (Moment test_mom)
206 bool on = to_boolean (get_property ("autoBeaming"));
209 bool b = test_moment (START, test_mom);
216 Auto_beam_engraver::consider_end (Moment test_mom)
220 /* Allow already started autobeam to end:
221 don't check for autoBeaming */
222 bool b = test_moment (STOP, test_mom);
229 Auto_beam_engraver::create_beam ()
231 if (to_boolean (get_property ("skipTypesetting")))
236 Spanner* beam = new Spanner (beam_settings_);
237 for (int i = 0; i < stems_->size (); i++)
240 watch out for stem tremolos and abbreviation beams
242 if (Stem::get_beam ((*stems_)[i]))
244 scm_gc_unprotect_object (beam->self_scm ());
247 Beam::add_stem (beam, (*stems_)[i]);
250 announce_grob(beam, SCM_EOL);
256 Auto_beam_engraver::begin_beam ()
259 stems_ = new Link_array<Item>;
261 grouping_ = new Beaming_info_list;
262 beam_settings_ = get_property ("Beam");
264 beam_start_moment_ = now_mom ();
265 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
266 subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
267 beat_length_ = *unsmob_moment (get_property ("beatLength"));
272 Auto_beam_engraver::junk_beam ()
280 beam_settings_ = SCM_EOL;
282 shortest_mom_ = Moment (Rational (1, 8));
286 Auto_beam_engraver::end_beam ()
288 if (stems_->size () < 2)
295 finished_beam_ = create_beam ();
297 finished_grouping_ = grouping_;
301 beam_settings_ = SCM_EOL;
304 shortest_mom_ = Moment (Rational (1, 8));
308 Auto_beam_engraver::typeset_beam ()
312 finished_grouping_->beamify(beat_length_, subdivide_beams_);
313 Beam::set_beaming (finished_beam_, finished_grouping_);
314 typeset_grob (finished_beam_);
317 delete finished_grouping_;
318 finished_grouping_= 0;
323 Auto_beam_engraver::start_translation_timestep ()
327 don't beam over skips
331 Moment now = now_mom ();
332 if (extend_mom_ < now)
340 Auto_beam_engraver::stop_translation_timestep ()
346 Auto_beam_engraver::finalize ()
348 /* finished beams may be typeset */
350 /* but unfinished may need another announce/acknowledge pass */
357 Auto_beam_engraver::acknowledge_grob (Grob_info info)
361 if (Beam::has_interface (info.grob_))
365 else if (Bar_line::has_interface (info.grob_))
369 else if (Rest::has_interface (info.grob_))
375 if (Stem::has_interface (info.grob_))
377 Item* stem = dynamic_cast<Item *> (info.grob_);
379 Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.music_cause ());
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 (rhythmic_req->get_mus_property ("duration"))->duration_log ();
416 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
420 Moment dur = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->length_mom ();
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) + rhythmic_req->length_mom ();
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) */
491 ENTER_DESCRIPTION(Auto_beam_engraver,
492 /* descr */ "Generate beams based on measure characteristics and observed
493 Stems. Uses beatLength, measureLength and measurePosition to decide
494 when to start and stop a beam. Overriding beaming is done through
495 @ref{Stem_engraver} properties stemLeftBeamCount and
499 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
500 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",