2 head-grav.cc -- part of GNU LilyPond
4 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "rhythmic-head.hh"
10 #include "paper-def.hh"
13 #include "dot-column.hh"
14 #include "staff-symbol-referencer.hh"
16 #include "score-engraver.hh"
20 #include "global-context.hh"
26 When we catch the note, we predict the end of the note. We keep the
27 events living until we reach the predicted end-time.
29 Every time process_music() is called and there are note events, we
30 figure out how long the note to typeset should be. It should be no
31 longer than what's specified, than what is left to do and it should
34 We copy the reqs into scratch note reqs, to make sure that we get
35 all durations exactly right.
38 class Completion_heads_engraver : public Engraver
40 Link_array<Item> notes_;
41 Link_array<Item> prev_notes_;
42 Link_array<Grob> ties_;
44 Link_array<Item> dots_;
45 Link_array<Music> note_reqs_;
46 Link_array<Music> scratch_note_reqs_;
51 Rational do_nothing_until_;
53 Moment next_barline_moment ();
54 Duration find_nearest_duration (Rational length);
57 TRANSLATOR_DECLARATIONS(Completion_heads_engraver);
60 virtual void initialize ();
61 virtual void start_translation_timestep ();
62 virtual bool try_music (Music *req) ;
63 virtual void process_music ();
64 virtual void stop_translation_timestep ();
68 Completion_heads_engraver::initialize ()
74 Completion_heads_engraver::try_music (Music *m)
76 if (m->is_mus_type ("note-event"))
81 Moment musiclen = m->get_length ();
82 Moment now = now_mom();
84 if (now_mom ().grace_part_)
86 musiclen.grace_part_ = musiclen.main_part_ ;
87 musiclen.main_part_ = Rational (0,1);
89 note_end_mom_ = note_end_mom_ >? (now + musiclen);
90 do_nothing_until_ = Rational (0,0);
94 else if (m->is_mus_type ("busy-playing-event"))
96 return note_reqs_.size ();
104 The duration _until_ the next barline.
107 Completion_heads_engraver::next_barline_moment ( )
109 Moment *e = unsmob_moment (get_property ("measurePosition"));
110 Moment *l = unsmob_moment (get_property ("measureLength"));
113 programming_error ("No timing props set?");
121 Completion_heads_engraver::find_nearest_duration (Rational length)
128 this could surely be done more efficient. Left to the reader as an
130 while (d.get_length () > length && d.duration_log () < log_limit)
134 d = Duration (d.duration_log (), d.dot_count ()- 1);
139 d = Duration (d.duration_log () + 1, 2);
143 if (d.duration_log () >= log_limit)
146 d = Duration (d.duration_log (), 0);
149 d = d.compressed (length / d.get_length ());
156 Completion_heads_engraver::process_music ()
158 if (!first_b_ && !left_to_do_)
163 Moment now = now_mom ();
164 if (do_nothing_until_ > now.main_part_)
171 note_dur = find_nearest_duration (left_to_do_);
175 orig = unsmob_duration (note_reqs_[0]->get_mus_property ("duration"));
178 Moment nb = next_barline_moment ();
179 if (nb < note_dur.get_length ())
181 note_dur = find_nearest_duration (nb.main_part_);
184 next.main_part_ += note_dur.get_length ();
186 get_global_context ()->add_moment_to_process (next);
187 do_nothing_until_ = next.main_part_;
192 left_to_do_ = orig->get_length ();
195 if (orig && note_dur.get_length () != orig->get_length ())
197 if (!scratch_note_reqs_.size ())
198 for (int i = 0; i < note_reqs_.size (); i++)
200 Music * m = note_reqs_[i]->clone ();
201 scratch_note_reqs_.push (m);
207 left_to_do_ && i < note_reqs_.size (); i++)
209 Item *note = make_item ("NoteHead");
211 Music * req = note_reqs_[i];
212 if (scratch_note_reqs_.size())
214 req = scratch_note_reqs_[i];
215 SCM pits = note_reqs_[i]->get_mus_property ("pitch");
216 req->set_mus_property ("pitch",pits);
219 req->set_mus_property ("duration", note_dur.smobbed_copy ());
220 note->set_grob_property ("duration-log",
221 gh_int2scm (note_dur.duration_log ()));
223 int dots= note_dur.dot_count ();
226 Item * d = make_item ("Dots");
227 Rhythmic_head::set_dots (note, d);
230 measly attempt to save an eeny-weenie bit of memory.
232 if (dots != gh_scm2int (d->get_grob_property ("dot-count")))
233 d->set_grob_property ("dot-count", gh_int2scm (dots));
235 d->set_parent (note, Y_AXIS);
236 announce_grob (d, SCM_EOL);
240 Pitch *pit =unsmob_pitch (req->get_mus_property ("pitch"));
242 int pos = pit->steps ();
243 SCM c0 = get_property ("centralCPosition");
244 if (gh_number_p (c0))
245 pos += gh_scm2int (c0);
247 note->set_grob_property ("staff-position", gh_int2scm (pos));
248 announce_grob (note,req->self_scm ());
252 if (prev_notes_.size() == notes_.size ())
254 for (int i= 0; i < notes_.size(); i++)
256 Grob * p = make_spanner ("Tie");
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]);
263 announce_grob(p, SCM_EOL);
267 left_to_do_ -= note_dur.get_length ();
270 don't do complicated arithmetic with grace notes.
273 && now_mom().grace_part_ )
275 left_to_do_ = Rational (0,0);
280 Completion_heads_engraver::stop_translation_timestep ()
282 for (int i = ties_.size (); i--;)
283 typeset_grob (ties_[i]);
286 for (int i=0; i < notes_.size (); i++)
288 typeset_grob (notes_[i]);
291 prev_notes_ = notes_;
294 for (int i=0; i < dots_.size (); i++)
296 typeset_grob (dots_[i]);
300 for (int i = scratch_note_reqs_.size(); i--;)
302 scm_gc_unprotect_object (scratch_note_reqs_[i]->self_scm () );
305 scratch_note_reqs_.clear();
309 Completion_heads_engraver::start_translation_timestep ()
311 Moment now = now_mom ();
312 if (note_end_mom_.main_part_ <= now.main_part_)
315 prev_notes_.clear ();
319 Completion_heads_engraver::Completion_heads_engraver()
323 ENTER_DESCRIPTION(Completion_heads_engraver,
324 /* descr */ "This engraver replaces "
325 "@code{Note_heads_engraver}. It plays some trickery to "
326 "break long notes and automatically tie them into the next measure.",
327 /* creats*/ "NoteHead Dots Tie",
328 /* accepts */ "busy-playing-event note-event",
330 /* reads */ "centralCPosition measurePosition measureLength",