2 head-grav.cc -- part of GNU LilyPond
4 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "rhythmic-head.hh"
10 #include "paper-def.hh"
11 #include "musical-request.hh"
13 #include "dot-column.hh"
14 #include "staff-symbol-referencer.hh"
16 #include "score-engraver.hh"
23 When we catch the note, we predict the end of the note. We keep the
24 requests living until we reach the predicted end-time.
26 Every time process_music() is called and there are note requests, we
27 figure out how long the note to typeset should be. It should be no
28 longer than what's specified, than what is left to do and it should
31 We copy the reqs into scratch note reqs, to make sure that we get
32 all durations exactly right.
35 class Completion_heads_engraver : public Engraver
37 Link_array<Item> note_p_arr_;
39 Link_array<Item> dot_p_arr_;
40 Link_array<Music> note_req_l_arr_;
41 Link_array<Music> scratch_note_reqs_;
47 Moment next_barline_moment ();
48 Duration find_nearest_duration (Rational length);
51 TRANSLATOR_DECLARATIONS(Completion_heads_engraver);
54 virtual void initialize ();
55 virtual void start_translation_timestep ();
56 virtual bool try_music (Music *req_l) ;
57 virtual void process_music ();
58 virtual void stop_translation_timestep ();
62 Completion_heads_engraver::initialize ()
68 Completion_heads_engraver::try_music (Music *m)
70 if (Note_req * n =dynamic_cast <Note_req *> (m))
72 note_req_l_arr_.push (n);
75 Moment musiclen = m->length_mom ();
76 Moment now = now_mom();
78 if (now_mom ().grace_part_)
80 musiclen.grace_part_ = musiclen.main_part_ ;
81 musiclen.main_part_ = Rational (0,1);
83 note_end_mom_ = note_end_mom_ >? (now + musiclen);
86 else if (dynamic_cast<Busy_playing_req*> (m))
88 return note_req_l_arr_.size ();
96 Completion_heads_engraver::next_barline_moment ( )
98 Moment *e = unsmob_moment (get_property ("measurePosition"));
99 Moment *l = unsmob_moment (get_property ("measureLength"));
102 programming_error ("No timing props set?");
110 Completion_heads_engraver::find_nearest_duration (Rational length)
117 this could surely be done more efficient. Left to the reader as an
119 while (d.length_mom () > length && d.duration_log () < log_limit)
123 d = Duration (d.duration_log (), d.dot_count ()- 1);
128 d = Duration (d.duration_log () + 1, 2);
132 if (d.duration_log () >= log_limit)
135 d = Duration (d.duration_log (), 0);
138 d = d.compressed (length / d.length_mom ());
145 Completion_heads_engraver::process_music ()
147 if (!first_b_ && !left_to_do_)
156 note_dur = find_nearest_duration (left_to_do_);
160 orig = unsmob_duration (note_req_l_arr_[0]->get_mus_property ("duration"));
164 Moment nb = next_barline_moment ();
165 if (nb < note_dur.length_mom ())
167 note_dur = find_nearest_duration (nb.main_part_);
169 Moment next = now_mom();
170 next.main_part_ += note_dur.length_mom ();
171 top_engraver ()->add_moment_to_process (next);
176 left_to_do_ = orig->length_mom ();
179 if (orig && note_dur.length_mom() != orig->length_mom())
181 if (!scratch_note_reqs_.size ())
182 for (int i = 0; i < note_req_l_arr_.size (); i++)
184 Music * m = note_req_l_arr_[i]->clone ();
185 scratch_note_reqs_.push (m);
191 left_to_do_ && i < note_req_l_arr_.size (); i++)
193 Item *note_p = new Item (get_property ("NoteHead"));
195 Music * req = note_req_l_arr_[i];
196 if (scratch_note_reqs_.size())
198 req = scratch_note_reqs_[i];
199 SCM pits = note_req_l_arr_[i]->get_mus_property ("pitch");
200 req->set_mus_property ("pitch",pits);
203 req->set_mus_property ("duration", note_dur.smobbed_copy ());
204 note_p->set_grob_property ("duration-log",
205 gh_int2scm (note_dur.duration_log ()));
207 int dots= note_dur.dot_count ();
210 Item * d = new Item (get_property ("Dots"));
211 Rhythmic_head::set_dots (note_p, d);
214 measly attempt to save an eeny-weenie bit of memory.
216 if (dots != gh_scm2int (d->get_grob_property ("dot-count")))
217 d->set_grob_property ("dot-count", gh_int2scm (dots));
219 d->set_parent (note_p, Y_AXIS);
220 announce_grob (d, SCM_EOL);
224 Pitch *pit =unsmob_pitch (req->get_mus_property ("pitch"));
226 int pos = pit->steps ();
227 SCM c0 = get_property ("centralCPosition");
228 if (gh_number_p (c0))
229 pos += gh_scm2int (c0);
231 note_p->set_grob_property ("staff-position", gh_int2scm (pos));
232 announce_grob (note_p,req->self_scm ());
233 note_p_arr_.push (note_p);
236 left_to_do_ -= note_dur.length_mom ();
240 don't do complicated arithmetic with grace notes.
243 && now_mom().grace_part_ )
245 left_to_do_ = Rational (0,0);
251 Completion_heads_engraver::stop_translation_timestep ()
253 for (int i=0; i < note_p_arr_.size (); i++)
255 typeset_grob (note_p_arr_[i]);
257 note_p_arr_.clear ();
259 for (int i=0; i < dot_p_arr_.size (); i++)
261 typeset_grob (dot_p_arr_[i]);
265 for (int i = scratch_note_reqs_.size(); i--;)
267 scm_gc_unprotect_object (scratch_note_reqs_[i]->self_scm () );
270 scratch_note_reqs_.clear();
273 Tie_req * tie_req = 0;
276 Completion_heads_engraver::start_translation_timestep ()
278 Moment now = now_mom ();
279 if (note_end_mom_.main_part_ <= now.main_part_)
281 note_req_l_arr_.clear ();
287 tie_req = new Tie_req;
289 bool succ = daddy_trans_l_->try_music (tie_req);
292 programming_error ("Completion_heads_engraver: no-one to make tie.");
297 Completion_heads_engraver::Completion_heads_engraver()
301 ENTER_DESCRIPTION(Completion_heads_engraver,
302 /* descr */ "This engraver replaces
303 @code{Note_heads_engraver}. It plays some trickery to
304 break long notes and automatically tie them into the next measure.",
305 /* creats*/ "NoteHead Dots",
307 /* reads */ "centralCPosition measurePosition measureLength",