2 auto-beam-engraver.cc -- implement Auto_beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2005 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "engraver.hh"
15 #include "bar-line.hh"
21 class Auto_beam_engraver : public Engraver
23 TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
26 virtual void stop_translation_timestep ();
27 virtual void start_translation_timestep ();
28 virtual void process_music ();
29 virtual bool try_music (Music*);
30 virtual void finalize ();
31 virtual void acknowledge_grob (Grob_info);
32 virtual void process_acknowledged_grobs ();
35 bool test_moment (Direction, Moment);
36 void consider_begin (Moment);
37 void consider_end (Moment);
38 Spanner* create_beam ();
42 bool is_same_grace_state (Grob* e);
47 shortest_mom is the shortest note in the beam.
50 Spanner *finished_beam_;
51 Link_array<Item>* stems_;
57 Projected ending of the beam we're working on.
60 Moment beam_start_moment_;
61 Moment beam_start_location_;
63 bool subdivide_beams_;
66 // We act as if beam were created, and start a grouping anyway.
67 Beaming_info_list*grouping_;
68 SCM beam_settings_ ; // ugh. should protect ?
70 Beaming_info_list*finished_grouping_;
74 Auto_beam_engraver::process_music ()
76 if (scm_is_string (get_property ("whichBar")))
78 consider_end (shortest_mom_);
84 consider_end (shortest_mom_);
90 Auto_beam_engraver::Auto_beam_engraver ()
95 shortest_mom_ = Moment (Rational (1, 8));
97 finished_grouping_ = 0;
99 beam_settings_ = SCM_EOL;
104 Auto_beam_engraver::try_music (Music*m)
106 if (m->is_mus_type ("beam-forbid-event"))
116 Determine end moment for auto beaming (or begin moment, but mostly
117 0== anywhere) In order of increasing priority:
119 i. begin anywhere, end at every beat
120 ii. end * <num> <den>
121 iii. end <type> <num> <den>
129 [to be defined in config file]
130 i. easy catch-all rule
131 ii. exceptions for time signature
132 iii. exceptions for time signature, for specific duration type
136 v. override for specific duration type
140 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
142 Moment now = now_mom ();
149 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
152 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
154 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
156 Moment beat_length (1, 4);
157 if (Moment * m = unsmob_moment (get_property ("beatLength")))
161 Moment measure_length (1, 1);
163 if (Moment* m = unsmob_moment (get_property ("measureLength")))
165 num = int ((*m / beat_length).main_part_);
168 int den = beat_length.den ();
169 SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
171 SCM type = scm_list_n (scm_int2num (test_mom.num ()),
172 scm_int2num (test_mom.den ()), SCM_UNDEFINED);
176 settings aren't grob-properties.
178 SCM settings = get_property ("autoBeamSettings");
182 /* begin beam at any position
183 (and fallback for end) */
186 /* end beam at end of beat */
189 moment = robust_scm2moment (get_property ("beatLength"), moment);
192 /* second guess: property generic time exception */
193 SCM m = scm_assoc (ly_append3 (function, wild, time), settings);
195 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
196 moment = * unsmob_moment (scm_cdr (m));
198 /* third guess: property time exception, specific for duration type */
199 m = scm_assoc (ly_append3 (function, type, time), settings);
200 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
201 moment = * unsmob_moment (scm_cdr (m));
203 /* fourth guess [user override]: property plain generic */
204 m = scm_assoc (ly_append3 (function, wild, wild), settings);
205 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
206 moment = * unsmob_moment (scm_cdr (m));
208 /* fifth guess [user override]: property plain, specific for duration type */
209 m = scm_assoc (ly_append3 (function, type, wild), settings);
210 if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
211 moment = * unsmob_moment (scm_cdr (m));
214 if (moment.to_bool ())
216 /* Ugh? measurePosition can be negative, when \partial
217 We may have to fix this elsewhere (timing translator)
218 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
221 if (Moment *m = unsmob_moment (get_property ("measurePosition")))
226 if (pos < Moment (0))
230 if ( Moment *m = unsmob_moment (get_property ("measureLength")))
236 r = pos.main_part_.mod_rat (moment.main_part_);
241 /* if undefined, starting is ok */
244 /* but ending is not */
252 Auto_beam_engraver::consider_begin (Moment test_mom)
254 bool on = to_boolean (get_property ("autoBeaming"));
258 bool b = test_moment (START, test_mom);
265 Auto_beam_engraver::consider_end (Moment test_mom)
269 /* Allow already started autobeam to end:
270 don't check for autoBeaming */
271 bool b = test_moment (STOP, test_mom);
278 Auto_beam_engraver::create_beam ()
280 if (to_boolean (get_property ("skipTypesetting")))
283 Spanner *beam = new Spanner (beam_settings_, context ()->get_grob_key ("Beam"));
284 for (int i = 0; i < stems_->size (); i++)
287 watch out for stem tremolos and abbreviation beams
289 if (Stem::get_beam ((*stems_)[i]))
291 scm_gc_unprotect_object (beam->self_scm ());
294 Beam::add_stem (beam, (*stems_)[i]);
297 announce_grob (beam, (*stems_)[0]->self_scm ());
303 Auto_beam_engraver::begin_beam ()
305 if (stems_ || grouping_ )
307 programming_error ("already have autobeam");
311 stems_ = new Link_array<Item>;
312 grouping_ = new Beaming_info_list;
313 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
315 beam_start_moment_ = now_mom ();
316 beam_start_location_ =
317 robust_scm2moment (get_property ("measurePosition"), Moment(0));
318 subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
319 beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment(1, 4));
323 Auto_beam_engraver::junk_beam ()
332 beam_settings_ = SCM_EOL;
334 shortest_mom_ = Moment (Rational (1, 8));
338 Auto_beam_engraver::end_beam ()
340 if (stems_->size () < 2)
346 finished_beam_ = create_beam ();
348 finished_grouping_ = grouping_;
352 beam_settings_ = SCM_EOL;
355 shortest_mom_ = Moment (Rational (1, 8));
359 Auto_beam_engraver::typeset_beam ()
363 finished_grouping_->beamify (beat_length_, subdivide_beams_);
364 Beam::set_beaming (finished_beam_, finished_grouping_);
367 delete finished_grouping_;
368 finished_grouping_ = 0;
373 Auto_beam_engraver::start_translation_timestep ()
377 don't beam over skips
381 Moment now = now_mom ();
382 if (extend_mom_ < now)
391 Auto_beam_engraver::stop_translation_timestep ()
397 Auto_beam_engraver::finalize ()
399 /* finished beams may be typeset */
401 /* but unfinished may need another announce/acknowledge pass */
408 Auto_beam_engraver::acknowledge_grob (Grob_info info)
411 Duplicated from process_music(), since
412 Repeat_acknowledge_engraver::process_music() may also set whichBar
414 if (scm_is_string (get_property ("whichBar"))
415 && beam_start_moment_ < now_mom())
417 consider_end (shortest_mom_);
423 if (Beam::has_interface (info.grob_))
427 else if (Bar_line::has_interface (info.grob_))
431 else if (Rest::has_interface (info.grob_))
437 if (Stem::has_interface (info.grob_))
439 Item* stem = dynamic_cast<Item *> (info.grob_);
440 Music* m = info.music_cause ();
441 if (!m->is_mus_type ("rhythmic-event"))
443 programming_error ("Stem must have rhythmic structure");
448 Don't (start) auto-beam over empty stems; skips or rests
450 if (!Stem::head_count (stem))
457 if (Stem::get_beam (stem))
464 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
477 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
481 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
484 This comment has been here since long:
486 if shortest duration would change
487 consider ending and beginning beam first.
489 but the code didn't match: */
492 consider_begin (dur);
494 if (dur < shortest_mom_)
497 /* I very much suspect that we wanted: */
499 consider_end (shortest_mom_);
500 if (dur < shortest_mom_)
503 consider_end (shortest_mom_);
505 consider_begin (shortest_mom_);
511 Moment now = now_mom ();
513 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
517 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
522 Auto_beam_engraver::process_acknowledged_grobs ()
526 consider_end (shortest_mom_);
527 consider_begin (shortest_mom_);
533 Moment now = now_mom ();
534 if ((extend_mom_ < now)
535 || ((extend_mom_ == now) && (last_add_mom_ != now)))
539 else if (!stems_->size ())
549 ADD_TRANSLATOR (Auto_beam_engraver,
550 /* descr */ "Generate beams based on measure characteristics and observed "
551 "Stems. Uses beatLength, measureLength and measurePosition to decide "
552 "when to start and stop a beam. Overriding beaming is done through "
553 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
554 "@code{stemRightBeamCount}. "
557 /* accepts */ "beam-forbid-event",
558 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
559 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",