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_
;
63 Projected ending of the beam we're working on.
66 Moment beam_start_moment_
;
67 Moment beam_start_location_
;
69 // We act as if beam were created, and start a grouping anyway.
70 Beaming_info_list
*grouping_p_
;
71 Beaming_info_list
*finished_grouping_p_
;
74 ADD_THIS_TRANSLATOR (Auto_beam_engraver
);
76 Auto_beam_engraver::Auto_beam_engraver ()
80 shortest_mom_
= Moment (1, 8);
82 finished_grouping_p_
= 0;
87 Determine end moment for auto beaming (or begin moment, but mostly
88 0==anywhere) In order of increasing priority:
90 i. begin anywhere, end at every beat
92 iii. end <type> <num> <den>
100 [to be defined in config file]
101 i. easy catch-all rule
102 ii. exceptions for time signature
103 iii. exceptions for time signature, for specific duration type
107 v. override for specific duration type
111 Auto_beam_engraver::test_moment (Direction dir
, Moment test_mom
)
113 SCM wild
= gh_list (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED
);
116 function
= gh_list (ly_symbol2scm ("begin"), SCM_UNDEFINED
);
118 function
= gh_list (ly_symbol2scm ("end"), SCM_UNDEFINED
);
120 Moment one_beat
= *unsmob_moment (get_property ("beatLength"));
121 int num
= *unsmob_moment (get_property ("measureLength")) / one_beat
;
122 int den
= one_beat
.den_i ();
123 SCM time
= gh_list (gh_int2scm (num
), gh_int2scm (den
), SCM_UNDEFINED
);
125 SCM type
= gh_list (gh_int2scm (test_mom
.num_i ()),
126 gh_int2scm (test_mom
.den_i ()), SCM_UNDEFINED
);
128 SCM settings
= get_property ("autoBeamSettings");
132 /* begin beam at any position
133 (and fallback for end) */
136 /* end beam at end of beat */
139 SCM
beat (get_property ("beatLength"));
141 if (unsmob_moment (beat
))
142 moment
= *unsmob_moment (beat
);
145 /* second guess: property generic time exception */
146 SCM m
= gh_assoc (gh_append3 (function
, wild
, time
), settings
);
148 if (m
!= SCM_BOOL_F
&& unsmob_moment (gh_cdr (m
)))
149 moment
= * unsmob_moment (gh_cdr (m
));
151 /* third guess: property time exception, specific for duration type */
152 m
= gh_assoc (gh_append3 (function
, type
, time
), settings
);
153 if (m
!= SCM_BOOL_F
&& unsmob_moment (gh_cdr (m
)))
154 moment
= * unsmob_moment (gh_cdr (m
));
156 /* fourth guess [user override]: property plain generic */
157 m
= gh_assoc (gh_append3 (function
, wild
, wild
), settings
);
158 if (m
!= SCM_BOOL_F
&& unsmob_moment (gh_cdr (m
)))
159 moment
= * unsmob_moment (gh_cdr (m
));
161 /* fifth guess [user override]: property plain, specific for duration type */
162 m
= gh_assoc (gh_append3 (function
, type
, wild
), settings
);
163 if (m
!= SCM_BOOL_F
&& unsmob_moment (gh_cdr (m
)))
164 moment
= * unsmob_moment (gh_cdr (m
));
169 /* Ugh? measurePosition can be negative, when \partial
170 We may have to fix this elsewhere (timing translator)
171 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
173 Moment pos
= * unsmob_moment (get_property ("measurePosition"));
174 if (pos
< Moment (0))
176 Moment length
= * unsmob_moment (get_property ("measureLength"));
179 r
= pos
.mod_rat (moment
);
184 /* if undefined, starting is ok */
187 /* but ending is not */
194 Auto_beam_engraver::consider_begin (Moment test_mom
)
196 bool off
= to_boolean (get_property ("noAutoBeaming"));
197 if (!stem_l_arr_p_
&& ! off
)
199 bool b
= test_moment (START
, test_mom
);
206 Auto_beam_engraver::consider_end (Moment test_mom
)
210 /* Allow already started autobeam to end:
211 don't check for noAutoBeaming */
212 bool b
= test_moment (STOP
, test_mom
);
219 Auto_beam_engraver::create_beam_p ()
221 if (to_boolean (get_property ("skipTypesetting")))
226 Spanner
* beam_p
= new Spanner (get_property ("Beam"));
227 for (int i
= 0; i
< stem_l_arr_p_
->size (); i
++)
230 watch out for stem tremolos and abbreviation beams
232 if (Stem::beam_l ((*stem_l_arr_p_
)[i
]))
234 scm_unprotect_object (beam_p
->self_scm ());
237 Beam::add_stem (beam_p
, (*stem_l_arr_p_
)[i
]);
240 announce_grob (beam_p
, 0);
246 Auto_beam_engraver::begin_beam ()
248 assert (!stem_l_arr_p_
);
249 stem_l_arr_p_
= new Link_array
<Item
>;
250 assert (!grouping_p_
);
251 grouping_p_
= new Beaming_info_list
;
252 beam_start_moment_
= now_mom ();
253 beam_start_location_
= *unsmob_moment (get_property ("measurePosition"));
258 Auto_beam_engraver::junk_beam ()
260 assert (stem_l_arr_p_
);
262 delete stem_l_arr_p_
;
267 shortest_mom_
= Moment (1, 8);
271 Auto_beam_engraver::end_beam ()
273 if (stem_l_arr_p_
->size () < 2)
280 finished_beam_p_
= create_beam_p ();
281 if (finished_beam_p_
)
282 finished_grouping_p_
= grouping_p_
;
283 delete stem_l_arr_p_
;
288 shortest_mom_
= Moment (1, 8);
292 Auto_beam_engraver::typeset_beam ()
294 if (finished_beam_p_
)
296 finished_grouping_p_
->beamify ();
297 Beam::set_beaming (finished_beam_p_
, finished_grouping_p_
);
298 typeset_grob (finished_beam_p_
);
299 finished_beam_p_
= 0;
301 delete finished_grouping_p_
;
302 finished_grouping_p_
= 0;
307 Auto_beam_engraver::start_translation_timestep ()
311 don't beam over skips
315 Moment now
= now_mom ();
316 if (extend_mom_
< now
)
324 Auto_beam_engraver::stop_translation_timestep ()
331 Auto_beam_engraver::finalize ()
333 /* finished beams may be typeset */
335 /* but unfinished may need another announce/acknowledge pass */
341 Auto_beam_engraver::same_grace_state_b (Grob
* e
)
343 bool gr
= e
->get_grob_property ("grace") == SCM_BOOL_T
;
344 SCM wg
=get_property ("weAreGraceContext");
345 return (to_boolean (wg
)) == gr
;
349 Auto_beam_engraver::acknowledge_grob (Grob_info info
)
351 if (!same_grace_state_b (info
.elem_l_
))
356 if (Beam::has_interface (info
.elem_l_
))
360 else if (Bar::has_interface (info
.elem_l_
))
364 else if (Rest::has_interface (info
.elem_l_
))
370 if (Stem::has_interface (info
.elem_l_
))
372 Item
* stem_l
= dynamic_cast<Item
*> (info
.elem_l_
);
374 Rhythmic_req
*rhythmic_req
= dynamic_cast <Rhythmic_req
*> (info
.req_l_
);
377 programming_error ("Stem must have rhythmic structure");
382 Don't (start) auto-beam over empty stems; skips or rests
384 if (!Stem::heads_i (stem_l
))
391 if (Stem::beam_l (stem_l
))
398 int durlog
= unsmob_duration (rhythmic_req
->get_mus_property ("duration"))->duration_log ();
407 Moment dur
= unsmob_duration (rhythmic_req
->get_mus_property ("duration"))->length_mom ();
410 This comment has been here since long:
412 if shortest duration would change
413 consider ending and beginning beam first.
415 but the code didn't match: */
418 consider_begin (dur
);
420 if (dur
< shortest_mom_
)
423 /* I very much suspect that we wanted: */
425 consider_end (shortest_mom_
);
426 if (dur
< shortest_mom_
)
429 consider_end (shortest_mom_
);
431 consider_begin (shortest_mom_
);
437 Moment now
= now_mom ();
439 grouping_p_
->add_stem (now
- beam_start_moment_
+ beam_start_location_
,
441 stem_l_arr_p_
->push (stem_l
);
443 extend_mom_
= extend_mom_
>? now
+ rhythmic_req
->length_mom ();
448 Auto_beam_engraver::create_grobs ()
452 consider_end (shortest_mom_
);
453 consider_begin (shortest_mom_
);
455 else if (count_i_
> 1)
459 Moment now
= now_mom ();
460 if ((extend_mom_
< now
)
461 || ((extend_mom_
== now
) && (last_add_mom_
!= now
)))
465 else if (!stem_l_arr_p_
->size ())
475 auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
476 count_i_
= count_i_
+ 1;