]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
* flower/*.cc: remove <? and >?
[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 void
155 Beam_engraver::typeset_beam ()
156 {
157   if (finished_beam_)
158     {
159       finished_beam_info_->beamify (beat_length_, subdivide_beams_);
160       Beam::set_beaming (finished_beam_, finished_beam_info_);
161
162       delete finished_beam_info_;
163       finished_beam_info_ = 0;
164       finished_beam_ = 0;
165     }
166 }
167
168 void
169 Beam_engraver::start_translation_timestep ()
170 {
171   start_ev_ = 0;
172
173   if (beam_)
174     {
175       set_melisma (true);
176
177       subdivide_beams_ = to_boolean (get_property ("subdivideBeams"));
178       beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
179     }
180 }
181
182 void
183 Beam_engraver::stop_translation_timestep ()
184 {
185   typeset_beam ();
186   if (now_stop_ev_)
187     {
188       finished_beam_ = beam_;
189       finished_beam_info_ = beam_info_;
190
191       now_stop_ev_ = 0;
192       beam_ = 0;
193       beam_info_ = 0;
194       typeset_beam ();
195       set_melisma (false);
196     }
197 }
198
199 void
200 Beam_engraver::finalize ()
201 {
202   typeset_beam ();
203   if (beam_)
204     {
205       prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
206
207       /*
208         we don't typeset it, (we used to, but it was commented
209         out. Reason unknown) */
210       beam_->suicide ();
211       delete beam_info_;
212     }
213 }
214
215 void
216 Beam_engraver::acknowledge_grob (Grob_info info)
217 {
218   if (beam_)
219     {
220       if (Rest::has_interface (info.grob_))
221         {
222           info.grob_->add_offset_callback (Beam::rest_collision_callback_proc, Y_AXIS);
223         }
224       else if (Stem::has_interface (info.grob_))
225         {
226           Moment now = now_mom ();
227
228           if (!valid_start_point ())
229             return;
230
231           Item *stem = dynamic_cast<Item *> (info.grob_);
232           if (Stem::get_beam (stem))
233             return;
234
235           Music *m = info.music_cause ();
236           if (!m->is_mus_type ("rhythmic-event"))
237             {
238               String s = _ ("stem must have Rhythmic structure");
239               if (info.music_cause ())
240                 info.music_cause ()->origin ()->warning (s);
241               else
242                 ::warning (s);
243
244               return;
245             }
246
247           last_stem_added_at_ = now;
248           int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
249           if (durlog <= 2)
250             {
251               m->origin ()->warning (_ ("stem doesn't fit in beam"));
252               prev_start_ev_->origin ()->warning (_ ("beam was started here"));
253               /*
254                 don't return, since
255
256                 [r4 c8] can just as well be modern notation.
257               */
258             }
259
260           stem->set_property ("duration-log",
261                               scm_int2num (durlog));
262           Moment stem_location = now - beam_start_mom_ + beam_start_location_;
263           beam_info_->add_stem (stem_location,
264                                 max (durlog- 2, 0));
265           Beam::add_stem (beam_, stem);
266         }
267     }
268 }
269
270 ADD_TRANSLATOR (Beam_engraver,
271                 /* descr */ "Handles Beam events by engraving Beams.    If omitted, then notes will be "
272                 "printed with flags instead of beams.",
273                 /* creats*/ "Beam",
274                 /* accepts */ "beam-event",
275                 /* acks  */ "stem-interface rest-interface",
276                 /* reads */ "beamMelismaBusy beatLength subdivideBeams",
277                 /* write */ "");
278
279 class Grace_beam_engraver : public Beam_engraver
280 {
281 public:
282   TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
283
284 protected:
285   virtual bool valid_start_point ();
286   virtual bool valid_end_point ();
287 };
288
289 Grace_beam_engraver::Grace_beam_engraver ()
290 {
291 }
292
293 bool
294 Grace_beam_engraver::valid_start_point ()
295 {
296   Moment n = now_mom ();
297
298   return n.grace_part_ != Rational (0);
299 }
300
301 bool
302 Grace_beam_engraver::valid_end_point ()
303 {
304   return beam_ && valid_start_point ();
305 }
306
307 ADD_TRANSLATOR (Grace_beam_engraver,
308                 /* descr */ "Handles Beam events by engraving Beams.  If omitted, then notes will "
309                 "be printed with flags instead of beams. Only engraves beams when we "
310                 " are at grace points in time. ",
311                 /* creats*/ "Beam",
312                 /* accepts */ "beam-event",
313                 /* acks  */ "stem-interface rest-interface",
314                 /* reads */ "beamMelismaBusy beatLength allowBeamBreak subdivideBeams",
315                 /* write */ "");
316