]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
* lily/tuplet-engraver.cc (start_translation_timestep): only read
[lilypond.git] / lily / auto-beam-engraver.cc
1 /*
2   auto-beam-engraver.cc -- implement Auto_beam_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "engraver.hh"
10 #include "beaming.hh"
11 #include "beam.hh"
12 #include "stem.hh"
13 #include "warn.hh"
14 #include "bar-line.hh"
15 #include "rest.hh"
16 #include "item.hh"
17 #include "spanner.hh"
18 #include "context.hh"
19 #include "duration.hh"
20
21 #include "translator.icc"
22
23
24 class Auto_beam_engraver : public Engraver
25 {
26   TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
27
28 protected:
29   PRECOMPUTED_VIRTUAL void stop_translation_timestep ();
30   PRECOMPUTED_VIRTUAL void start_translation_timestep ();
31   PRECOMPUTED_VIRTUAL void process_music ();
32   virtual bool try_music (Music *);
33   virtual void finalize ();
34
35   DECLARE_ACKNOWLEDGER(rest);
36   DECLARE_ACKNOWLEDGER(beam);
37   DECLARE_ACKNOWLEDGER(bar_line);
38   DECLARE_ACKNOWLEDGER(stem);
39   
40   PRECOMPUTED_VIRTUAL void process_acknowledged ();
41
42 private:
43   bool test_moment (Direction, Moment);
44   void consider_begin (Moment);
45   void consider_end (Moment);
46   Spanner *create_beam ();
47   void begin_beam ();
48   void end_beam ();
49   void junk_beam ();
50   bool is_same_grace_state (Grob *e);
51   void typeset_beam ();
52
53   Music *forbid_;
54   /*
55     shortest_mom is the shortest note in the beam.
56   */
57   Moment shortest_mom_;
58   Spanner *finished_beam_;
59   Link_array<Item> *stems_;
60
61   int process_acknowledged_count_;
62   Moment last_add_mom_;
63   /*
64     Projected ending of the  beam we're working on.
65   */
66   Moment extend_mom_;
67   Moment beam_start_moment_;
68   Moment beam_start_location_;
69
70   bool subdivide_beams_;
71   Moment beat_length_;
72
73   // We act as if beam were created, and start a grouping anyway.
74   Beaming_info_list *grouping_;
75   SCM beam_settings_;           // ugh. should protect ? 
76
77   Beaming_info_list *finished_grouping_;
78
79   void check_bar_property ();
80 };
81
82
83 void
84 Auto_beam_engraver::check_bar_property ()
85 {
86   /* Duplicated from process_music (), since
87      Repeat_acknowledge_engraver::process_music () may also set whichBar.  */
88
89   Moment now = now_mom ();
90   if (scm_is_string (get_property ("whichBar"))
91       && beam_start_moment_ < now)
92     {
93       consider_end (shortest_mom_);
94       junk_beam ();
95     }
96 }
97
98 void
99 Auto_beam_engraver::process_music ()
100 {
101   if (scm_is_string (get_property ("whichBar")))
102     {
103       consider_end (shortest_mom_);
104       junk_beam ();
105     }
106
107   if (forbid_)
108     {
109       consider_end (shortest_mom_);
110       junk_beam ();
111     }
112 }
113
114 Auto_beam_engraver::Auto_beam_engraver ()
115 {
116   forbid_ = 0;
117   process_acknowledged_count_ = 0;
118   stems_ = 0;
119   shortest_mom_ = Moment (Rational (1, 8));
120   finished_beam_ = 0;
121   finished_grouping_ = 0;
122   grouping_ = 0;
123   beam_settings_ = SCM_EOL;
124 }
125
126 bool
127 Auto_beam_engraver::try_music (Music *m)
128 {
129   if (m->is_mus_type ("beam-forbid-event"))
130     {
131       forbid_ = m;
132       return true;
133     }
134
135   return false;
136 }
137
138 bool
139 Auto_beam_engraver::test_moment (Direction dir, Moment test)
140 {
141   return scm_call_3 (get_property ("autoBeamCheck"),
142                      context ()->self_scm (),
143                      scm_from_int (dir),
144                      test.smobbed_copy ())
145     != SCM_BOOL_F;
146 }
147     
148 void
149 Auto_beam_engraver::consider_begin (Moment test_mom)
150 {
151   bool on = to_boolean (get_property ("autoBeaming"));
152   if (!stems_ && on
153       && !forbid_)
154     {
155       bool b = test_moment (START, test_mom);
156       if (b)
157         begin_beam ();
158     }
159 }
160
161 void
162 Auto_beam_engraver::consider_end (Moment test_mom)
163 {
164   if (stems_)
165     {
166       /* Allow already started autobeam to end:
167          don't check for autoBeaming */
168       bool b = test_moment (STOP, test_mom);
169       if (b)
170         end_beam ();
171     }
172 }
173
174 Spanner *
175 Auto_beam_engraver::create_beam ()
176 {
177   if (to_boolean (get_property ("skipTypesetting")))
178     return 0;
179
180   Spanner *beam = new Spanner (beam_settings_, context ()->get_grob_key ("Beam"));
181   for (int i = 0; i < stems_->size (); i++)
182     {
183       /*
184         watch out for stem tremolos and abbreviation beams
185       */
186       if (Stem::get_beam ((*stems_)[i]))
187         {
188           scm_gc_unprotect_object (beam->self_scm ());
189           return 0;
190         }
191       Beam::add_stem (beam, (*stems_)[i]);
192     }
193
194   announce_grob (beam, (*stems_)[0]->self_scm ());
195
196   return beam;
197 }
198
199 void
200 Auto_beam_engraver::begin_beam ()
201 {
202   if (stems_ || grouping_)
203     {
204       programming_error ("already have autobeam");
205       return;
206     }
207
208   stems_ = new Link_array<Item>;
209   grouping_ = new Beaming_info_list;
210   beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
211
212   beam_start_moment_ = now_mom ();
213   beam_start_location_
214     = robust_scm2moment (get_property ("measurePosition"), Moment (0));
215   subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
216   beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
217 }
218
219 void
220 Auto_beam_engraver::junk_beam ()
221 {
222   if (!stems_)
223     return;
224
225   delete stems_;
226   stems_ = 0;
227   delete grouping_;
228   grouping_ = 0;
229   beam_settings_ = SCM_EOL;
230
231   shortest_mom_ = Moment (Rational (1, 8));
232 }
233
234 void
235 Auto_beam_engraver::end_beam ()
236 {
237   if (stems_->size () < 2)
238     {
239       junk_beam ();
240     }
241   else
242     {
243       finished_beam_ = create_beam ();
244       if (finished_beam_)
245         finished_grouping_ = grouping_;
246       delete stems_;
247       stems_ = 0;
248       grouping_ = 0;
249       beam_settings_ = SCM_EOL;
250     }
251
252   shortest_mom_ = Moment (Rational (1, 8));
253 }
254
255 void
256 Auto_beam_engraver::typeset_beam ()
257 {
258   if (finished_beam_)
259     {
260       finished_grouping_->beamify (beat_length_, subdivide_beams_);
261       Beam::set_beaming (finished_beam_, finished_grouping_);
262       finished_beam_ = 0;
263
264       delete finished_grouping_;
265       finished_grouping_ = 0;
266     }
267 }
268
269 void
270 Auto_beam_engraver::start_translation_timestep ()
271 {
272   process_acknowledged_count_ = 0;
273   /*
274     don't beam over skips
275   */
276   if (stems_)
277     {
278       Moment now = now_mom ();
279       if (extend_mom_ < now)
280         {
281           end_beam ();
282         }
283     }
284   forbid_ = 0;
285 }
286
287 void
288 Auto_beam_engraver::stop_translation_timestep ()
289 {
290   typeset_beam ();
291 }
292
293 void
294 Auto_beam_engraver::finalize ()
295 {
296   /* finished beams may be typeset */
297   typeset_beam ();
298   /* but unfinished may need another announce/acknowledge pass */
299   if (stems_)
300     junk_beam ();
301 }
302
303
304
305
306 void
307 Auto_beam_engraver::acknowledge_beam (Grob_info info)
308 {
309   check_bar_property ();
310   if (stems_)
311     {
312       end_beam ();
313     }
314 }
315  
316 void
317 Auto_beam_engraver::acknowledge_bar_line (Grob_info info)
318 {
319   check_bar_property ();
320   if (stems_)
321     end_beam ();
322 }
323
324 void
325 Auto_beam_engraver::acknowledge_rest (Grob_info info)
326 {
327   check_bar_property ();
328   if (stems_)
329     end_beam ();
330 }
331
332 void
333 Auto_beam_engraver::acknowledge_stem (Grob_info info)
334 {
335   check_bar_property ();
336   Item *stem = dynamic_cast<Item *> (info.grob ());
337   Music *m = info.music_cause ();
338   if (!m->is_mus_type ("rhythmic-event"))
339     {
340       programming_error ("stem must have rhythmic structure");
341       return;
342     }
343
344   /*
345     Don't (start) auto-beam over empty stems; skips or rests
346   */
347   if (!Stem::head_count (stem))
348     {
349       if (stems_)
350         end_beam ();
351       return;
352     }
353
354   if (Stem::get_beam (stem))
355     {
356       if (stems_)
357         junk_beam ();
358       return;
359     }
360
361   int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
362
363   if (durlog <= 2)
364     {
365       if (stems_)
366         end_beam ();
367       return;
368     }
369
370   /*
371     ignore grace notes.
372   */
373   Moment now = now_mom ();
374   if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
375     return;
376
377   Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
378
379   consider_end (dur);
380   consider_begin (dur);
381
382   if (dur < shortest_mom_)
383     shortest_mom_ = dur;
384
385   if (!stems_)
386     return;
387
388   grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
389                        durlog - 2);
390   stems_->push (stem);
391   last_add_mom_ = now;
392   extend_mom_ = max (extend_mom_, now) + m->get_length ();
393 }
394
395 void
396 Auto_beam_engraver::process_acknowledged ()
397 {
398   if (extend_mom_ > now_mom ())
399     return ; 
400
401   if (!process_acknowledged_count_)
402     {
403       consider_end (shortest_mom_);
404       consider_begin (shortest_mom_);
405     }
406   else if (process_acknowledged_count_ > 1)
407     {
408       if (stems_)
409         {
410           Moment now = now_mom ();
411           if ((extend_mom_ < now)
412               || ((extend_mom_ == now) && (last_add_mom_ != now)))
413             {
414               end_beam ();
415             }
416           else if (!stems_->size ())
417             {
418               junk_beam ();
419             }
420         }
421     }
422
423   process_acknowledged_count_++;
424 }
425
426 ADD_ACKNOWLEDGER(Auto_beam_engraver,stem);
427 ADD_ACKNOWLEDGER(Auto_beam_engraver,bar_line);
428 ADD_ACKNOWLEDGER(Auto_beam_engraver,beam);
429 ADD_ACKNOWLEDGER(Auto_beam_engraver,rest);
430 ADD_TRANSLATOR (Auto_beam_engraver,
431                 /* descr */ "Generate beams based on measure characteristics and observed "
432                 "Stems.  Uses beatLength, measureLength and measurePosition to decide "
433                 "when to start and stop a beam.  Overriding beaming is done through "
434                 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
435                 "@code{stemRightBeamCount}. ",
436                 /* creats*/ "Beam",
437                 /* accepts */ "beam-forbid-event",
438                 /* acks  */ "",
439                 /* reads */ "autoBeaming autoBeamSettings beatLength subdivideBeams",
440                 /* write */ "");