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