]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
dbff4e95a2f8803bfe87fa2b7afd18fe307422e9
[lilypond.git] / lily / auto-beam-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2011 Jan Nieuwenhuizen <janneke@gnu.org>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "bar-line.hh"
21 #include "beaming-pattern.hh"
22 #include "beam.hh"
23 #include "context.hh"
24 #include "duration.hh"
25 #include "engraver.hh"
26 #include "item.hh"
27 #include "rest.hh"
28 #include "spanner.hh"
29 #include "stream-event.hh"
30 #include "stem.hh"
31 #include "warn.hh"
32
33 #include "translator.icc"
34
35 class Auto_beam_engraver : public Engraver
36 {
37   TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
38
39 protected:
40   void stop_translation_timestep ();
41   void process_music ();
42   virtual void finalize ();
43   virtual void derived_mark () const;
44
45   DECLARE_ACKNOWLEDGER (rest);
46   DECLARE_ACKNOWLEDGER (beam);
47   DECLARE_ACKNOWLEDGER (bar_line);
48   DECLARE_ACKNOWLEDGER (breathing_sign);
49   DECLARE_ACKNOWLEDGER (stem);
50   DECLARE_TRANSLATOR_LISTENER (beam_forbid);
51
52   void process_acknowledged ();
53
54 private:
55   bool test_moment (Direction, Moment, Moment);
56   void consider_begin (Moment, Moment);
57   void consider_end (Moment, Moment);
58   Spanner *create_beam ();
59   void begin_beam ();
60   void end_beam ();
61   void junk_beam ();
62   bool is_same_grace_state (Grob *e);
63   void recheck_beam ();
64   void typeset_beam ();
65   vector<Item *> *remove_end_stems (vsize);
66
67   Stream_event *forbid_;
68   /*
69     shortest_mom_ is the shortest note in the beam.
70   */
71   Moment shortest_mom_;
72   Spanner *finished_beam_;
73   vector<Item *> *stems_;
74
75   int process_acknowledged_count_;
76   Moment last_add_mom_;
77   /*
78     Projected ending of the  beam we're working on.
79   */
80   Moment extend_mom_;
81   Moment beam_start_moment_;
82   Moment beam_start_location_;
83
84   // We act as if beam were created, and start a grouping anyway.
85   Beaming_pattern *grouping_;
86   SCM beam_settings_;
87
88   Beaming_pattern *finished_grouping_;
89
90
91   Beaming_options beaming_options_;
92   Beaming_options finished_beaming_options_;
93
94
95   void check_bar_property ();
96 };
97
98 void
99 Auto_beam_engraver::derived_mark () const
100 {
101   scm_gc_mark (beam_settings_);
102 }
103
104 void
105 Auto_beam_engraver::check_bar_property ()
106 {
107   /* Duplicated from process_music (), since
108      Repeat_acknowledge_engraver::process_music () may also set whichBar.  */
109
110   Moment now = now_mom ();
111   if (scm_is_string (get_property ("whichBar"))
112       && beam_start_moment_ < now)
113     {
114       consider_end (measure_position (context ()), shortest_mom_);
115       junk_beam ();
116     }
117 }
118
119 void
120 Auto_beam_engraver::process_music ()
121 {
122   Moment now = now_mom ();
123   /*
124     don't beam over skips
125   */
126   if (stems_)
127     {
128       if (extend_mom_ < now)
129         end_beam ();
130     }
131
132   if (scm_is_string (get_property ("whichBar")))
133     {
134       consider_end (measure_position (context ()), shortest_mom_);
135       junk_beam ();
136     }
137
138   if (forbid_)
139     {
140       consider_end (measure_position (context ()), shortest_mom_);
141       junk_beam ();
142     }
143 }
144
145 Auto_beam_engraver::Auto_beam_engraver ()
146 {
147   forbid_ = 0;
148   process_acknowledged_count_ = 0;
149   stems_ = 0;
150   shortest_mom_ = Moment (Rational (1, 4));
151   finished_beam_ = 0;
152   finished_grouping_ = 0;
153   grouping_ = 0;
154   beam_settings_ = SCM_EOL;
155 }
156
157 IMPLEMENT_TRANSLATOR_LISTENER (Auto_beam_engraver, beam_forbid);
158 void
159 Auto_beam_engraver::listen_beam_forbid (Stream_event *ev)
160 {
161   ASSIGN_EVENT_ONCE (forbid_, ev);
162 }
163
164 bool
165 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom, Moment dur)
166 {
167   return scm_call_4 (get_property ("autoBeamCheck"),
168                      context ()->self_scm (),
169                      scm_from_int (dir),
170                      test_mom.smobbed_copy (),
171                      dur.smobbed_copy ())
172     != SCM_BOOL_F;
173 }
174
175 void
176 Auto_beam_engraver::consider_begin (Moment test_mom, Moment dur)
177 {
178   bool on = to_boolean (get_property ("autoBeaming"));
179   if (!stems_ && on
180       && !forbid_)
181     {
182       bool b = test_moment (START, test_mom, dur);
183       if (b)
184         begin_beam ();
185     }
186 }
187
188 void
189 Auto_beam_engraver::consider_end (Moment test_mom, Moment dur)
190 {
191   if (stems_)
192     {
193       /* Allow already started autobeam to end:
194          don't check for autoBeaming */
195       bool b = test_moment (STOP, test_mom, dur);
196       if (b)
197         end_beam ();
198     }
199 }
200
201 Spanner *
202 Auto_beam_engraver::create_beam ()
203 {
204   if (to_boolean (get_property ("skipTypesetting")))
205     return 0;
206
207   for (vsize i = 0; i < stems_->size (); i++)
208     if (Stem::get_beam ((*stems_)[i]))
209       return 0;
210
211   /*
212     Can't use make_spanner () because we have to use
213     beam_settings_.
214   */
215   Spanner *beam = new Spanner (beam_settings_);
216
217   for (vsize i = 0; i < stems_->size (); i++)
218     Beam::add_stem (beam, (*stems_)[i]);
219
220   announce_grob (beam, (*stems_)[0]->self_scm ());
221
222   return beam;
223 }
224
225 void
226 Auto_beam_engraver::begin_beam ()
227 {
228   if (stems_ || grouping_)
229     {
230       programming_error ("already have autobeam");
231       return;
232     }
233
234   stems_ = new vector<Item *>;
235   grouping_ = new Beaming_pattern ();
236   beaming_options_.from_context (context ());
237   beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
238
239   beam_start_moment_ = now_mom ();
240   beam_start_location_
241     = robust_scm2moment (get_property ("measurePosition"), Moment (0));
242 }
243
244 void
245 Auto_beam_engraver::junk_beam ()
246 {
247   if (!stems_)
248     return;
249
250   delete stems_;
251   stems_ = 0;
252   delete grouping_;
253   grouping_ = 0;
254   beam_settings_ = SCM_EOL;
255
256   shortest_mom_ = Moment (Rational (1, 4));
257 }
258
259 void
260 Auto_beam_engraver::end_beam ()
261 {
262   if (stems_->size () < 2)
263     junk_beam ();
264   else
265     {
266       finished_beam_ = create_beam ();
267
268       if (finished_beam_)
269         {
270           announce_end_grob (finished_beam_, SCM_EOL);
271           finished_grouping_ = grouping_;
272           finished_beaming_options_ = beaming_options_;
273         }
274       delete stems_;
275       stems_ = 0;
276       grouping_ = 0;
277       beam_settings_ = SCM_EOL;
278     }
279
280   shortest_mom_ = Moment (Rational (1, 4));
281 }
282
283 void
284 Auto_beam_engraver::typeset_beam ()
285 {
286   if (finished_beam_)
287     {
288       if (!finished_beam_->get_bound (RIGHT))
289         finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
290
291       finished_grouping_->beamify (finished_beaming_options_);
292       Beam::set_beaming (finished_beam_, finished_grouping_);
293       finished_beam_ = 0;
294
295       delete finished_grouping_;
296       finished_grouping_ = 0;
297     }
298 }
299
300 void
301 Auto_beam_engraver::stop_translation_timestep ()
302 {
303   typeset_beam ();
304   process_acknowledged_count_ = 0;
305   forbid_ = 0;
306 }
307
308 void
309 Auto_beam_engraver::finalize ()
310 {
311   /* finished beams may be typeset */
312   typeset_beam ();
313   /* but unfinished may need another announce/acknowledge pass */
314   if (stems_)
315     junk_beam ();
316 }
317
318
319 void
320 Auto_beam_engraver::acknowledge_beam (Grob_info /* info */)
321 {
322   check_bar_property ();
323   if (stems_)
324     end_beam ();
325 }
326
327 void
328 Auto_beam_engraver::acknowledge_bar_line (Grob_info /* info */)
329 {
330   check_bar_property ();
331   if (stems_)
332     end_beam ();
333 }
334
335 void
336 Auto_beam_engraver::acknowledge_breathing_sign (Grob_info /* info */)
337 {
338   check_bar_property ();
339   if (stems_)
340     end_beam ();
341 }
342
343 void
344 Auto_beam_engraver::acknowledge_rest (Grob_info /* info */)
345 {
346   check_bar_property ();
347   if (stems_)
348     end_beam ();
349 }
350
351 void
352 Auto_beam_engraver::acknowledge_stem (Grob_info info)
353 {
354   check_bar_property ();
355   Item *stem = dynamic_cast<Item *> (info.grob ());
356   Stream_event *ev = info.ultimate_event_cause ();
357   if (!ev->in_event_class ("rhythmic-event"))
358     {
359       programming_error ("stem must have rhythmic structure");
360       return;
361     }
362
363   /*
364     Don't (start) auto-beam over empty stems; skips or rests
365   */
366   if (!Stem::head_count (stem))
367     {
368       if (stems_)
369         end_beam ();
370       return;
371     }
372
373   if (Stem::get_beam (stem))
374     {
375       if (stems_)
376         junk_beam ();
377       return;
378     }
379
380   int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
381
382   if (durlog <= 2)
383     {
384       if (stems_)
385         end_beam ();
386       return;
387     }
388
389   /*
390     ignore grace notes.
391   */
392   Moment now = now_mom ();
393   if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
394     return;
395
396   Moment dur = unsmob_duration (ev->get_property ("duration"))->get_length ();
397   Moment measure_now = measure_position (context ());
398   bool recheck_needed = false;
399
400   if (dur < shortest_mom_)
401     {
402     /* new shortest moment, so store it and set recheck_needed */
403     shortest_mom_ = dur;
404     recheck_needed = true;
405     }
406
407   /* end should be based on shortest_mom_, begin should be
408      based on current duration  */
409   consider_end (measure_now, shortest_mom_);
410   consider_begin (measure_now, dur);
411
412   if (!stems_)
413     return;
414
415   grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
416                        durlog - 2,
417                        Stem::is_invisible (stem));
418   stems_->push_back (stem);
419   last_add_mom_ = now;
420   extend_mom_ = max (extend_mom_, now) + get_event_length (ev, now);
421   if (recheck_needed)
422     recheck_beam ();
423 }
424
425 void
426 Auto_beam_engraver::recheck_beam ()
427 {
428   /*
429     Recheck the beam after the shortest duration has changed
430     If shorter duration has created a new break, typeset the
431     first part of the beam and reset the current beam to just
432     the last part of the beam
433   */
434   Beaming_pattern *new_grouping_ = 0;
435   vector<Item *> *new_stems_ = 0;
436   Moment temporary_shortest_mom;
437   SCM temporary_beam_settings;
438
439   bool found_end;
440
441
442   for (vsize i = 0; i < stems_->size () - 1;)
443     {
444       found_end = test_moment (STOP,
445                                grouping_->end_moment (i),
446                                shortest_mom_);
447       if (!found_end)
448         i++;
449       else
450         {
451           /*
452             Save the current beam settings and shortest_mom_
453             Necessary because end_beam destroys them
454           */
455           temporary_shortest_mom = shortest_mom_;
456           temporary_beam_settings = beam_settings_;
457
458           /* Eliminate (and save) the items no longer part of the first beam */
459
460           new_grouping_ = grouping_->split_pattern (i);
461           new_stems_ = remove_end_stems (i);
462
463           end_beam ();
464           typeset_beam ();
465
466           /* now recreate the unbeamed data structures */
467           stems_ = new_stems_;
468           grouping_ = new_grouping_;
469           shortest_mom_ = temporary_shortest_mom;
470           beam_settings_ = temporary_beam_settings;
471
472           i = 0;
473         }
474
475     }
476
477 }
478
479 /*
480   Remove all stems with an index greater than split_index
481   from stems_, and return a vector containing all of the
482   removed stems
483 */
484 vector <Item *> *
485 Auto_beam_engraver::remove_end_stems (vsize split_index)
486 {
487   vector <Item *> *removed_stems = 0;
488   removed_stems = new vector <Item *>;
489
490   for (vsize j = split_index + 1; j < stems_->size (); j++)
491     removed_stems->push_back ((*stems_).at (j));
492   for (vsize j = split_index + 1; j < stems_->size ();)
493     stems_->pop_back ();
494   return removed_stems;
495 }
496
497 void
498 Auto_beam_engraver::process_acknowledged ()
499 {
500   Moment now = now_mom();
501   if (extend_mom_ > now)
502     return;
503
504   if (!process_acknowledged_count_)
505     {
506       Moment measure_now = measure_position (context ());
507       consider_end (measure_now, shortest_mom_);
508     }
509   else if (process_acknowledged_count_ > 1)
510     {
511       if (stems_)
512         {
513           if ((extend_mom_ < now)
514               || ((extend_mom_ == now) && (last_add_mom_ != now)))
515             end_beam ();
516           else if (!stems_->size ())
517             junk_beam ();
518         }
519     }
520
521   process_acknowledged_count_++;
522 }
523
524 ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
525 ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
526 ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
527 ADD_ACKNOWLEDGER (Auto_beam_engraver, breathing_sign);
528 ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
529 ADD_TRANSLATOR (Auto_beam_engraver,
530                 /* doc */
531                 "Generate beams based on measure characteristics and observed"
532                 " Stems.  Uses @code{baseMoment}, @code{beatStructure},"
533                 " @code{beamExceptions}, @code{measureLength}, and"
534                 " @code{measurePosition} to decide when to start and stop a"
535                 " beam.  Overriding beaming is done through"
536                 " @ref{Stem_engraver} properties @code{stemLeftBeamCount} and"
537                 " @code{stemRightBeamCount}.",
538
539                 /* create */
540                 "Beam ",
541
542                 /* read */
543                 "autoBeaming "
544                 "baseMoment "
545                 "beamExceptions "
546                 "beatStructure "
547                 "subdivideBeams ",
548
549                 /* write */
550                 ""
551                 );