2 auto-beam-engraver.cc -- implement Auto_beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2001 Jan Nieuwenhuizen <janneke@gnu.org>
11 #include "musical-request.hh"
15 #include "engraver-group-engraver.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 // We act as if beam were created, and start a grouping anyway.
66 Beaming_info_list*grouping_p_;
67 Beaming_info_list*finished_grouping_p_;
72 Auto_beam_engraver::Auto_beam_engraver ()
76 shortest_mom_ = Moment (Rational (1, 8));
78 finished_grouping_p_ = 0;
83 Determine end moment for auto beaming (or begin moment, but mostly
84 0==anywhere) In order of increasing priority:
86 i. begin anywhere, end at every beat
88 iii. end <type> <num> <den>
96 [to be defined in config file]
97 i. easy catch-all rule
98 ii. exceptions for time signature
99 iii. exceptions for time signature, for specific duration type
103 v. override for specific duration type
107 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
109 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
112 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
114 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
116 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
117 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
118 int den = one_beat.den ();
119 SCM time = scm_list_n (gh_int2scm (num), gh_int2scm (den), SCM_UNDEFINED);
121 SCM type = scm_list_n (gh_int2scm (test_mom.num ()),
122 gh_int2scm (test_mom.den ()), SCM_UNDEFINED);
124 SCM settings = get_property ("autoBeamSettings");
128 /* begin beam at any position
129 (and fallback for end) */
132 /* end beam at end of beat */
135 SCM beat (get_property ("beatLength"));
137 if (unsmob_moment (beat))
138 moment = *unsmob_moment (beat);
141 /* second guess: property generic time exception */
142 SCM m = gh_assoc (gh_append3 (function, wild, time), settings);
144 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
145 moment = * unsmob_moment (ly_cdr (m));
147 /* third guess: property time exception, specific for duration type */
148 m = gh_assoc (gh_append3 (function, type, time), settings);
149 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
150 moment = * unsmob_moment (ly_cdr (m));
152 /* fourth guess [user override]: property plain generic */
153 m = gh_assoc (gh_append3 (function, wild, wild), settings);
154 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
155 moment = * unsmob_moment (ly_cdr (m));
157 /* fifth guess [user override]: property plain, specific for duration type */
158 m = gh_assoc (gh_append3 (function, type, wild), settings);
159 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
160 moment = * unsmob_moment (ly_cdr (m));
163 if (moment.to_bool ())
165 /* Ugh? measurePosition can be negative, when \partial
166 We may have to fix this elsewhere (timing translator)
167 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
169 Moment pos = * unsmob_moment (get_property ("measurePosition"));
170 if (pos < Moment (0))
172 Moment length = * unsmob_moment (get_property ("measureLength"));
175 r = pos.main_part_.mod_rat (moment.main_part_);
180 /* if undefined, starting is ok */
183 /* but ending is not */
190 Auto_beam_engraver::consider_begin (Moment test_mom)
192 bool off = to_boolean (get_property ("noAutoBeaming"));
193 if (!stem_l_arr_p_ && ! off)
195 bool b = test_moment (START, test_mom);
202 Auto_beam_engraver::consider_end (Moment test_mom)
206 /* Allow already started autobeam to end:
207 don't check for noAutoBeaming */
208 bool b = test_moment (STOP, test_mom);
215 Auto_beam_engraver::create_beam_p ()
217 if (to_boolean (get_property ("skipTypesetting")))
222 Spanner* beam_p = new Spanner (get_property ("Beam"));
223 for (int i = 0; i < stem_l_arr_p_->size (); i++)
226 watch out for stem tremolos and abbreviation beams
228 if (Stem::beam_l ((*stem_l_arr_p_)[i]))
230 scm_gc_unprotect_object (beam_p->self_scm ());
233 Beam::add_stem (beam_p, (*stem_l_arr_p_)[i]);
236 announce_grob (beam_p, 0);
242 Auto_beam_engraver::begin_beam ()
244 assert (!stem_l_arr_p_);
245 stem_l_arr_p_ = new Link_array<Item>;
246 assert (!grouping_p_);
247 grouping_p_ = new Beaming_info_list;
248 beam_start_moment_ = now_mom ();
249 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
256 Auto_beam_engraver::junk_beam ()
258 assert (stem_l_arr_p_);
260 delete stem_l_arr_p_;
265 shortest_mom_ = Moment (Rational (1, 8));
269 Auto_beam_engraver::end_beam ()
271 if (stem_l_arr_p_->size () < 2)
278 finished_beam_p_ = create_beam_p ();
279 if (finished_beam_p_)
280 finished_grouping_p_ = grouping_p_;
281 delete stem_l_arr_p_;
286 shortest_mom_ = Moment (Rational (1, 8));
290 Auto_beam_engraver::typeset_beam ()
292 if (finished_beam_p_)
294 finished_grouping_p_->beamify(*unsmob_moment (get_property ("beatLength")),
295 (bool)gh_scm2bool(get_property("subdivideBeams")));
296 Beam::set_beaming (finished_beam_p_, finished_grouping_p_);
297 typeset_grob (finished_beam_p_);
298 finished_beam_p_ = 0;
300 delete finished_grouping_p_;
301 finished_grouping_p_= 0;
306 Auto_beam_engraver::start_translation_timestep ()
310 don't beam over skips
314 Moment now = now_mom ();
315 if (extend_mom_ < now)
323 Auto_beam_engraver::stop_translation_timestep ()
329 Auto_beam_engraver::finalize ()
331 /* finished beams may be typeset */
333 /* but unfinished may need another announce/acknowledge pass */
340 Auto_beam_engraver::acknowledge_grob (Grob_info info)
344 if (Beam::has_interface (info.grob_l_))
348 else if (Bar::has_interface (info.grob_l_))
352 else if (Rest::has_interface (info.grob_l_))
358 if (Stem::has_interface (info.grob_l_))
360 Item* stem_l = dynamic_cast<Item *> (info.grob_l_);
362 Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
365 programming_error ("Stem must have rhythmic structure");
370 Don't (start) auto-beam over empty stems; skips or rests
372 if (!Stem::heads_i (stem_l))
379 if (Stem::beam_l (stem_l))
386 int durlog = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->duration_log ();
399 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
403 Moment dur = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->length_mom ();
406 This comment has been here since long:
408 if shortest duration would change
409 consider ending and beginning beam first.
411 but the code didn't match: */
414 consider_begin (dur);
416 if (dur < shortest_mom_)
419 /* I very much suspect that we wanted: */
421 consider_end (shortest_mom_);
422 if (dur < shortest_mom_)
425 consider_end (shortest_mom_);
427 consider_begin (shortest_mom_);
433 Moment now = now_mom ();
435 grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
437 stem_l_arr_p_->push (stem_l);
439 extend_mom_ = (extend_mom_ >? now) + rhythmic_req->length_mom ();
444 Auto_beam_engraver::create_grobs ()
448 consider_end (shortest_mom_);
449 consider_begin (shortest_mom_);
451 else if (count_i_ > 1)
455 Moment now = now_mom ();
456 if ((extend_mom_ < now)
457 || ((extend_mom_ == now) && (last_add_mom_ != now)))
461 else if (!stem_l_arr_p_->size ())
471 auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
472 count_i_ = count_i_ + 1;
474 ENTER_DESCRIPTION(Auto_beam_engraver,
475 /* descr */ "Generate beams based on measure characteristics and observed
476 Stems. Uses beatLength, measureLength and measurePosition to decide
477 when to start and stop a beam. Overriding beaming is done through
478 @ref{Stem_engraver} properties stemLeftBeamCount and
482 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
483 /* reads */ "noAutoBeaming autoBeamSettings",