]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
* flower
[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
21 class Beam_engraver : public Engraver
22 {
23 protected:
24   Music *start_ev_;
25
26   Spanner *finished_beam_;
27   Spanner *beam_;
28   Music *prev_start_ev_;
29
30   Music *now_stop_ev_;
31
32   Beaming_info_list *beam_info_;
33   Beaming_info_list *finished_beam_info_;
34
35   /// location  within measure where beam started.
36   Moment beam_start_location_;
37
38   /// moment (global time) where beam started.
39   Moment beam_start_mom_;
40
41   bool subdivide_beams_;
42   Moment beat_length_;
43
44   void typeset_beam ();
45   void set_melisma (bool);
46
47   Moment last_stem_added_at_;
48   virtual void stop_translation_timestep ();
49   virtual void start_translation_timestep ();
50   virtual void finalize ();
51
52   virtual void acknowledge_grob (Grob_info);
53   virtual bool try_music (Music *);
54   virtual void process_music ();
55
56   virtual bool valid_start_point ();
57   virtual bool valid_end_point ();
58
59 public:
60   TRANSLATOR_DECLARATIONS (Beam_engraver);
61 };
62
63 /*
64   Hmm. this isn't necessary, since grace beams and normal beams are
65   always nested.
66 */
67 bool
68 Beam_engraver::valid_start_point ()
69 {
70   Moment n = now_mom ();
71
72   return n.grace_part_ == Rational (0);
73 }
74
75 bool
76 Beam_engraver::valid_end_point ()
77 {
78   return valid_start_point ();
79 }
80
81 Beam_engraver::Beam_engraver ()
82 {
83   beam_ = 0;
84   finished_beam_ = 0;
85   finished_beam_info_ = 0;
86   beam_info_ = 0;
87   now_stop_ev_ = 0;
88   start_ev_ = 0;
89   prev_start_ev_ = 0;
90 }
91
92 bool
93 Beam_engraver::try_music (Music *m)
94 {
95   if (m->is_mus_type ("beam-event"))
96     {
97       Direction d = to_dir (m->get_property ("span-direction"));
98       if (d == START && !valid_start_point ())
99         return false;
100       if (d == STOP && !valid_end_point ())
101         return false;
102
103       if (d == START)
104         {
105           start_ev_ = m;
106         }
107       else if (d == STOP)
108         {
109           now_stop_ev_ = m;
110         }
111       return true;
112     }
113   return false;
114 }
115
116 void
117 Beam_engraver::set_melisma (bool ml)
118 {
119   SCM b = get_property ("autoBeaming");
120   if (!to_boolean (b))
121     context ()->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T :SCM_BOOL_F);
122 }
123
124 void
125 Beam_engraver::process_music ()
126 {
127   if (beam_ && !to_boolean (get_property ("allowBeamBreak")))
128     {
129       get_score_engraver ()->forbid_breaks ();
130     }
131
132   if (start_ev_)
133     {
134       if (beam_)
135         {
136           start_ev_->origin ()->warning (_ ("already have a beam"));
137           return;
138         }
139
140       set_melisma (true);
141       prev_start_ev_ = start_ev_;
142       beam_ = make_spanner ("Beam", start_ev_->self_scm ());
143       Moment mp (robust_scm2moment (get_property ("measurePosition"), Moment (0)));
144
145       beam_start_location_ = mp;
146       beam_start_mom_ = now_mom ();
147
148       beam_info_ = new Beaming_info_list;
149
150       /* urg, must copy to Auto_beam_engraver too */
151     }
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                                 (durlog- 2) >? 0);
266           Beam::add_stem (beam_, stem);
267         }
268     }
269 }
270
271
272 ADD_TRANSLATOR (Beam_engraver,
273                 /* descr */ "Handles Beam events by engraving Beams.    If omitted, then notes will be "
274                 "printed with flags instead of beams.",
275                 /* creats*/ "Beam",
276                 /* accepts */ "beam-event",
277                 /* acks  */ "stem-interface rest-interface",
278                 /* reads */ "beamMelismaBusy beatLength subdivideBeams",
279                 /* write */ "");
280
281 class Grace_beam_engraver : public Beam_engraver
282 {
283 public:
284   TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
285
286 protected:
287   virtual bool valid_start_point ();
288   virtual bool valid_end_point ();
289 };
290
291 Grace_beam_engraver::Grace_beam_engraver ()
292 {
293 }
294
295 bool
296 Grace_beam_engraver::valid_start_point ()
297 {
298   Moment n = now_mom ();
299
300   return n.grace_part_ != Rational (0);
301 }
302
303 bool
304 Grace_beam_engraver::valid_end_point ()
305 {
306   return beam_ && valid_start_point ();
307 }
308
309 ADD_TRANSLATOR (Grace_beam_engraver,
310                 /* descr */ "Handles Beam events by engraving Beams.  If omitted, then notes will "
311                 "be printed with flags instead of beams. Only engraves beams when we "
312                 " are at grace points in time. ",
313                 /* creats*/ "Beam",
314                 /* accepts */ "beam-event",
315                 /* acks  */ "stem-interface rest-interface",
316                 /* reads */ "beamMelismaBusy beatLength allowBeamBreak subdivideBeams",
317                 /* write */ "");
318