*** empty log message ***
[lilypond.git] / lily / auto-beam-engraver.cc
blob6dc65ddfb33d6b83a93e3a6695a500d8ff6a922e
1 /*
2 auto-beam-engraver.cc -- implement Auto_beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2003 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"
24 TODO: figure what to do in grace?
26 TODO: documentme.
28 class Auto_beam_engraver : public Engraver
30 TRANSLATOR_DECLARATIONS(Auto_beam_engraver);
31 protected:
32 virtual void stop_translation_timestep ();
33 virtual void start_translation_timestep ();
34 virtual void finalize ();
35 virtual void acknowledge_grob (Grob_info);
36 virtual void process_acknowledged_grobs ();
38 private:
39 bool test_moment (Direction, Moment);
40 void consider_begin (Moment);
41 void consider_end (Moment);
42 Spanner* create_beam ();
43 void begin_beam ();
44 void end_beam ();
45 void junk_beam ();
46 bool same_grace_state_b (Grob* e);
47 void typeset_beam ();
50 shortest_mom is the shortest note in the beam.
52 Moment shortest_mom_;
53 Spanner *finished_beam_;
54 Link_array<Item>* stems_;
57 int count_;
58 Moment last_add_mom_;
60 Projected ending of the beam we're working on.
62 Moment extend_mom_;
63 Moment beam_start_moment_;
64 Moment beam_start_location_;
66 bool subdivide_beams_;
67 Moment beat_length_;
69 // We act as if beam were created, and start a grouping anyway.
70 Beaming_info_list*grouping_;
71 SCM beam_settings_ ; // ugh. should protect ?
73 Beaming_info_list*finished_grouping_;
78 Auto_beam_engraver::Auto_beam_engraver ()
80 count_ = 0;
81 stems_ = 0;
82 shortest_mom_ = Moment (Rational (1, 8));
83 finished_beam_ = 0;
84 finished_grouping_ = 0;
85 grouping_ = 0;
86 beam_settings_ = SCM_EOL;
90 Determine end moment for auto beaming (or begin moment, but mostly
91 0==anywhere) In order of increasing priority:
93 i. begin anywhere, end at every beat
94 ii. end * <num> <den>
95 iii. end <type> <num> <den>
97 iv. end * * *
98 v. end <type> * *
101 Rationale:
103 [to be defined in config file]
104 i. easy catch-all rule
105 ii. exceptions for time signature
106 iii. exceptions for time signature, for specific duration type
108 [user override]
109 iv. generic override
110 v. override for specific duration type
113 bool
114 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
116 Moment now = now_mom();
117 if (dir == START
118 && now.grace_part_)
120 return false;
123 SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
124 SCM function;
125 if (dir == START)
126 function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
127 else
128 function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
130 Moment one_beat = *unsmob_moment (get_property ("beatLength"));
131 int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
132 int den = one_beat.den ();
133 SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
135 SCM type = scm_list_n (scm_int2num (test_mom.num ()),
136 scm_int2num (test_mom.den ()), SCM_UNDEFINED);
138 SCM settings = get_property ("autoBeamSettings");
140 /* first guess */
142 /* begin beam at any position
143 (and fallback for end) */
144 Moment moment (0);
146 /* end beam at end of beat */
147 if (dir == STOP)
149 SCM beat (get_property ("beatLength"));
151 if (unsmob_moment (beat))
152 moment = *unsmob_moment (beat);
155 /* second guess: property generic time exception */
156 SCM m = gh_assoc (gh_append3 (function, wild, time), settings);
158 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
159 moment = * unsmob_moment (ly_cdr (m));
161 /* third guess: property time exception, specific for duration type */
162 m = gh_assoc (gh_append3 (function, type, time), settings);
163 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
164 moment = * unsmob_moment (ly_cdr (m));
166 /* fourth guess [user override]: property plain generic */
167 m = gh_assoc (gh_append3 (function, wild, wild), settings);
168 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
169 moment = * unsmob_moment (ly_cdr (m));
171 /* fifth guess [user override]: property plain, specific for duration type */
172 m = gh_assoc (gh_append3 (function, type, wild), settings);
173 if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
174 moment = * unsmob_moment (ly_cdr (m));
176 Rational r;
177 if (moment.to_bool ())
179 /* Ugh? measurePosition can be negative, when \partial
180 We may have to fix this elsewhere (timing translator)
181 r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
183 Moment pos = * unsmob_moment (get_property ("measurePosition"));
184 if (pos < Moment (0))
186 Moment length = * unsmob_moment (get_property ("measureLength"));
187 pos = length - pos;
189 r = pos.main_part_.mod_rat (moment.main_part_);
191 else
193 if (dir == START)
194 /* if undefined, starting is ok */
195 r = 0;
196 else
197 /* but ending is not */
198 r = 1;
201 return !r;
204 void
205 Auto_beam_engraver::consider_begin (Moment test_mom)
207 bool on = to_boolean (get_property ("autoBeaming"));
208 if (!stems_ && on)
210 bool b = test_moment (START, test_mom);
211 if (b)
212 begin_beam ();
216 void
217 Auto_beam_engraver::consider_end (Moment test_mom)
219 if (stems_)
221 /* Allow already started autobeam to end:
222 don't check for autoBeaming */
223 bool b = test_moment (STOP, test_mom);
224 if (b)
225 end_beam ();
229 Spanner*
230 Auto_beam_engraver::create_beam ()
232 if (to_boolean (get_property ("skipTypesetting")))
234 return 0;
237 Spanner* beam = new Spanner (beam_settings_);
238 for (int i = 0; i < stems_->size (); i++)
241 watch out for stem tremolos and abbreviation beams
243 if (Stem::get_beam ((*stems_)[i]))
245 scm_gc_unprotect_object (beam->self_scm ());
246 return 0;
248 Beam::add_stem (beam, (*stems_)[i]);
251 announce_grob(beam, SCM_EOL);
253 return beam;
256 void
257 Auto_beam_engraver::begin_beam ()
259 assert (!stems_);
260 stems_ = new Link_array<Item>;
261 assert (!grouping_);
262 grouping_ = new Beaming_info_list;
263 beam_settings_ = get_property ("Beam");
265 beam_start_moment_ = now_mom ();
266 beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
267 subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
268 beat_length_ = *unsmob_moment (get_property ("beatLength"));
272 void
273 Auto_beam_engraver::junk_beam ()
275 assert (stems_);
277 delete stems_;
278 stems_ = 0;
279 delete grouping_;
280 grouping_ = 0;
281 beam_settings_ = SCM_EOL;
283 shortest_mom_ = Moment (Rational (1, 8));
286 void
287 Auto_beam_engraver::end_beam ()
289 if (stems_->size () < 2)
291 junk_beam ();
293 else
296 finished_beam_ = create_beam ();
297 if (finished_beam_)
298 finished_grouping_ = grouping_;
299 delete stems_;
300 stems_ = 0;
301 grouping_ = 0;
302 beam_settings_ = SCM_EOL;
305 shortest_mom_ = Moment (Rational (1, 8));
308 void
309 Auto_beam_engraver::typeset_beam ()
311 if (finished_beam_)
313 finished_grouping_->beamify(beat_length_, subdivide_beams_);
314 Beam::set_beaming (finished_beam_, finished_grouping_);
315 typeset_grob (finished_beam_);
316 finished_beam_ = 0;
318 delete finished_grouping_;
319 finished_grouping_= 0;
323 void
324 Auto_beam_engraver::start_translation_timestep ()
326 count_ = 0;
328 don't beam over skips
330 if (stems_)
332 Moment now = now_mom ();
333 if (extend_mom_ < now)
335 end_beam ();
340 void
341 Auto_beam_engraver::stop_translation_timestep ()
343 typeset_beam ();
346 void
347 Auto_beam_engraver::finalize ()
349 /* finished beams may be typeset */
350 typeset_beam ();
351 /* but unfinished may need another announce/acknowledge pass */
352 if (stems_)
353 junk_beam ();
357 void
358 Auto_beam_engraver::acknowledge_grob (Grob_info info)
360 if (stems_)
362 if (Beam::has_interface (info.grob_))
364 end_beam ();
366 else if (Bar_line::has_interface (info.grob_))
368 end_beam ();
370 else if (Rest::has_interface (info.grob_))
372 end_beam ();
376 if (Stem::has_interface (info.grob_))
378 Item* stem = dynamic_cast<Item *> (info.grob_);
379 Music* m = info.music_cause ();
380 if (!m->is_mus_type ("rhythmic-event"))
382 programming_error ("Stem must have rhythmic structure");
383 return;
387 Don't (start) auto-beam over empty stems; skips or rests
389 if (!Stem::head_count (stem))
391 if (stems_)
392 end_beam ();
393 return;
396 if (Stem::get_beam (stem))
398 if (stems_)
399 junk_beam ();
400 return ;
403 int durlog = unsmob_duration (m->get_mus_property ("duration"))->duration_log ();
405 if (durlog <= 2)
407 if (stems_)
408 end_beam ();
409 return;
414 ignore grace notes.
416 if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
417 return ;
420 Moment dur = unsmob_duration (m->get_mus_property ("duration"))->get_length ();
421 /* FIXME:
423 This comment has been here since long:
425 if shortest duration would change
426 consider ending and beginning beam first.
428 but the code didn't match: */
429 #if 1
430 consider_end (dur);
431 consider_begin (dur);
433 if (dur < shortest_mom_)
434 shortest_mom_ = dur;
435 #else
436 /* I very much suspect that we wanted: */
438 consider_end (shortest_mom_);
439 if (dur < shortest_mom_)
441 shortest_mom_ = dur;
442 consider_end (shortest_mom_);
444 consider_begin (shortest_mom_);
445 #endif
447 if (!stems_)
448 return;
450 Moment now = now_mom ();
452 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
453 durlog - 2);
454 stems_->push (stem);
455 last_add_mom_ = now;
456 extend_mom_ = (extend_mom_ >? now) + m->get_length ();
460 void
461 Auto_beam_engraver::process_acknowledged_grobs ()
463 if (!count_)
465 consider_end (shortest_mom_);
466 consider_begin (shortest_mom_);
468 else if (count_ > 1)
470 if (stems_)
472 Moment now = now_mom ();
473 if ((extend_mom_ < now)
474 || ((extend_mom_ == now) && (last_add_mom_ != now)))
476 end_beam ();
478 else if (!stems_->size ())
480 junk_beam ();
486 count_++ ->
488 auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
489 count_ = count_ + 1;
492 ENTER_DESCRIPTION (Auto_beam_engraver,
493 /* descr */ "Generate beams based on measure characteristics and observed "
494 "Stems. Uses beatLength, measureLength and measurePosition to decide "
495 "when to start and stop a beam. Overriding beaming is done through "
496 "@ref{Stem_engraver} properties stemLeftBeamCount and "
497 "stemRightBeamCount. "
499 /* creats*/ "Beam",
500 /* accepts */ "",
501 /* acks */ "stem-interface rest-interface beam-interface bar-line-interface",
502 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",
503 /* write */ "");