2 completion-note-heads-engraver.cc -- Completion_heads_engraver
4 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "rhythmic-head.hh"
10 #include "output-def.hh"
13 #include "dot-column.hh"
14 #include "staff-symbol-referencer.hh"
16 #include "score-engraver.hh"
20 #include "global-context.hh"
21 #include "duration.hh"
26 TODO: make matching rest engraver.
32 When we catch the note, we predict the end of the note. We keep the
33 events living until we reach the predicted end-time.
35 Every time process_music () is called and there are note events, we
36 figure out how long the note to typeset should be. It should be no
37 longer than what's specified, than what is left to do and it should
40 We copy the events into scratch note events, to make sure that we get
41 all durations exactly right.
44 class Completion_heads_engraver : public Engraver
46 Link_array<Item> notes_;
47 Link_array<Item> prev_notes_;
48 Link_array<Grob> ties_;
50 Link_array<Item> dots_;
51 Link_array<Music> note_events_;
52 Link_array<Music> scratch_note_events_;
57 Rational do_nothing_until_;
59 Moment next_barline_moment ();
60 Duration find_nearest_duration (Rational length);
63 TRANSLATOR_DECLARATIONS (Completion_heads_engraver);
66 virtual void initialize ();
67 void start_translation_timestep ();
68 virtual bool try_music (Music *event);
69 void process_music ();
70 void stop_translation_timestep ();
74 Completion_heads_engraver::initialize ()
80 Completion_heads_engraver::try_music (Music *m)
82 if (m->is_mus_type ("note-event"))
84 note_events_.push (m);
87 Moment musiclen = m->get_length ();
88 Moment now = now_mom ();
90 if (now_mom ().grace_part_)
92 musiclen.grace_part_ = musiclen.main_part_;
93 musiclen.main_part_ = Rational (0, 1);
95 note_end_mom_ = max (note_end_mom_, (now + musiclen));
96 do_nothing_until_ = Rational (0, 0);
100 else if (m->is_mus_type ("busy-playing-event"))
102 return note_events_.size () && is_first_;
109 The duration _until_ the next barline.
112 Completion_heads_engraver::next_barline_moment ()
114 Moment *e = unsmob_moment (get_property ("measurePosition"));
115 Moment *l = unsmob_moment (get_property ("measureLength"));
118 programming_error ("no timing props set?");
119 return Moment (1, 1);
126 Completion_heads_engraver::find_nearest_duration (Rational length)
133 this could surely be done more efficient. Left to the reader as an
135 while (d.get_length () > length && d.duration_log () < log_limit)
139 d = Duration (d.duration_log (), d.dot_count ()- 1);
143 d = Duration (d.duration_log () + 1, 2);
146 if (d.duration_log () >= log_limit)
149 d = Duration (d.duration_log (), 0);
152 d = d.compressed (length / d.get_length ());
159 Completion_heads_engraver::process_music ()
161 if (!is_first_ && !left_to_do_)
166 Moment now = now_mom ();
167 if (do_nothing_until_ > now.main_part_)
174 note_dur = find_nearest_duration (left_to_do_);
178 orig = unsmob_duration (note_events_[0]->get_property ("duration"));
181 Moment nb = next_barline_moment ();
182 if (nb < note_dur.get_length ())
184 note_dur = find_nearest_duration (nb.main_part_);
187 next.main_part_ += note_dur.get_length ();
189 get_global_context ()->add_moment_to_process (next);
190 do_nothing_until_ = next.main_part_;
195 left_to_do_ = orig->get_length ();
198 if (orig && note_dur.get_length () != orig->get_length ())
200 if (!scratch_note_events_.size ())
201 for (int i = 0; i < note_events_.size (); i++)
203 Music *m = note_events_[i]->clone ();
204 scratch_note_events_.push (m);
209 left_to_do_ && i < note_events_.size (); i++)
211 Music *event = note_events_[i];
212 if (scratch_note_events_.size ())
214 event = scratch_note_events_[i];
215 SCM pits = note_events_[i]->get_property ("pitch");
216 event->set_property ("pitch", pits);
219 event->set_property ("duration", note_dur.smobbed_copy ());
221 Item *note = make_item ("NoteHead", event->self_scm ());
222 note->set_property ("duration-log",
223 scm_from_int (note_dur.duration_log ()));
225 int dots = note_dur.dot_count ();
228 Item *d = make_item ("Dots", SCM_EOL);
229 Rhythmic_head::set_dots (note, d);
232 measly attempt to save an eeny-weenie bit of memory.
234 if (dots != scm_to_int (d->get_property ("dot-count")))
235 d->set_property ("dot-count", scm_from_int (dots));
237 d->set_parent (note, Y_AXIS);
241 Pitch *pit = unsmob_pitch (event->get_property ("pitch"));
243 int pos = pit->steps ();
244 SCM c0 = get_property ("middleCPosition");
245 if (scm_is_number (c0))
246 pos += scm_to_int (c0);
248 note->set_property ("staff-position", scm_from_int (pos));
252 if (prev_notes_.size () == notes_.size ())
254 for (int i = 0; i < notes_.size (); i++)
256 Grob *p = make_spanner ("Tie", SCM_EOL);
257 Tie::set_interface (p); // cannot remove yet!
259 Tie::set_head (p, LEFT, prev_notes_[i]);
260 Tie::set_head (p, RIGHT, notes_[i]);
266 left_to_do_ -= note_dur.get_length ();
269 don't do complicated arithmetic with grace notes.
272 && now_mom ().grace_part_)
274 left_to_do_ = Rational (0, 0);
279 Completion_heads_engraver::stop_translation_timestep ()
284 prev_notes_ = notes_;
289 for (int i = scratch_note_events_.size (); i--;)
291 scratch_note_events_[i]->unprotect ();
294 scratch_note_events_.clear ();
298 Completion_heads_engraver::start_translation_timestep ()
300 Moment now = now_mom ();
301 if (note_end_mom_.main_part_ <= now.main_part_)
303 note_events_.clear ();
304 prev_notes_.clear ();
308 Completion_heads_engraver::Completion_heads_engraver ()
312 #include "translator.icc"
314 ADD_TRANSLATOR (Completion_heads_engraver,
315 /* doc */ "This engraver replaces "
316 "@code{Note_heads_engraver}. It plays some trickery to "
317 "break long notes and automatically tie them into the next measure.",
318 /* create */ "NoteHead Dots Tie",
319 /* accept */ "busy-playing-event note-event",
320 /* read */ "middleCPosition measurePosition measureLength",