*** empty log message ***
[lilypond.git] / lily / beam-engraver.cc
blob917d2db3c1193b258892b7d42d4976f85ee153be
1 /*
2 beam-engraver.cc -- implement Beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
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"
21 class Beam_engraver : public Engraver
23 protected:
24 Music *start_ev_;
26 Spanner *finished_beam_;
27 Spanner *beam_;
28 Music *prev_start_ev_;
30 Music *now_stop_ev_;
32 Beaming_info_list *beam_info_;
33 Beaming_info_list *finished_beam_info_;
35 /// location within measure where beam started.
36 Moment beam_start_location_;
38 /// moment (global time) where beam started.
39 Moment beam_start_mom_;
41 bool subdivide_beams_;
42 Moment beat_length_;
44 void typeset_beam ();
45 void set_melisma (bool);
47 Moment last_stem_added_at_;
48 virtual void stop_translation_timestep ();
49 virtual void start_translation_timestep ();
50 virtual void finalize ();
52 virtual void acknowledge_grob (Grob_info);
53 virtual bool try_music (Music *);
54 virtual void process_music ();
56 virtual bool valid_start_point ();
57 virtual bool valid_end_point ();
59 public:
60 TRANSLATOR_DECLARATIONS (Beam_engraver);
64 Hmm. this isn't necessary, since grace beams and normal beams are
65 always nested.
67 bool
68 Beam_engraver::valid_start_point ()
70 Moment n = now_mom ();
72 return n.grace_part_ == Rational (0);
75 bool
76 Beam_engraver::valid_end_point ()
78 return valid_start_point ();
81 Beam_engraver::Beam_engraver ()
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;
92 bool
93 Beam_engraver::try_music (Music *m)
95 if (m->is_mus_type ("beam-event"))
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;
103 if (d == START)
105 start_ev_ = m;
107 else if (d == STOP)
109 now_stop_ev_ = m;
111 return true;
113 return false;
116 void
117 Beam_engraver::set_melisma (bool ml)
119 SCM b = get_property ("autoBeaming");
120 if (!to_boolean (b))
121 context ()->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T : SCM_BOOL_F);
124 void
125 Beam_engraver::process_music ()
127 if (beam_ && !to_boolean (get_property ("allowBeamBreak")))
129 get_score_engraver ()->forbid_breaks ();
132 if (start_ev_)
134 if (beam_)
136 start_ev_->origin ()->warning (_ ("already have a beam"));
137 return;
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)));
145 beam_start_location_ = mp;
146 beam_start_mom_ = now_mom ();
148 beam_info_ = new Beaming_info_list;
150 /* urg, must copy to Auto_beam_engraver too */
154 void
155 Beam_engraver::typeset_beam ()
157 if (finished_beam_)
159 finished_beam_info_->beamify (beat_length_, subdivide_beams_);
160 Beam::set_beaming (finished_beam_, finished_beam_info_);
162 delete finished_beam_info_;
163 finished_beam_info_ = 0;
164 finished_beam_ = 0;
168 void
169 Beam_engraver::start_translation_timestep ()
171 start_ev_ = 0;
173 if (beam_)
175 set_melisma (true);
177 subdivide_beams_ = to_boolean (get_property ("subdivideBeams"));
178 beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment (1, 4));
182 void
183 Beam_engraver::stop_translation_timestep ()
185 typeset_beam ();
186 if (now_stop_ev_)
188 finished_beam_ = beam_;
189 finished_beam_info_ = beam_info_;
191 now_stop_ev_ = 0;
192 beam_ = 0;
193 beam_info_ = 0;
194 typeset_beam ();
195 set_melisma (false);
199 void
200 Beam_engraver::finalize ()
202 typeset_beam ();
203 if (beam_)
205 prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
208 we don't typeset it, (we used to, but it was commented
209 out. Reason unknown) */
210 beam_->suicide ();
211 delete beam_info_;
215 void
216 Beam_engraver::acknowledge_grob (Grob_info info)
218 if (beam_)
220 if (Rest::has_interface (info.grob ()))
222 info.grob ()->add_offset_callback (Beam::rest_collision_callback_proc, Y_AXIS);
224 else if (Stem::has_interface (info.grob ()))
226 Moment now = now_mom ();
228 if (!valid_start_point ())
229 return;
231 Item *stem = dynamic_cast<Item *> (info.grob ());
232 if (Stem::get_beam (stem))
233 return;
235 Music *m = info.music_cause ();
236 if (!m->is_mus_type ("rhythmic-event"))
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);
244 return;
247 last_stem_added_at_ = now;
248 int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
249 if (durlog <= 2)
251 m->origin ()->warning (_ ("stem doesn't fit in beam"));
252 prev_start_ev_->origin ()->warning (_ ("beam was started here"));
254 don't return, since
256 [r4 c8] can just as well be modern notation.
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);
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 */ "");
279 class Grace_beam_engraver : public Beam_engraver
281 public:
282 TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
284 protected:
285 virtual bool valid_start_point ();
286 virtual bool valid_end_point ();
289 Grace_beam_engraver::Grace_beam_engraver ()
293 bool
294 Grace_beam_engraver::valid_start_point ()
296 Moment n = now_mom ();
298 return n.grace_part_ != Rational (0);
301 bool
302 Grace_beam_engraver::valid_end_point ()
304 return beam_ && valid_start_point ();
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 */ "");