]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
* input/test/ac-extra-voice.ly (accompany): remove file.
[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--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9
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
23 /*
24   TODO: figure what to do in grace?
25
26   TODO: documentme.
27  */
28 class Auto_beam_engraver : public Engraver
29 {
30   TRANSLATOR_DECLARATIONS(Auto_beam_engraver);
31 protected:
32   virtual void stop_translation_timestep ();
33   virtual void start_translation_timestep ();
34   virtual void process_music ();
35   virtual void finalize ();
36   virtual void acknowledge_grob (Grob_info);
37   virtual void process_acknowledged_grobs ();
38
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 same_grace_state_b (Grob* e);
48   void typeset_beam ();
49
50   /*
51     shortest_mom is the shortest note in the beam.
52    */
53   Moment shortest_mom_;
54   Spanner *finished_beam_;
55   Link_array<Item>* stems_;
56
57
58   int count_;
59   Moment last_add_mom_;
60   /*
61     Projected ending of the  beam we're working on.
62    */
63   Moment extend_mom_;
64   Moment beam_start_moment_;
65   Moment beam_start_location_;
66
67   bool subdivide_beams_;
68   Moment beat_length_;
69   
70   // We act as if beam were created, and start a grouping anyway.
71   Beaming_info_list*grouping_;
72   SCM beam_settings_ ;          // ugh. should protect ? 
73   
74   Beaming_info_list*finished_grouping_;
75 };
76
77 void
78 Auto_beam_engraver::process_music ()
79 {
80   if (gh_string_p (get_property ("whichBar")))
81     {
82       consider_end (shortest_mom_);
83       junk_beam ();
84     }
85 }
86
87
88 Auto_beam_engraver::Auto_beam_engraver ()
89 {
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 /*
100   Determine end moment for auto beaming (or begin moment, but mostly
101   0==anywhere) In order of increasing priority:
102   
103   i.   begin anywhere, end at every beat
104   ii.  end   *    <num> <den>
105   iii. end <type> <num> <den>
106   
107   iv.  end   *      *     *
108   v.   end <type>   *     *
109   
110   
111   Rationale:
112   
113   [to be defined in config file]
114   i.   easy catch-all rule
115   ii.  exceptions for time signature
116   iii. exceptions for time signature, for specific duration type
117   
118   [user override]
119   iv.  generic override
120   v.   override for specific duration type
121   
122 */
123 bool
124 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
125 {
126   Moment now = now_mom();
127   if (dir == START
128       && now.grace_part_)
129     {
130       return false;
131     }
132   
133   SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
134   SCM function;
135   if (dir == START)
136     function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
137   else
138     function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
139
140   Moment one_beat = *unsmob_moment (get_property ("beatLength"));
141   int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
142   int den = one_beat.den ();
143   SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
144
145   SCM type = scm_list_n (scm_int2num (test_mom.num ()),
146                       scm_int2num (test_mom.den ()), SCM_UNDEFINED);
147
148   /*
149     UGH UGH.
150     settings aren't grob-properties.
151    */
152   SCM settings = get_property ("autoBeamSettings");
153   
154   /* first guess */
155   
156   /* begin beam at any position
157  (and fallback for end) */
158   Moment moment (0);
159   
160   /* end beam at end of beat */
161   if (dir == STOP)
162     {
163       SCM beat (get_property ("beatLength"));
164       
165       if (unsmob_moment (beat))
166         moment = *unsmob_moment (beat);
167     }
168
169   /* second guess: property generic time exception */
170   SCM m = scm_assoc (gh_append3 (function, wild, time), settings);
171   
172   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
173     moment = * unsmob_moment (ly_cdr (m));
174
175   /* third guess: property time exception, specific for duration type */
176   m = scm_assoc (gh_append3 (function, type, time), settings);
177   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
178     moment = * unsmob_moment (ly_cdr (m));
179
180   /* fourth guess [user override]: property plain generic */
181   m = scm_assoc (gh_append3 (function, wild, wild), settings);
182   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
183     moment = * unsmob_moment (ly_cdr (m));
184
185   /* fifth guess [user override]: property plain, specific for duration type */
186   m = scm_assoc (gh_append3 (function, type, wild), settings);
187   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
188     moment = * unsmob_moment (ly_cdr (m));
189   
190   Rational r;
191   if (moment.to_bool ())
192     {
193       /* Ugh? measurePosition can be negative, when \partial
194          We may have to fix this elsewhere (timing translator)
195         r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
196       */
197       Moment pos = * unsmob_moment (get_property ("measurePosition"));
198       if (pos < Moment (0))
199         {
200           Moment length = * unsmob_moment (get_property ("measureLength"));
201           pos = length - pos;
202         }
203       r = pos.main_part_.mod_rat (moment.main_part_);
204     }
205   else
206     {
207       if (dir == START)
208         /* if undefined, starting is ok */
209         r = 0;
210       else
211         /* but ending is not */
212         r = 1;
213     }
214
215   return !r;
216 }
217
218 void
219 Auto_beam_engraver::consider_begin (Moment test_mom)
220 {
221   bool on = to_boolean (get_property ("autoBeaming"));
222   if (!stems_ && on)
223     {
224       bool b = test_moment (START, test_mom);
225       if (b)
226         begin_beam ();
227     }
228 }
229
230 void
231 Auto_beam_engraver::consider_end (Moment test_mom)
232 {
233   if (stems_)
234     {
235       /* Allow already started autobeam to end:
236          don't check for autoBeaming */
237       bool b = test_moment (STOP, test_mom);
238       if (b)
239         end_beam ();
240     }
241 }
242
243 Spanner*
244 Auto_beam_engraver::create_beam ()
245 {
246   if (to_boolean (get_property ("skipTypesetting")))
247     {
248      return 0;
249     }
250   
251   Spanner* beam = new Spanner (beam_settings_);
252   for (int i = 0; i < stems_->size (); i++)
253     {
254       /*
255         watch out for stem tremolos and abbreviation beams
256        */
257       if (Stem::get_beam ((*stems_)[i]))
258         {
259           scm_gc_unprotect_object (beam->self_scm ());
260           return 0;
261         }
262       Beam::add_stem (beam, (*stems_)[i]);
263     }
264   
265   announce_grob(beam, SCM_EOL);
266
267   return beam;
268 }
269
270 void
271 Auto_beam_engraver::begin_beam ()
272 {
273   if (stems_ || grouping_ )
274     {
275       programming_error ("already have autobeam");
276       return; 
277     }
278   
279   stems_ = new Link_array<Item>;
280   grouping_ = new Beaming_info_list;
281   beam_settings_ = updated_grob_properties (daddy_trans_, ly_symbol2scm ("Beam"));
282   
283   beam_start_moment_ = now_mom ();
284   beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
285   subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
286   beat_length_ = *unsmob_moment (get_property ("beatLength"));
287 }
288
289 void
290 Auto_beam_engraver::junk_beam () 
291 {
292   if (!stems_)
293     return ;
294   
295   delete stems_;
296   stems_ = 0;
297   delete grouping_;
298   grouping_ = 0;
299   beam_settings_ = SCM_EOL;
300   
301   shortest_mom_ = Moment (Rational (1, 8));
302 }
303
304 void
305 Auto_beam_engraver::end_beam ()
306 {
307   if (stems_->size () < 2)
308     {
309       junk_beam ();
310     }
311   else
312     {
313       finished_beam_ = create_beam ();
314       if (finished_beam_)
315         finished_grouping_ = grouping_;
316       delete stems_;
317       stems_ = 0;
318       grouping_ = 0;
319       beam_settings_ = SCM_EOL;
320     }
321
322   shortest_mom_ = Moment (Rational (1, 8));
323 }
324
325 void
326 Auto_beam_engraver::typeset_beam ()
327 {
328   if (finished_beam_)
329     {
330       finished_grouping_->beamify(beat_length_, subdivide_beams_);
331       Beam::set_beaming (finished_beam_, finished_grouping_);
332       typeset_grob (finished_beam_);
333       finished_beam_ = 0;
334     
335       delete finished_grouping_;
336       finished_grouping_= 0;
337     }
338 }
339
340 void
341 Auto_beam_engraver::start_translation_timestep ()
342 {
343   count_ = 0;
344   /*
345     don't beam over skips
346    */
347   if (stems_)
348     {
349       Moment now = now_mom ();
350       if (extend_mom_ < now)
351         {
352           end_beam ();
353         }
354     }
355 }
356
357 void
358 Auto_beam_engraver::stop_translation_timestep ()
359 {
360   typeset_beam ();
361 }
362
363 void
364 Auto_beam_engraver::finalize ()
365 {
366   /* finished beams may be typeset */
367   typeset_beam ();
368   /* but unfinished may need another announce/acknowledge pass */
369   if (stems_)
370     junk_beam ();
371 }
372
373
374 void
375 Auto_beam_engraver::acknowledge_grob (Grob_info info)
376 {
377   if (stems_)
378     {
379       if (Beam::has_interface (info.grob_))
380         {
381           end_beam ();
382         }
383       else if (Bar_line::has_interface (info.grob_))
384         {
385           end_beam ();
386         }
387       else if (Rest::has_interface (info.grob_))
388         {
389           end_beam ();
390         }
391     }
392   
393   if (Stem::has_interface (info.grob_))
394     {
395       Item* stem = dynamic_cast<Item *> (info.grob_);
396       Music* m = info.music_cause ();
397       if (!m->is_mus_type ("rhythmic-event"))
398         {
399           programming_error ("Stem must have rhythmic structure");
400           return;
401         }
402       
403       /*
404         Don't (start) auto-beam over empty stems; skips or rests
405         */
406       if (!Stem::head_count (stem))
407         {
408           if (stems_)
409             end_beam ();
410           return;
411         }
412
413       if (Stem::get_beam (stem))
414         {
415           if (stems_)
416             junk_beam ();
417           return ;
418         }
419               
420       int durlog  = unsmob_duration (m->get_mus_property ("duration"))->duration_log ();
421       
422       if (durlog <= 2)
423         {
424           if (stems_)
425             end_beam ();
426           return;
427         }
428
429
430       /*
431         ignore grace notes.
432        */
433       if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
434         return ;
435         
436       
437       Moment dur = unsmob_duration (m->get_mus_property ("duration"))->get_length ();
438       /* FIXME:
439
440         This comment has been here since long:
441
442            if shortest duration would change
443             consider ending and beginning beam first. 
444
445         but the code didn't match: */
446 #if 1
447       consider_end (dur);
448       consider_begin (dur);
449
450       if (dur < shortest_mom_)
451         shortest_mom_ = dur;
452 #else
453       /* I very much suspect that we wanted: */
454
455       consider_end (shortest_mom_);
456       if (dur < shortest_mom_)
457         {
458           shortest_mom_ = dur;
459           consider_end (shortest_mom_);
460         }
461       consider_begin (shortest_mom_);
462 #endif
463
464       if (!stems_)
465         return;
466       
467       Moment now = now_mom ();
468       
469       grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
470                              durlog - 2);
471       stems_->push (stem);
472       last_add_mom_ = now;
473       extend_mom_ = (extend_mom_ >? now) + m->get_length ();
474     }
475 }
476
477 void
478 Auto_beam_engraver::process_acknowledged_grobs ()
479 {
480   if (!count_)
481     {
482       consider_end (shortest_mom_);
483       consider_begin (shortest_mom_);
484     }
485   else if (count_ > 1)
486     {
487       if (stems_)
488         {
489           Moment now = now_mom ();
490           if ((extend_mom_ < now)
491               || ((extend_mom_ == now) && (last_add_mom_ != now)))
492             {
493               end_beam ();
494             }
495           else if (!stems_->size ())
496             {
497               junk_beam ();
498             }
499         }    
500     }
501   
502   count_ ++;
503 }
504
505 ENTER_DESCRIPTION (Auto_beam_engraver,
506 /* descr */       "Generate beams based on measure characteristics and observed "
507 "Stems.  Uses beatLength, measureLength and measurePosition to decide "
508 "when to start and stop a beam.  Overriding beaming is done through "
509 "@ref{Stem_engraver} properties stemLeftBeamCount and "
510 "stemRightBeamCount. "
511 ,
512 /* creats*/       "Beam",
513 /* accepts */     "",
514 /* acks  */      "stem-interface rest-interface beam-interface bar-line-interface",
515 /* reads */       "autoBeaming autoBeamSettings beatLength subdivideBeams",
516 /* write */       "");