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