* lily/paper-book.cc: remove copyright & tagline. Remove
[lilypond.git] / lily / beam-engraver.cc
blob4e6238bedf35859f456933059ca9f06db9f2c362
1 /*
2 beam-engraver.cc -- implement Beam_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "engraver.hh"
11 #include "event.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"
21 #include "context.hh"
23 class Beam_engraver : public Engraver
25 protected:
26 Music * start_ev_;
28 Spanner *finished_beam_;
29 Spanner *beam_;
30 Music * prev_start_ev_;
32 Music * now_stop_ev_;
34 Beaming_info_list * beam_info_;
35 Beaming_info_list * finished_beam_info_;
37 /// location within measure where beam started.
38 Moment beam_start_location_;
40 /// moment (global time) where beam started.
41 Moment beam_start_mom_;
43 bool subdivide_beams_;
44 Moment beat_length_;
46 void typeset_beam ();
47 void set_melisma (bool);
49 Moment last_stem_added_at_;
50 virtual void stop_translation_timestep ();
51 virtual void start_translation_timestep ();
52 virtual void finalize ();
54 virtual void acknowledge_grob (Grob_info);
55 virtual bool try_music (Music*);
56 virtual void process_music ();
58 virtual bool valid_start_point ();
59 virtual bool valid_end_point ();
61 public:
62 TRANSLATOR_DECLARATIONS (Beam_engraver);
67 Hmm. this isn't necessary, since grace beams and normal beams are
68 always nested.
70 bool
71 Beam_engraver::valid_start_point ()
73 Moment n = now_mom ();
75 return n.grace_part_ == Rational (0);
78 bool
79 Beam_engraver::valid_end_point ()
81 return valid_start_point ();
84 Beam_engraver::Beam_engraver ()
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;
95 bool
96 Beam_engraver::try_music (Music *m)
98 if (m->is_mus_type ("beam-event"))
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;
106 if (d == START)
108 start_ev_ = m;
110 else if (d==STOP)
112 now_stop_ev_ = m;
114 return true;
116 return false;
119 void
120 Beam_engraver::set_melisma (bool ml)
122 SCM b = get_property ("autoBeaming");
123 if (!to_boolean (b))
124 context ()->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T :SCM_BOOL_F);
127 void
128 Beam_engraver::process_music ()
130 if (beam_ && !to_boolean (get_property ("allowBeamBreak")))
132 get_score_engraver ()->forbid_breaks ();
135 if (start_ev_)
137 if (beam_)
139 start_ev_->origin ()->warning (_ ("already have a beam"));
140 return;
143 set_melisma (true);
144 prev_start_ev_ = start_ev_;
145 beam_ = make_spanner ("Beam", start_ev_->self_scm ());
146 SCM smp = get_property ("measurePosition");
147 Moment mp = (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
149 beam_start_location_ = mp;
150 beam_start_mom_ = now_mom ();
152 beam_info_ = new Beaming_info_list;
154 /* urg, must copy to Auto_beam_engraver too */
160 void
161 Beam_engraver::typeset_beam ()
163 if (finished_beam_)
165 finished_beam_info_->beamify (beat_length_, subdivide_beams_);
166 Beam::set_beaming (finished_beam_, finished_beam_info_);
168 delete finished_beam_info_;
169 finished_beam_info_ =0;
170 finished_beam_ = 0;
174 void
175 Beam_engraver::start_translation_timestep ()
177 start_ev_ = 0;
179 if (beam_)
181 set_melisma (true);
183 subdivide_beams_ = to_boolean (get_property ("subdivideBeams"));
184 beat_length_ = *unsmob_moment (get_property ("beatLength"));
188 void
189 Beam_engraver::stop_translation_timestep ()
191 typeset_beam ();
192 if (now_stop_ev_ )
194 finished_beam_ = beam_;
195 finished_beam_info_ = beam_info_;
197 now_stop_ev_ = 0;
198 beam_ = 0;
199 beam_info_ = 0;
200 typeset_beam ();
201 set_melisma (false);
205 void
206 Beam_engraver::finalize ()
208 typeset_beam ();
209 if (beam_)
211 prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
214 we don't typeset it, (we used to, but it was commented
215 out. Reason unknown) */
216 beam_->suicide ();
217 delete beam_info_;
221 void
222 Beam_engraver::acknowledge_grob (Grob_info info)
224 if (beam_)
226 if (Rest::has_interface (info.grob_))
228 info.grob_->add_offset_callback (Beam::rest_collision_callback_proc, Y_AXIS);
230 else if (Stem::has_interface (info.grob_))
232 Moment now = now_mom ();
234 if (!valid_start_point ())
235 return ;
237 Item *stem = dynamic_cast<Item*> (info.grob_);
238 if (Stem::get_beam (stem))
239 return;
241 Music* m = info.music_cause ();
242 if (!m->is_mus_type ("rhythmic-event"))
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);
250 return;
254 last_stem_added_at_ = now;
255 int durlog = unsmob_duration (m->get_property ("duration"))-> duration_log ();
256 if (durlog <= 2)
258 m->origin ()->warning (_ ("stem doesn't fit in beam"));
259 prev_start_ev_->origin ()->warning (_ ("beam was started here"));
261 don't return, since
263 [r4 c8] can just as well be modern notation.
267 stem->set_property ("duration-log",
268 scm_int2num (durlog));
269 Moment stem_location = now - beam_start_mom_ + beam_start_location_;
270 beam_info_->add_stem (stem_location,
271 (durlog- 2) >? 0);
272 Beam::add_stem (beam_, stem);
281 ENTER_DESCRIPTION (Beam_engraver,
282 /* descr */ "Handles Beam events by engraving Beams. If omitted, then notes will be "
283 "printed with flags instead of beams.",
284 /* creats*/ "Beam",
285 /* accepts */ "beam-event",
286 /* acks */ "stem-interface rest-interface",
287 /* reads */ "beamMelismaBusy beatLength subdivideBeams",
288 /* write */ "");
291 class Grace_beam_engraver : public Beam_engraver
293 public:
294 TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
296 protected:
297 virtual bool valid_start_point ();
298 virtual bool valid_end_point ();
301 Grace_beam_engraver::Grace_beam_engraver ()
305 bool
306 Grace_beam_engraver::valid_start_point ()
308 Moment n = now_mom ();
310 return n.grace_part_ != Rational (0);
314 bool
315 Grace_beam_engraver::valid_end_point ()
317 return beam_ && valid_start_point ();
322 ENTER_DESCRIPTION (Grace_beam_engraver,
323 /* descr */ "Handles Beam events by engraving Beams. If omitted, then notes will "
324 "be printed with flags instead of beams. Only engraves beams when we "
325 " are at grace points in time. "
327 /* creats*/ "Beam",
328 /* accepts */ "beam-event",
329 /* acks */ "stem-interface rest-interface",
330 /* reads */ "beamMelismaBusy beatLength allowBeamBreak subdivideBeams",
331 /* write */ "");