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