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"));
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)
294 finished_beam_ = create_beam ();
296 finished_grouping_ = grouping_;
300 beam_settings_ = SCM_EOL;
303 shortest_mom_ = Moment (Rational (1, 8));
307 Auto_beam_engraver::typeset_beam ()
311 finished_grouping_->beamify(beat_length_, subdivide_beams_);
312 Beam::set_beaming (finished_beam_, finished_grouping_);
313 typeset_grob (finished_beam_);
316 delete finished_grouping_;
317 finished_grouping_= 0;
322 Auto_beam_engraver::start_translation_timestep ()
326 don't beam over skips
330 Moment now = now_mom ();
331 if (extend_mom_ < now)
339 Auto_beam_engraver::stop_translation_timestep ()
345 Auto_beam_engraver::finalize ()
347 /* finished beams may be typeset */
349 /* but unfinished may need another announce/acknowledge pass */
356 Auto_beam_engraver::acknowledge_grob (Grob_info info)
360 if (Beam::has_interface (info.grob_))
364 else if (Bar_line::has_interface (info.grob_))
368 else if (Rest::has_interface (info.grob_))
374 if (Stem::has_interface (info.grob_))
376 Item* stem = dynamic_cast<Item *> (info.grob_);
377 Music* m = info.music_cause ();
378 if (!m->is_mus_type ("rhythmic-event"))
380 programming_error ("Stem must have rhythmic structure");
385 Don't (start) auto-beam over empty stems; skips or rests
387 if (!Stem::head_count (stem))
394 if (Stem::get_beam (stem))
401 int durlog = unsmob_duration (m->get_mus_property ("duration"))->duration_log ();
414 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
418 Moment dur = unsmob_duration (m->get_mus_property ("duration"))->get_length ();
421 This comment has been here since long:
423 if shortest duration would change
424 consider ending and beginning beam first.
426 but the code didn't match: */
429 consider_begin (dur);
431 if (dur < shortest_mom_)
434 /* I very much suspect that we wanted: */
436 consider_end (shortest_mom_);
437 if (dur < shortest_mom_)
440 consider_end (shortest_mom_);
442 consider_begin (shortest_mom_);
448 Moment now = now_mom ();
450 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
454 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
459 Auto_beam_engraver::process_acknowledged_grobs ()
463 consider_end (shortest_mom_);
464 consider_begin (shortest_mom_);
470 Moment now = now_mom ();
471 if ((extend_mom_ < now)
472 || ((extend_mom_ == now) && (last_add_mom_ != now)))
476 else if (!stems_->size ())
486 ENTER_DESCRIPTION (Auto_beam_engraver,
487 /* descr */ "Generate beams based on measure characteristics and observed "
488 "Stems. Uses beatLength, measureLength and measurePosition to decide "
489 "when to start and stop a beam. Overriding beaming is done through "
490 "@ref{Stem_engraver} properties stemLeftBeamCount and "
491 "stemRightBeamCount. "
495 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
496 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",