2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1999--2009 Jan Nieuwenhuizen <janneke@gnu.org>
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/>.
20 #include "bar-line.hh"
21 #include "beaming-pattern.hh"
24 #include "duration.hh"
25 #include "engraver.hh"
29 #include "stream-event.hh"
33 #include "translator.icc"
35 class Auto_beam_engraver : public Engraver
37 TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
40 void stop_translation_timestep ();
41 void process_music ();
42 virtual void finalize ();
43 virtual void derived_mark () const;
45 DECLARE_ACKNOWLEDGER (rest);
46 DECLARE_ACKNOWLEDGER (beam);
47 DECLARE_ACKNOWLEDGER (bar_line);
48 DECLARE_ACKNOWLEDGER (stem);
49 DECLARE_TRANSLATOR_LISTENER (beam_forbid);
51 void process_acknowledged ();
54 bool test_moment (Direction, Moment);
55 void consider_begin (Moment);
56 void consider_end (Moment);
57 Spanner *create_beam ();
61 bool is_same_grace_state (Grob *e);
64 Stream_event *forbid_;
66 shortest_mom is the shortest note in the beam.
69 Spanner *finished_beam_;
70 vector<Item*> *stems_;
72 int process_acknowledged_count_;
75 Projected ending of the beam we're working on.
78 Moment beam_start_moment_;
79 Moment beam_start_location_;
81 // We act as if beam were created, and start a grouping anyway.
82 Beaming_pattern *grouping_;
85 Beaming_pattern *finished_grouping_;
88 Beaming_options beaming_options_;
89 Beaming_options finished_beaming_options_;
92 void check_bar_property ();
96 Auto_beam_engraver::derived_mark () const
98 scm_gc_mark (beam_settings_);
102 Auto_beam_engraver::check_bar_property ()
104 /* Duplicated from process_music (), since
105 Repeat_acknowledge_engraver::process_music () may also set whichBar. */
107 Moment now = now_mom ();
108 if (scm_is_string (get_property ("whichBar"))
109 && beam_start_moment_ < now)
111 consider_end (shortest_mom_);
117 Auto_beam_engraver::process_music ()
120 don't beam over skips
124 Moment now = now_mom ();
125 if (extend_mom_ < now)
129 if (scm_is_string (get_property ("whichBar")))
131 consider_end (shortest_mom_);
137 consider_end (shortest_mom_);
142 Auto_beam_engraver::Auto_beam_engraver ()
145 process_acknowledged_count_ = 0;
147 shortest_mom_ = Moment (Rational (1, 8));
149 finished_grouping_ = 0;
151 beam_settings_ = SCM_EOL;
154 IMPLEMENT_TRANSLATOR_LISTENER (Auto_beam_engraver, beam_forbid);
156 Auto_beam_engraver::listen_beam_forbid (Stream_event *ev)
158 ASSIGN_EVENT_ONCE (forbid_, ev);
162 Auto_beam_engraver::test_moment (Direction dir, Moment test)
164 return scm_call_3 (get_property ("autoBeamCheck"),
165 context ()->self_scm (),
167 test.smobbed_copy ())
172 Auto_beam_engraver::consider_begin (Moment test_mom)
174 bool on = to_boolean (get_property ("autoBeaming"));
178 bool b = test_moment (START, test_mom);
185 Auto_beam_engraver::consider_end (Moment test_mom)
189 /* Allow already started autobeam to end:
190 don't check for autoBeaming */
191 bool b = test_moment (STOP, test_mom);
198 Auto_beam_engraver::create_beam ()
200 if (to_boolean (get_property ("skipTypesetting")))
203 for (vsize i = 0; i < stems_->size (); i++)
204 if (Stem::get_beam ((*stems_)[i]))
208 Can't use make_spanner_from_properties () because we have to use
211 Spanner *beam = new Spanner (beam_settings_);
213 for (vsize i = 0; i < stems_->size (); i++)
214 Beam::add_stem (beam, (*stems_)[i]);
216 announce_grob (beam, (*stems_)[0]->self_scm ());
222 Auto_beam_engraver::begin_beam ()
224 if (stems_ || grouping_)
226 programming_error ("already have autobeam");
230 stems_ = new vector<Item*>;
231 grouping_ = new Beaming_pattern ();
232 beaming_options_.from_context (context ());
233 beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
235 beam_start_moment_ = now_mom ();
237 = robust_scm2moment (get_property ("measurePosition"), Moment (0));
241 Auto_beam_engraver::junk_beam ()
250 beam_settings_ = SCM_EOL;
252 shortest_mom_ = Moment (Rational (1, 8));
256 Auto_beam_engraver::end_beam ()
258 if (stems_->size () < 2)
262 finished_beam_ = create_beam ();
266 announce_end_grob (finished_beam_, SCM_EOL);
267 finished_grouping_ = grouping_;
268 finished_beaming_options_ = beaming_options_;
273 beam_settings_ = SCM_EOL;
276 shortest_mom_ = Moment (Rational (1, 8));
280 Auto_beam_engraver::typeset_beam ()
284 if (!finished_beam_->get_bound (RIGHT))
285 finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
287 finished_grouping_->beamify (finished_beaming_options_);
288 Beam::set_beaming (finished_beam_, finished_grouping_);
291 delete finished_grouping_;
292 finished_grouping_ = 0;
297 Auto_beam_engraver::stop_translation_timestep ()
300 process_acknowledged_count_ = 0;
305 Auto_beam_engraver::finalize ()
307 /* finished beams may be typeset */
309 /* but unfinished may need another announce/acknowledge pass */
316 Auto_beam_engraver::acknowledge_beam (Grob_info /* info */)
318 check_bar_property ();
324 Auto_beam_engraver::acknowledge_bar_line (Grob_info /* info */)
326 check_bar_property ();
332 Auto_beam_engraver::acknowledge_rest (Grob_info /* info */)
334 check_bar_property ();
340 Auto_beam_engraver::acknowledge_stem (Grob_info info)
342 check_bar_property ();
343 Item *stem = dynamic_cast<Item *> (info.grob ());
344 Stream_event *ev = info.ultimate_event_cause ();
345 if (!ev->in_event_class ("rhythmic-event"))
347 programming_error ("stem must have rhythmic structure");
352 Don't (start) auto-beam over empty stems; skips or rests
354 if (!Stem::head_count (stem))
361 if (Stem::get_beam (stem))
368 int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
380 Moment now = now_mom ();
381 if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
384 Moment dur = unsmob_duration (ev->get_property ("duration"))->get_length ();
387 consider_begin (dur);
389 if (dur < shortest_mom_)
395 grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
397 Stem::is_invisible (stem));
398 stems_->push_back (stem);
400 extend_mom_ = max (extend_mom_, now) + get_event_length (ev, now);
404 Auto_beam_engraver::process_acknowledged ()
406 if (extend_mom_ > now_mom ())
409 if (!process_acknowledged_count_)
411 consider_end (shortest_mom_);
412 consider_begin (shortest_mom_);
414 else if (process_acknowledged_count_ > 1)
418 Moment now = now_mom ();
419 if ((extend_mom_ < now)
420 || ((extend_mom_ == now) && (last_add_mom_ != now)))
422 else if (!stems_->size ())
427 process_acknowledged_count_++;
430 ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
431 ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
432 ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
433 ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
434 ADD_TRANSLATOR (Auto_beam_engraver,
436 "Generate beams based on measure characteristics and observed"
437 " Stems. Uses @code{beatLength}, @code{measureLength}, and"
438 " @code{measurePosition} to decide when to start and stop a"
439 " beam. Overriding beaming is done through"
440 " @ref{Stem_engraver} properties @code{stemLeftBeamCount} and"
441 " @code{stemRightBeamCount}.",