]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
2003 -> 2004
[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   SCM settings = get_property ("autoBeamSettings");
149   
150   /* first guess */
151   
152   /* begin beam at any position
153  (and fallback for end) */
154   Moment moment (0);
155   
156   /* end beam at end of beat */
157   if (dir == STOP)
158     {
159       SCM beat (get_property ("beatLength"));
160       
161       if (unsmob_moment (beat))
162         moment = *unsmob_moment (beat);
163     }
164
165   /* second guess: property generic time exception */
166   SCM m = scm_assoc (gh_append3 (function, wild, time), settings);
167   
168   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
169     moment = * unsmob_moment (ly_cdr (m));
170
171   /* third guess: property time exception, specific for duration type */
172   m = scm_assoc (gh_append3 (function, type, time), settings);
173   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
174     moment = * unsmob_moment (ly_cdr (m));
175
176   /* fourth guess [user override]: property plain generic */
177   m = scm_assoc (gh_append3 (function, wild, wild), settings);
178   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
179     moment = * unsmob_moment (ly_cdr (m));
180
181   /* fifth guess [user override]: property plain, specific for duration type */
182   m = scm_assoc (gh_append3 (function, type, wild), settings);
183   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
184     moment = * unsmob_moment (ly_cdr (m));
185   
186   Rational r;
187   if (moment.to_bool ())
188     {
189       /* Ugh? measurePosition can be negative, when \partial
190          We may have to fix this elsewhere (timing translator)
191         r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
192       */
193       Moment pos = * unsmob_moment (get_property ("measurePosition"));
194       if (pos < Moment (0))
195         {
196           Moment length = * unsmob_moment (get_property ("measureLength"));
197           pos = length - pos;
198         }
199       r = pos.main_part_.mod_rat (moment.main_part_);
200     }
201   else
202     {
203       if (dir == START)
204         /* if undefined, starting is ok */
205         r = 0;
206       else
207         /* but ending is not */
208         r = 1;
209     }
210
211   return !r;
212 }
213
214 void
215 Auto_beam_engraver::consider_begin (Moment test_mom)
216 {
217   bool on = to_boolean (get_property ("autoBeaming"));
218   if (!stems_ && on)
219     {
220       bool b = test_moment (START, test_mom);
221       if (b)
222         begin_beam ();
223     }
224 }
225
226 void
227 Auto_beam_engraver::consider_end (Moment test_mom)
228 {
229   if (stems_)
230     {
231       /* Allow already started autobeam to end:
232          don't check for autoBeaming */
233       bool b = test_moment (STOP, test_mom);
234       if (b)
235         end_beam ();
236     }
237 }
238
239 Spanner*
240 Auto_beam_engraver::create_beam ()
241 {
242   if (to_boolean (get_property ("skipTypesetting")))
243     {
244      return 0;
245     }
246   
247   Spanner* beam = new Spanner (beam_settings_);
248   for (int i = 0; i < stems_->size (); i++)
249     {
250       /*
251         watch out for stem tremolos and abbreviation beams
252        */
253       if (Stem::get_beam ((*stems_)[i]))
254         {
255           scm_gc_unprotect_object (beam->self_scm ());
256           return 0;
257         }
258       Beam::add_stem (beam, (*stems_)[i]);
259     }
260   
261   announce_grob(beam, SCM_EOL);
262
263   return beam;
264 }
265
266 void
267 Auto_beam_engraver::begin_beam ()
268 {
269   if (stems_ || grouping_ )
270     {
271       programming_error ("already have autobeam");
272       return; 
273     }
274   
275   stems_ = new Link_array<Item>;
276   grouping_ = new Beaming_info_list;
277   beam_settings_ = get_property ("Beam");
278   
279   beam_start_moment_ = now_mom ();
280   beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
281   subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
282   beat_length_ = *unsmob_moment (get_property ("beatLength"));
283 }
284
285 void
286 Auto_beam_engraver::junk_beam () 
287 {
288   if (!stems_)
289     return ;
290   
291   delete stems_;
292   stems_ = 0;
293   delete grouping_;
294   grouping_ = 0;
295   beam_settings_ = SCM_EOL;
296   
297   shortest_mom_ = Moment (Rational (1, 8));
298 }
299
300 void
301 Auto_beam_engraver::end_beam ()
302 {
303   if (stems_->size () < 2)
304     {
305       junk_beam ();
306     }
307   else
308     {
309       finished_beam_ = create_beam ();
310       if (finished_beam_)
311         finished_grouping_ = grouping_;
312       delete stems_;
313       stems_ = 0;
314       grouping_ = 0;
315       beam_settings_ = SCM_EOL;
316     }
317
318   shortest_mom_ = Moment (Rational (1, 8));
319 }
320
321 void
322 Auto_beam_engraver::typeset_beam ()
323 {
324   if (finished_beam_)
325     {
326       finished_grouping_->beamify(beat_length_, subdivide_beams_);
327       Beam::set_beaming (finished_beam_, finished_grouping_);
328       typeset_grob (finished_beam_);
329       finished_beam_ = 0;
330     
331       delete finished_grouping_;
332       finished_grouping_= 0;
333     }
334 }
335
336 void
337 Auto_beam_engraver::start_translation_timestep ()
338 {
339   count_ = 0;
340   /*
341     don't beam over skips
342    */
343   if (stems_)
344     {
345       Moment now = now_mom ();
346       if (extend_mom_ < now)
347         {
348           end_beam ();
349         }
350     }
351 }
352
353 void
354 Auto_beam_engraver::stop_translation_timestep ()
355 {
356   typeset_beam ();
357 }
358
359 void
360 Auto_beam_engraver::finalize ()
361 {
362   /* finished beams may be typeset */
363   typeset_beam ();
364   /* but unfinished may need another announce/acknowledge pass */
365   if (stems_)
366     junk_beam ();
367 }
368
369
370 void
371 Auto_beam_engraver::acknowledge_grob (Grob_info info)
372 {
373   if (stems_)
374     {
375       if (Beam::has_interface (info.grob_))
376         {
377           end_beam ();
378         }
379       else if (Bar_line::has_interface (info.grob_))
380         {
381           end_beam ();
382         }
383       else if (Rest::has_interface (info.grob_))
384         {
385           end_beam ();
386         }
387     }
388   
389   if (Stem::has_interface (info.grob_))
390     {
391       Item* stem = dynamic_cast<Item *> (info.grob_);
392       Music* m = info.music_cause ();
393       if (!m->is_mus_type ("rhythmic-event"))
394         {
395           programming_error ("Stem must have rhythmic structure");
396           return;
397         }
398       
399       /*
400         Don't (start) auto-beam over empty stems; skips or rests
401         */
402       if (!Stem::head_count (stem))
403         {
404           if (stems_)
405             end_beam ();
406           return;
407         }
408
409       if (Stem::get_beam (stem))
410         {
411           if (stems_)
412             junk_beam ();
413           return ;
414         }
415               
416       int durlog  = unsmob_duration (m->get_mus_property ("duration"))->duration_log ();
417       
418       if (durlog <= 2)
419         {
420           if (stems_)
421             end_beam ();
422           return;
423         }
424
425
426       /*
427         ignore grace notes.
428        */
429       if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
430         return ;
431         
432       
433       Moment dur = unsmob_duration (m->get_mus_property ("duration"))->get_length ();
434       /* FIXME:
435
436         This comment has been here since long:
437
438            if shortest duration would change
439             consider ending and beginning beam first. 
440
441         but the code didn't match: */
442 #if 1
443       consider_end (dur);
444       consider_begin (dur);
445
446       if (dur < shortest_mom_)
447         shortest_mom_ = dur;
448 #else
449       /* I very much suspect that we wanted: */
450
451       consider_end (shortest_mom_);
452       if (dur < shortest_mom_)
453         {
454           shortest_mom_ = dur;
455           consider_end (shortest_mom_);
456         }
457       consider_begin (shortest_mom_);
458 #endif
459
460       if (!stems_)
461         return;
462       
463       Moment now = now_mom ();
464       
465       grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
466                              durlog - 2);
467       stems_->push (stem);
468       last_add_mom_ = now;
469       extend_mom_ = (extend_mom_ >? now) + m->get_length ();
470     }
471 }
472
473 void
474 Auto_beam_engraver::process_acknowledged_grobs ()
475 {
476   if (!count_)
477     {
478       consider_end (shortest_mom_);
479       consider_begin (shortest_mom_);
480     }
481   else if (count_ > 1)
482     {
483       if (stems_)
484         {
485           Moment now = now_mom ();
486           if ((extend_mom_ < now)
487               || ((extend_mom_ == now) && (last_add_mom_ != now)))
488             {
489               end_beam ();
490             }
491           else if (!stems_->size ())
492             {
493               junk_beam ();
494             }
495         }    
496     }
497   
498   count_ ++;
499 }
500
501 ENTER_DESCRIPTION (Auto_beam_engraver,
502 /* descr */       "Generate beams based on measure characteristics and observed "
503 "Stems.  Uses beatLength, measureLength and measurePosition to decide "
504 "when to start and stop a beam.  Overriding beaming is done through "
505 "@ref{Stem_engraver} properties stemLeftBeamCount and "
506 "stemRightBeamCount. "
507 ,
508 /* creats*/       "Beam",
509 /* accepts */     "",
510 /* acks  */      "stem-interface rest-interface beam-interface bar-line-interface",
511 /* reads */       "autoBeaming autoBeamSettings beatLength subdivideBeams",
512 /* write */       "");