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 void acknowledge_stem (Grob_info);
42 void acknowledge_rest (Grob_info);
43 void listen_beam (Stream_event *);
46 Stream_event *start_ev_;
48 Spanner *finished_beam_;
50 Stream_event *prev_start_ev_;
52 Stream_event *stop_ev_;
54 Direction forced_direction_;
56 Beaming_pattern *beam_info_;
57 Beaming_pattern *finished_beam_info_;
59 /// location within measure where beam started.
60 Moment beam_start_location_;
62 /// moment (global time) where beam started.
63 Moment beam_start_mom_;
65 Beaming_options beaming_options_;
66 Beaming_options finished_beaming_options_;
69 void set_melisma (bool);
71 Moment last_stem_added_at_;
72 void stop_translation_timestep ();
73 void start_translation_timestep ();
74 virtual void finalize ();
76 void process_music ();
78 virtual bool valid_start_point ();
79 virtual bool valid_end_point ();
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 (Context *c)
108 finished_beam_info_ = 0;
110 forced_direction_ = CENTER;
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);
317 Beam_engraver::boot ()
319 ADD_LISTENER (Beam_engraver, beam);
320 ADD_ACKNOWLEDGER (Beam_engraver, stem);
321 ADD_ACKNOWLEDGER (Beam_engraver, rest);
324 ADD_TRANSLATOR (Beam_engraver,
326 "Handle @code{Beam} events by engraving beams. If omitted,"
327 " then notes are printed with flags instead of beams.",
342 class Grace_beam_engraver : public Beam_engraver
345 TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
346 TRANSLATOR_INHERIT (Beam_engraver);
349 virtual bool valid_start_point ();
350 virtual bool valid_end_point ();
353 Grace_beam_engraver::Grace_beam_engraver (Context *c)
359 Grace_beam_engraver::valid_start_point ()
361 Moment n = now_mom ();
363 return n.grace_part_ != Rational (0);
367 Grace_beam_engraver::valid_end_point ()
369 return beam_ && valid_start_point ();
373 Grace_beam_engraver::boot ()
375 ADD_LISTENER (Grace_beam_engraver, beam);
376 ADD_ACKNOWLEDGER (Grace_beam_engraver, stem);
377 ADD_ACKNOWLEDGER (Grace_beam_engraver, rest);
380 ADD_TRANSLATOR (Grace_beam_engraver,
382 "Handle @code{Beam} events by engraving beams. If omitted,"
383 " then notes are printed with flags instead of beams. Only"
384 " engraves beams when we are at grace points in time.",