]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
* lily/include/context.hh (class Context): make members protected.
[lilypond.git] / lily / beam-engraver.cc
1 /*   
2   beam-engraver.cc --  implement Beam_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "engraver.hh"
11 #include "event.hh"
12 #include "beam.hh"
13 #include "stem.hh"
14 #include "warn.hh"
15 #include "beaming.hh"
16 #include "score-engraver.hh"
17 #include "rest.hh"
18 #include "drul-array.hh"
19 #include "item.hh"
20 #include "spanner.hh"
21 #include "context.hh"
22
23 class Beam_engraver : public Engraver
24 {
25 protected:  
26   Music * start_ev_;
27   
28   Spanner *finished_beam_;
29   Spanner *beam_;
30   Music * prev_start_ev_;
31
32   Music * now_stop_ev_;
33   
34   Beaming_info_list * beam_info_;
35   Beaming_info_list * finished_beam_info_;  
36
37   /// location  within measure where beam started.
38   Moment beam_start_location_;
39
40   /// moment (global time) where beam started.
41   Moment beam_start_mom_;
42
43   bool subdivide_beams_;
44   Moment beat_length_;
45
46   void typeset_beam ();
47   void set_melisma (bool);
48
49   Moment last_stem_added_at_;
50   virtual void stop_translation_timestep ();
51   virtual void start_translation_timestep ();
52   virtual void finalize ();
53
54   virtual void acknowledge_grob (Grob_info);
55   virtual bool try_music (Music*);
56   virtual void process_music ();
57
58   virtual bool valid_start_point ();
59   virtual bool valid_end_point ();
60   
61 public:
62   TRANSLATOR_DECLARATIONS (Beam_engraver);
63 };
64
65
66 /*
67   Hmm. this isn't necessary, since grace beams and normal beams are
68   always nested.
69  */
70 bool
71 Beam_engraver::valid_start_point ()
72 {
73   Moment n = now_mom ();
74
75   return n.grace_part_ == Rational (0);
76 }
77
78 bool
79 Beam_engraver::valid_end_point ()
80 {
81   return valid_start_point ();
82 }
83
84 Beam_engraver::Beam_engraver ()
85 {
86   beam_ = 0;
87   finished_beam_ =0;
88   finished_beam_info_=0;
89   beam_info_ =0;
90   now_stop_ev_ = 0;
91   start_ev_ = 0;
92   prev_start_ev_ =0;
93 }
94
95 bool
96 Beam_engraver::try_music (Music *m)
97 {
98   if (m->is_mus_type ("beam-event"))
99     {
100       Direction d = to_dir (m->get_property ("span-direction"));
101       if (d == START && !valid_start_point ())
102         return false;
103       if (d == STOP && !valid_end_point ())
104         return false;
105
106       if (d == START)
107         {
108           start_ev_ = m;
109         }
110       else if (d==STOP)
111         {
112           now_stop_ev_ = m;
113         }
114       return true;
115     }
116   return false;
117 }
118
119 void
120 Beam_engraver::set_melisma (bool ml)
121 {
122   SCM b = get_property ("autoBeaming");
123   if (!to_boolean (b))
124     get_parent_context ()->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T :SCM_BOOL_F);
125 }
126
127 void
128 Beam_engraver::process_music ()
129 {
130   if (beam_ && !to_boolean (get_property ("allowBeamBreak")))
131     {
132       get_score_engraver ()->forbid_breaks ();
133     }
134
135   if (start_ev_)
136     {
137       if (beam_)
138         {
139           start_ev_->origin ()->warning (_ ("already have a beam"));
140           return;
141         }
142
143       set_melisma (true);
144       prev_start_ev_ = start_ev_;
145       beam_ = make_spanner ("Beam");
146       SCM smp = get_property ("measurePosition");
147       Moment mp = (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
148
149       beam_start_location_ = mp;
150       beam_start_mom_ = now_mom ();
151       
152       beam_info_ = new Beaming_info_list;
153       
154       /* urg, must copy to Auto_beam_engraver too */
155       announce_grob (beam_, start_ev_->self_scm ());
156     }
157
158 }
159
160
161 void
162 Beam_engraver::typeset_beam ()
163 {
164   if (finished_beam_)
165     {
166       finished_beam_info_->beamify (beat_length_, subdivide_beams_);
167       Beam::set_beaming (finished_beam_, finished_beam_info_);
168       typeset_grob (finished_beam_);
169       delete finished_beam_info_;
170       finished_beam_info_ =0;
171       finished_beam_ = 0;
172     }
173 }
174
175 void
176 Beam_engraver::start_translation_timestep ()
177 {
178   start_ev_ = 0;
179   
180   if (beam_)
181     {
182       set_melisma (true);
183       
184       subdivide_beams_ = to_boolean (get_property ("subdivideBeams"));
185       beat_length_ = *unsmob_moment (get_property ("beatLength"));
186     }
187 }
188
189 void
190 Beam_engraver::stop_translation_timestep ()
191 {
192   typeset_beam ();
193   if (now_stop_ev_ )
194     {
195       finished_beam_ = beam_;
196       finished_beam_info_ = beam_info_;
197
198       now_stop_ev_ = 0;
199       beam_ = 0;
200       beam_info_ = 0;
201       typeset_beam ();
202       set_melisma (false);
203     }
204 }
205
206 void
207 Beam_engraver::finalize ()
208 {
209   typeset_beam ();
210   if (beam_)
211     {
212       prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
213
214       /*
215         we don't typeset it, (we used to, but it was commented
216         out. Reason unknown) */
217       beam_->suicide ();
218       delete beam_info_;
219     }
220 }
221
222 void
223 Beam_engraver::acknowledge_grob (Grob_info info)
224 {
225   if (beam_)
226     {
227       if (Rest::has_interface (info.grob_))
228         {
229           info.grob_->add_offset_callback (Beam::rest_collision_callback_proc, Y_AXIS);
230         }
231       else if (Stem::has_interface (info.grob_))
232         {
233           Moment now = now_mom ();
234
235           if (!valid_start_point ())
236             return ;
237           
238           Item *stem = dynamic_cast<Item*> (info.grob_);
239           if (Stem::get_beam (stem))
240             return;
241
242           Music* m = info.music_cause ();
243           if (!m->is_mus_type ("rhythmic-event"))
244             {
245               String s = _ ("stem must have Rhythmic structure");
246               if (info.music_cause ())
247                 info.music_cause ()->origin ()->warning (s);
248               else
249                 ::warning (s);
250           
251               return;
252             }
253
254
255           last_stem_added_at_ = now;
256           int durlog  = unsmob_duration (m->get_property ("duration"))-> duration_log ();
257           if (durlog <= 2)
258             {
259               m->origin ()->warning (_ ("stem doesn't fit in beam"));
260               prev_start_ev_->origin ()->warning (_ ("beam was started here"));
261               /*
262                 don't return, since
263
264                 [r4 c8] can just as well be modern notation.
265               */
266             }
267
268           stem->set_property ("duration-log",
269                                     scm_int2num (durlog));
270           Moment stem_location = now - beam_start_mom_ + beam_start_location_;
271           beam_info_->add_stem (stem_location,
272  (durlog- 2) >? 0);
273           Beam::add_stem (beam_, stem);
274         }
275     }
276 }
277
278
279
280
281
282 ENTER_DESCRIPTION (Beam_engraver,
283 /* descr */       "Handles Beam events by engraving Beams.    If omitted, then notes will be "
284 "printed with flags instead of beams.",
285 /* creats*/       "Beam",
286 /* accepts */     "beam-event",
287 /* acks  */      "stem-interface rest-interface",
288 /* reads */       "beamMelismaBusy beatLength subdivideBeams",
289 /* write */       "");
290
291
292 class Grace_beam_engraver : public Beam_engraver
293 {
294 public:
295   TRANSLATOR_DECLARATIONS (Grace_beam_engraver);  
296
297 protected:
298   virtual bool valid_start_point ();
299   virtual bool valid_end_point ();
300 };
301
302 Grace_beam_engraver::Grace_beam_engraver ()
303 {
304 }
305
306 bool
307 Grace_beam_engraver::valid_start_point ()
308 {
309   Moment n = now_mom ();
310
311   return n.grace_part_ != Rational (0);
312 }
313
314
315 bool
316 Grace_beam_engraver::valid_end_point ()
317 {
318   return beam_ && valid_start_point ();
319 }
320
321
322
323 ENTER_DESCRIPTION (Grace_beam_engraver,
324 /* descr */       "Handles Beam events by engraving Beams.  If omitted, then notes will "
325 "be printed with flags instead of beams. Only engraves beams when we "
326 " are at grace points in time. "
327 ,
328 /* creats*/       "Beam",
329 /* accepts */     "beam-event",
330 /* acks  */      "stem-interface rest-interface",
331 /* reads */       "beamMelismaBusy beatLength allowBeamBreak subdivideBeams",
332 /* write */       "");
333