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