(Which properties to
[lilypond.git] / lily / auto-beam-engraver.cc
bloba4c431ba5520f9baef6de30db2abbbbed0c7d7c6
1 /*
2 auto-beam-engraver.cc -- implement Auto_beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2004 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
10 #include "beaming.hh"
11 #include "event.hh"
12 #include "beam.hh"
13 #include "stem.hh"
14 #include "warn.hh"
15 #include "engraver-group-engraver.hh"
16 #include "bar-line.hh"
17 #include "rest.hh"
18 #include "engraver.hh"
19 #include "item.hh"
20 #include "spanner.hh"
21 #include "duration.hh"
22 #include "context.hh"
25 TODO: documentme.
27 class Auto_beam_engraver : public Engraver
29 TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
30 protected:
31 virtual void stop_translation_timestep ();
32 virtual void start_translation_timestep ();
33 virtual void process_music ();
34 virtual bool try_music (Music*);
35 virtual void finalize ();
36 virtual void acknowledge_grob (Grob_info);
37 virtual void process_acknowledged_grobs ();
39 private:
40 bool test_moment (Direction, Moment);
41 void consider_begin (Moment);
42 void consider_end (Moment);
43 Spanner* create_beam ();
44 void begin_beam ();
45 void end_beam ();
46 void junk_beam ();
47 bool is_same_grace_state (Grob* e);
48 void typeset_beam ();
50 Music *forbid_;
52 shortest_mom is the shortest note in the beam.
54 Moment shortest_mom_;
55 Spanner *finished_beam_;
56 Link_array<Item>* stems_;
59 int count_;
60 Moment last_add_mom_;
62 Projected ending of the beam we're working on.
64 Moment extend_mom_;
65 Moment beam_start_moment_;
66 Moment beam_start_location_;
68 bool subdivide_beams_;
69 Moment beat_length_;
71 // We act as if beam were created, and start a grouping anyway.
72 Beaming_info_list*grouping_;
73 SCM beam_settings_ ; // ugh. should protect ?
75 Beaming_info_list*finished_grouping_;
78 void
79 Auto_beam_engraver::process_music ()
81 if (gh_string_p (get_property ("whichBar")))
83 consider_end (shortest_mom_);
84 junk_beam ();
87 if (forbid_)
89 consider_end (shortest_mom_);
90 junk_beam ();
95 Auto_beam_engraver::Auto_beam_engraver ()
97 forbid_ = 0;
98 count_ = 0;
99 stems_ = 0;
100 shortest_mom_ = Moment (Rational (1, 8));
101 finished_beam_ = 0;
102 finished_grouping_ = 0;
103 grouping_ = 0;
104 beam_settings_ = SCM_EOL;
108 bool
109 Auto_beam_engraver::try_music (Music*m)
111 if (m->is_mus_type ("beam-forbid-event"))
113 forbid_ = m;
114 return true;
117 return false;
121 Determine end moment for auto beaming (or begin moment, but mostly
122 0==anywhere) In order of increasing priority:
124 i. begin anywhere, end at every beat
125 ii. end * <num> <den>
126 iii. end <type> <num> <den>
128 iv. end * * *
129 v. end <type> * *
132 Rationale:
134 [to be defined in config file]
135 i. easy catch-all rule
136 ii. exceptions for time signature
137 iii. exceptions for time signature, for specific duration type
139 [user override]
140 iv. generic override
141 v. override for specific duration type
144 bool
145 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
147 Moment now = now_mom ();
148 if (dir == START
149 && now.grace_part_)
151 return false;
154 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
155 SCM function;
156 if (dir == START)
157 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
158 else
159 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
161 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
162 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
163 int den = one_beat.den ();
164 SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
166 SCM type = scm_list_n (scm_int2num (test_mom.num ()),
167 scm_int2num (test_mom.den ()), SCM_UNDEFINED);
170 UGH UGH.
171 settings aren't grob-properties.
173 SCM settings = get_property ("autoBeamSettings");
175 /* first guess */
177 /* begin beam at any position
178 (and fallback for end) */
179 Moment moment (0);
181 /* end beam at end of beat */
182 if (dir == STOP)
184 SCM beat (get_property ("beatLength"));
186 if (unsmob_moment (beat))
187 moment = *unsmob_moment (beat);
190 /* second guess: property generic time exception */
191 SCM m = scm_assoc (gh_append3 (function, wild, time), settings);
193 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
194 moment = * unsmob_moment (ly_cdr (m));
196 /* third guess: property time exception, specific for duration type */
197 m = scm_assoc (gh_append3 (function, type, time), settings);
198 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
199 moment = * unsmob_moment (ly_cdr (m));
201 /* fourth guess [user override]: property plain generic */
202 m = scm_assoc (gh_append3 (function, wild, wild), settings);
203 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
204 moment = * unsmob_moment (ly_cdr (m));
206 /* fifth guess [user override]: property plain, specific for duration type */
207 m = scm_assoc (gh_append3 (function, type, wild), settings);
208 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
209 moment = * unsmob_moment (ly_cdr (m));
211 Rational r;
212 if (moment.to_bool ())
214 /* Ugh? measurePosition can be negative, when \partial
215 We may have to fix this elsewhere (timing translator)
216 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
218 Moment pos = * unsmob_moment (get_property ("measurePosition"));
219 if (pos < Moment (0))
221 Moment length = * unsmob_moment (get_property ("measureLength"));
222 pos = length - pos;
224 r = pos.main_part_.mod_rat (moment.main_part_);
226 else
228 if (dir == START)
229 /* if undefined, starting is ok */
230 r = 0;
231 else
232 /* but ending is not */
233 r = 1;
236 return !r;
239 void
240 Auto_beam_engraver::consider_begin (Moment test_mom)
242 bool on = to_boolean (get_property ("autoBeaming"));
243 if (!stems_ && on
244 && !forbid_)
246 bool b = test_moment (START, test_mom);
247 if (b)
248 begin_beam ();
252 void
253 Auto_beam_engraver::consider_end (Moment test_mom)
255 if (stems_)
257 /* Allow already started autobeam to end:
258 don't check for autoBeaming */
259 bool b = test_moment (STOP, test_mom);
260 if (b)
261 end_beam ();
265 Spanner*
266 Auto_beam_engraver::create_beam ()
268 if (to_boolean (get_property ("skipTypesetting")))
270 return 0;
273 Spanner* beam = new Spanner (beam_settings_);
274 for (int i = 0; i < stems_->size (); i++)
277 watch out for stem tremolos and abbreviation beams
279 if (Stem::get_beam ((*stems_)[i]))
281 scm_gc_unprotect_object (beam->self_scm ());
282 return 0;
284 Beam::add_stem (beam, (*stems_)[i]);
287 announce_grob (beam, (*stems_)[0]->self_scm ());
289 return beam;
292 void
293 Auto_beam_engraver::begin_beam ()
295 if (stems_ || grouping_ )
297 programming_error ("already have autobeam");
298 return;
301 stems_ = new Link_array<Item>;
302 grouping_ = new Beaming_info_list;
303 beam_settings_ = updated_grob_properties (daddy_context_, ly_symbol2scm ("Beam"));
305 beam_start_moment_ = now_mom ();
306 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
307 subdivide_beams_ = gh_scm2bool (get_property ("subdivideBeams"));
308 beat_length_ = *unsmob_moment (get_property ("beatLength"));
311 void
312 Auto_beam_engraver::junk_beam ()
314 if (!stems_)
315 return ;
317 delete stems_;
318 stems_ = 0;
319 delete grouping_;
320 grouping_ = 0;
321 beam_settings_ = SCM_EOL;
323 shortest_mom_ = Moment (Rational (1, 8));
326 void
327 Auto_beam_engraver::end_beam ()
329 if (stems_->size () < 2)
331 junk_beam ();
333 else
335 finished_beam_ = create_beam ();
336 if (finished_beam_)
337 finished_grouping_ = grouping_;
338 delete stems_;
339 stems_ = 0;
340 grouping_ = 0;
341 beam_settings_ = SCM_EOL;
344 shortest_mom_ = Moment (Rational (1, 8));
347 void
348 Auto_beam_engraver::typeset_beam ()
350 if (finished_beam_)
352 finished_grouping_->beamify (beat_length_, subdivide_beams_);
353 Beam::set_beaming (finished_beam_, finished_grouping_);
354 typeset_grob (finished_beam_);
355 finished_beam_ = 0;
357 delete finished_grouping_;
358 finished_grouping_= 0;
362 void
363 Auto_beam_engraver::start_translation_timestep ()
365 count_ = 0;
367 don't beam over skips
369 if (stems_)
371 Moment now = now_mom ();
372 if (extend_mom_ < now)
374 end_beam ();
377 forbid_ = 0;
380 void
381 Auto_beam_engraver::stop_translation_timestep ()
383 typeset_beam ();
386 void
387 Auto_beam_engraver::finalize ()
389 /* finished beams may be typeset */
390 typeset_beam ();
391 /* but unfinished may need another announce/acknowledge pass */
392 if (stems_)
393 junk_beam ();
397 void
398 Auto_beam_engraver::acknowledge_grob (Grob_info info)
400 if (stems_)
402 if (Beam::has_interface (info.grob_))
404 end_beam ();
406 else if (Bar_line::has_interface (info.grob_))
408 end_beam ();
410 else if (Rest::has_interface (info.grob_))
412 end_beam ();
416 if (Stem::has_interface (info.grob_))
418 Item* stem = dynamic_cast<Item *> (info.grob_);
419 Music* m = info.music_cause ();
420 if (!m->is_mus_type ("rhythmic-event"))
422 programming_error ("Stem must have rhythmic structure");
423 return;
427 Don't (start) auto-beam over empty stems; skips or rests
429 if (!Stem::head_count (stem))
431 if (stems_)
432 end_beam ();
433 return;
436 if (Stem::get_beam (stem))
438 if (stems_)
439 junk_beam ();
440 return ;
443 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
445 if (durlog <= 2)
447 if (stems_)
448 end_beam ();
449 return;
454 ignore grace notes.
456 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
457 return ;
460 Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
461 /* FIXME:
463 This comment has been here since long:
465 if shortest duration would change
466 consider ending and beginning beam first.
468 but the code didn't match: */
469 #if 1
470 consider_end (dur);
471 consider_begin (dur);
473 if (dur < shortest_mom_)
474 shortest_mom_ = dur;
475 #else
476 /* I very much suspect that we wanted: */
478 consider_end (shortest_mom_);
479 if (dur < shortest_mom_)
481 shortest_mom_ = dur;
482 consider_end (shortest_mom_);
484 consider_begin (shortest_mom_);
485 #endif
487 if (!stems_)
488 return;
490 Moment now = now_mom ();
492 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
493 durlog - 2);
494 stems_->push (stem);
495 last_add_mom_ = now;
496 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
500 void
501 Auto_beam_engraver::process_acknowledged_grobs ()
503 if (!count_)
505 consider_end (shortest_mom_);
506 consider_begin (shortest_mom_);
508 else if (count_ > 1)
510 if (stems_)
512 Moment now = now_mom ();
513 if ((extend_mom_ < now)
514 || ((extend_mom_ == now) && (last_add_mom_ != now)))
516 end_beam ();
518 else if (!stems_->size ())
520 junk_beam ();
525 count_ ++;
528 ENTER_DESCRIPTION (Auto_beam_engraver,
529 /* descr */ "Generate beams based on measure characteristics and observed "
530 "Stems. Uses beatLength, measureLength and measurePosition to decide "
531 "when to start and stop a beam. Overriding beaming is done through "
532 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
533 "@code{stemRightBeamCount}. "
535 /* creats*/ "Beam",
536 /* accepts */ "beam-forbid-event",
537 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
538 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",
539 /* write */ "");