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