lilypond-1.5.1
[lilypond.git] / lily / beam-engraver.cc
blob4445b79643e4192f3dd68bc5a82cec6ef988014e
1 /*
2 beam-engraver.cc -- implement Beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9 #include "engraver-group-engraver.hh"
10 #include "engraver.hh"
11 #include "musical-request.hh"
12 #include "beam.hh"
13 #include "stem.hh"
14 #include "warn.hh"
15 #include "beaming.hh"
16 #include "score-engraver.hh"
17 #include "rest.hh"
18 #include "drul-array.hh"
19 #include "item.hh"
20 #include "spanner.hh"
22 class Beam_engraver : public Engraver
24 Drul_array<Span_req*> reqs_drul_;
26 Link_array<Stem> stems_;
29 Spanner *finished_beam_p_;
30 Spanner *beam_p_;
31 Span_req * prev_start_req_;
33 Beaming_info_list * beam_info_p_;
34 Beaming_info_list * finished_beam_info_p_;
36 /// location within measure where beam started.
37 Moment beam_start_location_;
39 /// moment (global time) where beam started.
40 Moment beam_start_mom_;
42 void typeset_beam ();
43 void set_melisma (bool);
44 protected:
45 virtual void stop_translation_timestep ();
46 virtual void start_translation_timestep ();
47 virtual void finalize ();
48 virtual void create_grobs ();
49 virtual void acknowledge_grob (Grob_info);
50 virtual bool try_music (Music*);
51 virtual void process_music ();
53 public:
54 Beam_engraver ();
55 VIRTUAL_COPY_CONS (Translator);
59 Beam_engraver::Beam_engraver ()
61 beam_p_ = 0;
62 finished_beam_p_ =0;
63 finished_beam_info_p_=0;
64 beam_info_p_ =0;
65 reqs_drul_[LEFT] = reqs_drul_[RIGHT] =0;
66 prev_start_req_ =0;
69 bool
70 Beam_engraver::try_music (Music *m)
72 if (Span_req * c = dynamic_cast<Span_req*> (m))
74 if (scm_equal_p (c->get_mus_property ("span-type"),
75 ly_str02scm ("abort")) == SCM_BOOL_T)
77 reqs_drul_[START] = 0;
78 reqs_drul_[STOP] = 0;
79 if (beam_p_)
80 beam_p_->suicide ();
81 beam_p_ = 0;
83 else if (scm_equal_p (c->get_mus_property ("span-type"),
84 ly_str02scm ("beam")) == SCM_BOOL_T)
87 Direction d =c->get_span_dir ();
89 if (d == STOP && !beam_p_)
91 m->origin ()->warning (_ ("can't find start of beam"));
92 return false;
95 if (d == STOP)
97 SCM m = get_property ("automaticMelismata");
98 SCM b = get_property ("noAutoBeaming");
99 if (to_boolean (m) && to_boolean (b))
101 set_melisma (false);
105 reqs_drul_[d ] = c;
106 return true;
109 return false;
112 void
113 Beam_engraver::set_melisma (bool m)
115 daddy_trans_l_->set_property ("beamMelismaBusy", m ? SCM_BOOL_T :SCM_BOOL_F);
118 void
119 Beam_engraver::process_music ()
121 if (reqs_drul_[STOP])
123 if (!beam_p_)
124 reqs_drul_[STOP]->origin ()->warning (_ ("can't find start of beam"));
125 prev_start_req_ =0;
126 finished_beam_p_ = beam_p_;
127 finished_beam_info_p_ = beam_info_p_;
129 beam_info_p_ =0;
130 beam_p_ = 0;
134 if (beam_p_)
136 Score_engraver * e = 0;
137 Translator * t = daddy_grav_l ();
138 for (; !e && t; t = t->daddy_trans_l_)
140 e = dynamic_cast<Score_engraver*> (t);
143 if (!e)
144 programming_error ("No score engraver!");
145 else
146 e->forbid_breaks ();
151 void
152 Beam_engraver::create_grobs ()
154 if (reqs_drul_[START])
156 if (beam_p_)
158 reqs_drul_[START]->origin ()->warning (_ ("already have a beam"));
159 return;
162 prev_start_req_ = reqs_drul_[START];
163 beam_p_ = new Spanner (get_property ("Beam"));
164 SCM smp = get_property ("measurePosition");
165 Moment mp = (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
167 beam_start_location_ = mp;
168 beam_start_mom_ = now_mom ();
169 beam_info_p_ = new Beaming_info_list;
172 /* urg, must copy to Auto_beam_engraver too */
174 announce_grob (beam_p_, reqs_drul_[START]);
176 reqs_drul_[STOP] = 0;
177 reqs_drul_[START] = 0;
180 void
181 Beam_engraver::typeset_beam ()
183 if (finished_beam_p_)
185 finished_beam_info_p_->beamify ();
187 Beam::set_beaming (finished_beam_p_, finished_beam_info_p_);
188 typeset_grob (finished_beam_p_);
189 delete finished_beam_info_p_;
190 finished_beam_info_p_ =0;
191 finished_beam_p_ = 0;
193 reqs_drul_[STOP] = 0;
197 void
198 Beam_engraver::start_translation_timestep ()
200 reqs_drul_ [START] =0;
201 if (beam_p_) {
202 SCM m = get_property ("automaticMelismata");
203 SCM b = get_property ("noAutoBeaming");
204 if (to_boolean (m) && to_boolean (b)) {
205 set_melisma (true);
210 void
211 Beam_engraver::stop_translation_timestep ()
213 typeset_beam ();
216 void
217 Beam_engraver::finalize ()
219 typeset_beam ();
220 if (beam_p_)
222 prev_start_req_->origin ()->warning (_ ("unterminated beam"));
225 we don't typeset it, (we used to, but it was commented
226 out. Reason unknown) */
227 beam_p_->suicide ();
228 delete beam_info_p_;
232 void
233 Beam_engraver::acknowledge_grob (Grob_info info)
235 if (beam_p_)
237 if (Rest::has_interface (info.elem_l_))
239 info.elem_l_->add_offset_callback (Beam::rest_collision_callback_proc, Y_AXIS);
241 else if (Stem::has_interface (info.elem_l_))
243 Item *stem_l = dynamic_cast<Item*> (info.elem_l_);
244 if (Stem::beam_l (stem_l))
245 return;
247 Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
248 if (!rhythmic_req)
250 String s = _ ("stem must have Rhythmic structure");
251 if (info.req_l_)
252 info.req_l_->origin ()->warning (s);
253 else
254 ::warning (s);
256 return;
259 int durlog = unsmob_duration (rhythmic_req->get_mus_property ("duration"))-> duration_log ();
260 if (durlog <= 2)
262 rhythmic_req->origin ()->warning (_ ("stem doesn't fit in beam"));
263 prev_start_req_->origin ()->warning (_ ("beam was started here"));
265 don't return, since
267 [r4 c8] can just as well be modern notation.
271 stem_l->set_grob_property ("duration-log",
272 gh_int2scm (durlog));
273 Moment stem_location = now_mom () - beam_start_mom_ + beam_start_location_;
274 beam_info_p_->add_stem (stem_location,
275 (durlog- 2) >? 1);
276 Beam::add_stem (beam_p_, stem_l);
283 ADD_THIS_TRANSLATOR (Beam_engraver);