2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "beaming-pattern.hh"
23 #include "directional-element-interface.hh"
24 #include "drul-array.hh"
25 #include "duration.hh"
26 #include "engraver.hh"
27 #include "international.hh"
31 #include "stream-event.hh"
35 #include "translator.icc"
37 class Beam_engraver : public Engraver
40 DECLARE_ACKNOWLEDGER (stem);
41 DECLARE_ACKNOWLEDGER (rest);
44 Stream_event *start_ev_;
46 Spanner *finished_beam_;
48 Stream_event *prev_start_ev_;
50 Stream_event *stop_ev_;
52 Direction forced_direction_;
54 Beaming_pattern *beam_info_;
55 Beaming_pattern *finished_beam_info_;
57 /// location within measure where beam started.
58 Moment beam_start_location_;
60 /// moment (global time) where beam started.
61 Moment beam_start_mom_;
63 Beaming_options beaming_options_;
64 Beaming_options finished_beaming_options_;
67 void set_melisma (bool);
69 Moment last_stem_added_at_;
70 void stop_translation_timestep ();
71 void start_translation_timestep ();
72 virtual void finalize ();
74 void process_music ();
76 virtual bool valid_start_point ();
77 virtual bool valid_end_point ();
79 DECLARE_TRANSLATOR_LISTENER (beam);
81 TRANSLATOR_DECLARATIONS (Beam_engraver);
85 Hmm. this isn't necessary, since grace beams and normal beams are
89 Beam_engraver::valid_start_point ()
91 Moment n = now_mom ();
93 return n.grace_part_ == Rational (0);
97 Beam_engraver::valid_end_point ()
99 return valid_start_point ();
102 Beam_engraver::Beam_engraver ()
106 finished_beam_info_ = 0;
108 forced_direction_ = CENTER;
114 IMPLEMENT_TRANSLATOR_LISTENER (Beam_engraver, beam);
116 Beam_engraver::listen_beam (Stream_event *ev)
118 Direction d = to_dir (ev->get_property ("span-direction"));
120 if (d == START && valid_start_point ())
122 ASSIGN_EVENT_ONCE (start_ev_, ev);
124 Direction updown = to_dir (ev->get_property ("direction"));
126 forced_direction_ = updown;
128 else if (d == STOP && valid_end_point ())
129 ASSIGN_EVENT_ONCE (stop_ev_, ev);
133 Beam_engraver::set_melisma (bool ml)
135 SCM b = get_property ("autoBeaming");
137 context ()->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T : SCM_BOOL_F);
141 Beam_engraver::process_music ()
147 start_ev_->origin ()->warning (_ ("already have a beam"));
152 prev_start_ev_ = start_ev_;
153 beam_ = make_spanner ("Beam", start_ev_->self_scm ());
155 Moment mp (robust_scm2moment (get_property ("measurePosition"),
158 beam_start_location_ = mp;
159 beam_start_mom_ = now_mom ();
161 beaming_options_.from_context (context ());
162 beam_info_ = new Beaming_pattern;
163 /* urg, must copy to Auto_beam_engraver too */
167 if (stop_ev_ && beam_)
169 announce_end_grob (beam_, stop_ev_->self_scm ());
175 Beam_engraver::typeset_beam ()
179 if (!finished_beam_->get_bound (RIGHT))
180 finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
181 if (forced_direction_)
183 Grob *stem = finished_beam_->get_bound (RIGHT);
184 set_grob_direction (stem, forced_direction_);
185 forced_direction_ = CENTER;
187 finished_beam_info_->beamify (finished_beaming_options_);
189 Beam::set_beaming (finished_beam_, finished_beam_info_);
191 delete finished_beam_info_;
192 finished_beam_info_ = 0;
199 Beam_engraver::start_translation_timestep ()
208 Beam_engraver::stop_translation_timestep ()
212 finished_beam_ = beam_;
213 finished_beam_info_ = beam_info_;
214 finished_beaming_options_ = beaming_options_;
225 Beam_engraver::finalize ()
230 prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
233 we don't typeset it, (we used to, but it was commented
234 out. Reason unknown) */
241 Beam_engraver::acknowledge_rest (Grob_info info)
244 && !scm_is_number (info.grob ()->get_property_data ("staff-position")))
245 chain_offset_callback (info.grob (),
246 Beam::rest_collision_callback_proc, Y_AXIS);
250 Beam_engraver::acknowledge_stem (Grob_info info)
255 Moment now = now_mom ();
256 if (!valid_start_point ())
259 Item *stem = dynamic_cast<Item *> (info.grob ());
260 if (Stem::get_beam (stem))
263 Stream_event *ev = info.ultimate_event_cause ();
264 if (!ev->in_event_class ("rhythmic-event"))
266 info.grob ()->warning (_ ("stem must have Rhythmic structure"));
270 last_stem_added_at_ = now;
271 int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
274 ev->origin ()->warning (_ ("stem does not fit in beam"));
275 prev_start_ev_->origin ()->warning (_ ("beam was started here"));
279 [r4 c8] can just as well be modern notation.
283 if (forced_direction_)
284 set_grob_direction (stem, forced_direction_);
286 stem->set_property ("duration-log", scm_from_int (durlog));
287 Moment stem_location = now - beam_start_mom_ + beam_start_location_;
288 beam_info_->add_stem (stem_location,
290 Stem::is_invisible (stem));
291 Beam::add_stem (beam_, stem);
294 ADD_ACKNOWLEDGER (Beam_engraver, stem);
295 ADD_ACKNOWLEDGER (Beam_engraver, rest);
297 ADD_TRANSLATOR (Beam_engraver,
299 "Handle @code{Beam} events by engraving beams. If omitted,"
300 " then notes are printed with flags instead of beams.",
315 class Grace_beam_engraver : public Beam_engraver
318 TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
320 DECLARE_TRANSLATOR_LISTENER (beam);
323 virtual bool valid_start_point ();
324 virtual bool valid_end_point ();
327 Grace_beam_engraver::Grace_beam_engraver ()
332 Grace_beam_engraver::valid_start_point ()
334 Moment n = now_mom ();
336 return n.grace_part_ != Rational (0);
340 Grace_beam_engraver::valid_end_point ()
342 return beam_ && valid_start_point ();
348 IMPLEMENT_TRANSLATOR_LISTENER (Grace_beam_engraver, beam);
350 Grace_beam_engraver::listen_beam (Stream_event *ev)
352 Direction d = to_dir (ev->get_property ("span-direction"));
354 if (d == START && valid_start_point ())
356 else if (d == STOP && valid_end_point ())
360 ADD_ACKNOWLEDGER (Grace_beam_engraver, stem);
361 ADD_ACKNOWLEDGER (Grace_beam_engraver, rest);
363 ADD_TRANSLATOR (Grace_beam_engraver,
365 "Handle @code{Beam} events by engraving beams. If omitted,"
366 " then notes are printed with flags instead of beams. Only"
367 " engraves beams when we are at grace points in time.",