2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2015 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"
33 #include "unpure-pure-container.hh"
36 #include "translator.icc"
38 class Beam_engraver : public Engraver
41 DECLARE_ACKNOWLEDGER (stem);
42 DECLARE_ACKNOWLEDGER (rest);
45 Stream_event *start_ev_;
47 Spanner *finished_beam_;
49 Stream_event *prev_start_ev_;
51 Stream_event *stop_ev_;
53 Direction forced_direction_;
55 Beaming_pattern *beam_info_;
56 Beaming_pattern *finished_beam_info_;
58 /// location within measure where beam started.
59 Moment beam_start_location_;
61 /// moment (global time) where beam started.
62 Moment beam_start_mom_;
64 Beaming_options beaming_options_;
65 Beaming_options finished_beaming_options_;
68 void set_melisma (bool);
70 Moment last_stem_added_at_;
71 void stop_translation_timestep ();
72 void start_translation_timestep ();
73 virtual void finalize ();
75 void process_music ();
77 virtual bool valid_start_point ();
78 virtual bool valid_end_point ();
80 DECLARE_TRANSLATOR_LISTENER (beam);
82 TRANSLATOR_DECLARATIONS (Beam_engraver);
86 Hmm. this isn't necessary, since grace beams and normal beams are
90 Beam_engraver::valid_start_point ()
92 Moment n = now_mom ();
94 return n.grace_part_ == Rational (0);
98 Beam_engraver::valid_end_point ()
100 return valid_start_point ();
103 Beam_engraver::Beam_engraver ()
107 finished_beam_info_ = 0;
109 forced_direction_ = CENTER;
115 IMPLEMENT_TRANSLATOR_LISTENER (Beam_engraver, beam);
117 Beam_engraver::listen_beam (Stream_event *ev)
119 Direction d = to_dir (ev->get_property ("span-direction"));
121 if (d == START && valid_start_point ())
123 ASSIGN_EVENT_ONCE (start_ev_, ev);
125 Direction updown = to_dir (ev->get_property ("direction"));
127 forced_direction_ = updown;
129 else if (d == STOP && valid_end_point ())
130 ASSIGN_EVENT_ONCE (stop_ev_, ev);
134 Beam_engraver::set_melisma (bool ml)
136 SCM b = get_property ("autoBeaming");
138 context ()->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T : SCM_BOOL_F);
142 Beam_engraver::process_music ()
148 start_ev_->origin ()->warning (_ ("already have a beam"));
153 prev_start_ev_ = start_ev_;
154 beam_ = make_spanner ("Beam", start_ev_->self_scm ());
156 Moment mp (robust_scm2moment (get_property ("measurePosition"),
159 beam_start_location_ = mp;
160 beam_start_mom_ = now_mom ();
162 beaming_options_.from_context (context ());
163 beam_info_ = new Beaming_pattern;
164 /* urg, must copy to Auto_beam_engraver too */
168 if (stop_ev_ && beam_)
170 announce_end_grob (beam_, stop_ev_->self_scm ());
176 Beam_engraver::typeset_beam ()
180 Grob *stem = finished_beam_->get_bound (RIGHT);
183 stem = finished_beam_->get_bound (LEFT);
185 finished_beam_->set_bound (RIGHT, stem);
188 if (stem && forced_direction_)
189 set_grob_direction (stem, forced_direction_);
191 forced_direction_ = CENTER;
192 finished_beam_info_->beamify (finished_beaming_options_);
194 Beam::set_beaming (finished_beam_, finished_beam_info_);
196 delete finished_beam_info_;
197 finished_beam_info_ = 0;
204 Beam_engraver::start_translation_timestep ()
213 Beam_engraver::stop_translation_timestep ()
217 finished_beam_ = beam_;
218 finished_beam_info_ = beam_info_;
219 finished_beaming_options_ = beaming_options_;
230 Beam_engraver::finalize ()
235 prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
238 we don't typeset it, (we used to, but it was commented
239 out. Reason unknown) */
246 Beam_engraver::acknowledge_rest (Grob_info info)
249 && !scm_is_number (info.grob ()->get_property_data ("staff-position")))
250 chain_offset_callback (info.grob (),
251 Unpure_pure_container::make_smob
252 (Beam::rest_collision_callback_proc,
253 Beam::pure_rest_collision_callback_proc),
258 Beam_engraver::acknowledge_stem (Grob_info info)
263 Moment now = now_mom ();
264 if (!valid_start_point ())
267 // It's suboptimal that we don't support callbacks returning ##f,
268 // but this makes beams have no effect on "stems" reliably in
269 // TabStaff when \tabFullNotation is switched off: the real stencil
270 // callback for beams is called quite late in the process, and we
271 // don't want to trigger it early.
272 if (scm_is_false (beam_->get_property_data ("stencil")))
275 Item *stem = dynamic_cast<Item *> (info.grob ());
276 if (Stem::get_beam (stem))
279 Stream_event *ev = info.ultimate_event_cause ();
280 if (!ev->in_event_class ("rhythmic-event"))
282 info.grob ()->warning (_ ("stem must have Rhythmic structure"));
286 last_stem_added_at_ = now;
288 Duration *stem_duration = unsmob<Duration> (ev->get_property ("duration"));
289 int durlog = stem_duration->duration_log ();
290 //int durlog = unsmob<Duration> (ev->get_property ("duration"))->duration_log ();
293 ev->origin ()->warning (_ ("stem does not fit in beam"));
294 prev_start_ev_->origin ()->warning (_ ("beam was started here"));
298 [r4 c8] can just as well be modern notation.
302 if (forced_direction_)
303 set_grob_direction (stem, forced_direction_);
305 stem->set_property ("duration-log", scm_from_int (durlog));
306 Moment stem_location = now - beam_start_mom_ + beam_start_location_;
307 beam_info_->add_stem (stem_location,
309 Stem::is_invisible (stem),
310 stem_duration->factor (),
311 (to_boolean (stem->get_property ("tuplet-start"))));
312 Beam::add_stem (beam_, stem);
315 ADD_ACKNOWLEDGER (Beam_engraver, stem);
316 ADD_ACKNOWLEDGER (Beam_engraver, rest);
318 ADD_TRANSLATOR (Beam_engraver,
320 "Handle @code{Beam} events by engraving beams. If omitted,"
321 " then notes are printed with flags instead of beams.",
336 class Grace_beam_engraver : public Beam_engraver
339 TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
341 DECLARE_TRANSLATOR_LISTENER (beam);
344 virtual bool valid_start_point ();
345 virtual bool valid_end_point ();
348 Grace_beam_engraver::Grace_beam_engraver ()
353 Grace_beam_engraver::valid_start_point ()
355 Moment n = now_mom ();
357 return n.grace_part_ != Rational (0);
361 Grace_beam_engraver::valid_end_point ()
363 return beam_ && valid_start_point ();
369 IMPLEMENT_TRANSLATOR_LISTENER (Grace_beam_engraver, beam);
371 Grace_beam_engraver::listen_beam (Stream_event *ev)
373 Direction d = to_dir (ev->get_property ("span-direction"));
375 if (d == START && valid_start_point ())
377 else if (d == STOP && valid_end_point ())
381 ADD_ACKNOWLEDGER (Grace_beam_engraver, stem);
382 ADD_ACKNOWLEDGER (Grace_beam_engraver, rest);
384 ADD_TRANSLATOR (Grace_beam_engraver,
386 "Handle @code{Beam} events by engraving beams. If omitted,"
387 " then notes are printed with flags instead of beams. Only"
388 " engraves beams when we are at grace points in time.",