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 ()
107 finished_beam_info_ = 0;
109 forced_direction_ = CENTER;
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 Grob *stem = finished_beam_->get_bound (RIGHT);
182 stem = finished_beam_->get_bound (LEFT);
184 finished_beam_->set_bound (RIGHT, stem);
187 if (stem && forced_direction_)
188 set_grob_direction (stem, forced_direction_);
190 forced_direction_ = CENTER;
191 finished_beam_info_->beamify (finished_beaming_options_);
193 Beam::set_beaming (finished_beam_, finished_beam_info_);
195 delete finished_beam_info_;
196 finished_beam_info_ = 0;
203 Beam_engraver::start_translation_timestep ()
212 Beam_engraver::stop_translation_timestep ()
216 finished_beam_ = beam_;
217 finished_beam_info_ = beam_info_;
218 finished_beaming_options_ = beaming_options_;
229 Beam_engraver::finalize ()
234 prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
237 we don't typeset it, (we used to, but it was commented
238 out. Reason unknown) */
245 Beam_engraver::acknowledge_rest (Grob_info info)
248 && !scm_is_number (info.grob ()->get_property_data ("staff-position")))
249 chain_offset_callback (info.grob (),
250 Unpure_pure_container::make_smob
251 (Beam::rest_collision_callback_proc,
252 Beam::pure_rest_collision_callback_proc),
257 Beam_engraver::acknowledge_stem (Grob_info info)
262 Moment now = now_mom ();
263 if (!valid_start_point ())
266 // It's suboptimal that we don't support callbacks returning ##f,
267 // but this makes beams have no effect on "stems" reliably in
268 // TabStaff when \tabFullNotation is switched off: the real stencil
269 // callback for beams is called quite late in the process, and we
270 // don't want to trigger it early.
271 if (scm_is_false (beam_->get_property_data ("stencil")))
274 Item *stem = dynamic_cast<Item *> (info.grob ());
275 if (Stem::get_beam (stem))
278 Stream_event *ev = info.ultimate_event_cause ();
279 if (!ev->in_event_class ("rhythmic-event"))
281 info.grob ()->warning (_ ("stem must have Rhythmic structure"));
285 last_stem_added_at_ = now;
287 Duration *stem_duration = unsmob<Duration> (ev->get_property ("duration"));
288 int durlog = stem_duration->duration_log ();
289 //int durlog = unsmob<Duration> (ev->get_property ("duration"))->duration_log ();
292 ev->origin ()->warning (_ ("stem does not fit in beam"));
293 prev_start_ev_->origin ()->warning (_ ("beam was started here"));
297 [r4 c8] can just as well be modern notation.
301 if (forced_direction_)
302 set_grob_direction (stem, forced_direction_);
304 stem->set_property ("duration-log", scm_from_int (durlog));
305 Moment stem_location = now - beam_start_mom_ + beam_start_location_;
306 beam_info_->add_stem (stem_location,
308 Stem::is_invisible (stem),
309 stem_duration->factor (),
310 (to_boolean (stem->get_property ("tuplet-start"))));
311 Beam::add_stem (beam_, stem);
316 Beam_engraver::boot ()
318 ADD_LISTENER (Beam_engraver, beam);
319 ADD_ACKNOWLEDGER (Beam_engraver, stem);
320 ADD_ACKNOWLEDGER (Beam_engraver, rest);
323 ADD_TRANSLATOR (Beam_engraver,
325 "Handle @code{Beam} events by engraving beams. If omitted,"
326 " then notes are printed with flags instead of beams.",
341 class Grace_beam_engraver : public Beam_engraver
344 TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
345 TRANSLATOR_INHERIT (Beam_engraver);
348 virtual bool valid_start_point ();
349 virtual bool valid_end_point ();
352 Grace_beam_engraver::Grace_beam_engraver ()
357 Grace_beam_engraver::valid_start_point ()
359 Moment n = now_mom ();
361 return n.grace_part_ != Rational (0);
365 Grace_beam_engraver::valid_end_point ()
367 return beam_ && valid_start_point ();
371 Grace_beam_engraver::boot ()
373 ADD_LISTENER (Beam_engraver, beam);
374 ADD_ACKNOWLEDGER (Beam_engraver, stem);
375 ADD_ACKNOWLEDGER (Beam_engraver, rest);
378 ADD_TRANSLATOR (Grace_beam_engraver,
380 "Handle @code{Beam} events by engraving beams. If omitted,"
381 " then notes are printed with flags instead of beams. Only"
382 " engraves beams when we are at grace points in time.",