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
30 Auto_beam_engraver ();
31 VIRTUAL_COPY_CONS (Translator);
34 virtual void stop_translation_timestep ();
35 virtual void start_translation_timestep ();
36 virtual void finalize ();
37 virtual void acknowledge_grob (Grob_info);
38 virtual void create_grobs ();
41 bool test_moment (Direction, Moment);
42 void consider_begin (Moment);
43 void consider_end (Moment);
44 Spanner* create_beam_p ();
48 bool same_grace_state_b (Grob* e);
52 shortest_mom is the shortest note in the beam.
55 Spanner *finished_beam_p_;
56 Link_array<Item>* stem_l_arr_p_;
62 Projected ending of the beam we're working on.
65 Moment beam_start_moment_;
66 Moment beam_start_location_;
68 // We act as if beam were created, and start a grouping anyway.
69 Beaming_info_list*grouping_p_;
70 Beaming_info_list*finished_grouping_p_;
73 ADD_THIS_TRANSLATOR (Auto_beam_engraver);
75 Auto_beam_engraver::Auto_beam_engraver ()
79 shortest_mom_ = Moment (1, 8);
81 finished_grouping_p_ = 0;
86 Determine end moment for auto beaming (or begin moment, but mostly
87 0==anywhere) In order of increasing priority:
89 i. begin anywhere, end at every beat
91 iii. end <type> <num> <den>
99 [to be defined in config file]
100 i. easy catch-all rule
101 ii. exceptions for time signature
102 iii. exceptions for time signature, for specific duration type
106 v. override for specific duration type
110 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
112 SCM wild = gh_list (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
115 function = gh_list (ly_symbol2scm ("begin"), SCM_UNDEFINED);
117 function = gh_list (ly_symbol2scm ("end"), SCM_UNDEFINED);
119 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
120 int num = *unsmob_moment (get_property ("measureLength")) / one_beat;
121 int den = one_beat.den ();
122 SCM time = gh_list (gh_int2scm (num), gh_int2scm (den), SCM_UNDEFINED);
124 SCM type = gh_list (gh_int2scm (test_mom.num ()),
125 gh_int2scm (test_mom.den ()), SCM_UNDEFINED);
127 SCM settings = get_property ("autoBeamSettings");
131 /* begin beam at any position
132 (and fallback for end) */
135 /* end beam at end of beat */
138 SCM beat (get_property ("beatLength"));
140 if (unsmob_moment (beat))
141 moment = *unsmob_moment (beat);
144 /* second guess: property generic time exception */
145 SCM m = gh_assoc (gh_append3 (function, wild, time), settings);
147 if (m != SCM_BOOL_F && unsmob_moment (gh_cdr (m)))
148 moment = * unsmob_moment (gh_cdr (m));
150 /* third guess: property time exception, specific for duration type */
151 m = gh_assoc (gh_append3 (function, type, time), settings);
152 if (m != SCM_BOOL_F && unsmob_moment (gh_cdr (m)))
153 moment = * unsmob_moment (gh_cdr (m));
155 /* fourth guess [user override]: property plain generic */
156 m = gh_assoc (gh_append3 (function, wild, wild), settings);
157 if (m != SCM_BOOL_F && unsmob_moment (gh_cdr (m)))
158 moment = * unsmob_moment (gh_cdr (m));
160 /* fifth guess [user override]: property plain, specific for duration type */
161 m = gh_assoc (gh_append3 (function, type, wild), settings);
162 if (m != SCM_BOOL_F && unsmob_moment (gh_cdr (m)))
163 moment = * unsmob_moment (gh_cdr (m));
168 /* Ugh? measurePosition can be negative, when \partial
169 We may have to fix this elsewhere (timing translator)
170 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
172 Moment pos = * unsmob_moment (get_property ("measurePosition"));
173 if (pos < Moment (0))
175 Moment length = * unsmob_moment (get_property ("measureLength"));
178 r = pos.main_part_.mod_rat (moment.main_part_);
183 /* if undefined, starting is ok */
186 /* but ending is not */
193 Auto_beam_engraver::consider_begin (Moment test_mom)
195 bool off = to_boolean (get_property ("noAutoBeaming"));
196 if (!stem_l_arr_p_ && ! off)
198 bool b = test_moment (START, test_mom);
205 Auto_beam_engraver::consider_end (Moment test_mom)
209 /* Allow already started autobeam to end:
210 don't check for noAutoBeaming */
211 bool b = test_moment (STOP, test_mom);
218 Auto_beam_engraver::create_beam_p ()
220 if (to_boolean (get_property ("skipTypesetting")))
225 Spanner* beam_p = new Spanner (get_property ("Beam"));
226 for (int i = 0; i < stem_l_arr_p_->size (); i++)
229 watch out for stem tremolos and abbreviation beams
231 if (Stem::beam_l ((*stem_l_arr_p_)[i]))
233 scm_gc_unprotect_object (beam_p->self_scm ());
236 Beam::add_stem (beam_p, (*stem_l_arr_p_)[i]);
239 announce_grob (beam_p, 0);
245 Auto_beam_engraver::begin_beam ()
247 assert (!stem_l_arr_p_);
248 stem_l_arr_p_ = new Link_array<Item>;
249 assert (!grouping_p_);
250 grouping_p_ = new Beaming_info_list;
251 beam_start_moment_ = now_mom ();
252 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
259 Auto_beam_engraver::junk_beam ()
261 assert (stem_l_arr_p_);
263 delete stem_l_arr_p_;
268 shortest_mom_ = Moment (1, 8);
272 Auto_beam_engraver::end_beam ()
274 if (stem_l_arr_p_->size () < 2)
281 finished_beam_p_ = create_beam_p ();
282 if (finished_beam_p_)
283 finished_grouping_p_ = grouping_p_;
284 delete stem_l_arr_p_;
289 shortest_mom_ = Moment (1, 8);
293 Auto_beam_engraver::typeset_beam ()
295 if (finished_beam_p_)
297 finished_grouping_p_->beamify ();
298 Beam::set_beaming (finished_beam_p_, finished_grouping_p_);
299 typeset_grob (finished_beam_p_);
300 finished_beam_p_ = 0;
302 delete finished_grouping_p_;
303 finished_grouping_p_= 0;
308 Auto_beam_engraver::start_translation_timestep ()
312 don't beam over skips
316 Moment now = now_mom ();
317 if (extend_mom_ < now)
325 Auto_beam_engraver::stop_translation_timestep ()
332 Auto_beam_engraver::finalize ()
334 /* finished beams may be typeset */
336 /* but unfinished may need another announce/acknowledge pass */
343 Auto_beam_engraver::acknowledge_grob (Grob_info info)
347 if (Beam::has_interface (info.elem_l_))
351 else if (Bar::has_interface (info.elem_l_))
355 else if (Rest::has_interface (info.elem_l_))
361 if (Stem::has_interface (info.elem_l_))
363 Item* stem_l = dynamic_cast<Item *> (info.elem_l_);
365 Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
368 programming_error ("Stem must have rhythmic structure");
373 Don't (start) auto-beam over empty stems; skips or rests
375 if (!Stem::heads_i (stem_l))
382 if (Stem::beam_l (stem_l))
389 int durlog = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->duration_log ();
402 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
406 Moment dur = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->length_mom ();
409 This comment has been here since long:
411 if shortest duration would change
412 consider ending and beginning beam first.
414 but the code didn't match: */
417 consider_begin (dur);
419 if (dur < shortest_mom_)
422 /* I very much suspect that we wanted: */
424 consider_end (shortest_mom_);
425 if (dur < shortest_mom_)
428 consider_end (shortest_mom_);
430 consider_begin (shortest_mom_);
436 Moment now = now_mom ();
438 grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
440 stem_l_arr_p_->push (stem_l);
442 extend_mom_ = (extend_mom_ >? now) + rhythmic_req->length_mom ();
447 Auto_beam_engraver::create_grobs ()
451 consider_end (shortest_mom_);
452 consider_begin (shortest_mom_);
454 else if (count_i_ > 1)
458 Moment now = now_mom ();
459 if ((extend_mom_ < now)
460 || ((extend_mom_ == now) && (last_add_mom_ != now)))
464 else if (!stem_l_arr_p_->size ())
474 auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
475 count_i_ = count_i_ + 1;