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