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
> notes_
;
39 Link_array
<Item
> dots_
;
40 Link_array
<Music
> note_reqs_
;
41 Link_array
<Music
> scratch_note_reqs_
;
46 Rational do_nothing_until_
;
48 Moment
next_barline_moment ();
49 Duration
find_nearest_duration (Rational length
);
52 TRANSLATOR_DECLARATIONS(Completion_heads_engraver
);
55 virtual void initialize ();
56 virtual void start_translation_timestep ();
57 virtual bool try_music (Music
*req
) ;
58 virtual void process_music ();
59 virtual void stop_translation_timestep ();
63 Completion_heads_engraver::initialize ()
69 Completion_heads_engraver::try_music (Music
*m
)
71 if (Note_req
* n
=dynamic_cast <Note_req
*> (m
))
76 Moment musiclen
= m
->length_mom ();
77 Moment now
= now_mom();
79 if (now_mom ().grace_part_
)
81 musiclen
.grace_part_
= musiclen
.main_part_
;
82 musiclen
.main_part_
= Rational (0,1);
84 note_end_mom_
= note_end_mom_
>? (now
+ musiclen
);
85 do_nothing_until_
= Rational (0,0);
89 else if (dynamic_cast<Busy_playing_req
*> (m
))
91 return note_reqs_
.size ();
99 The duration _until_ the next barline.
102 Completion_heads_engraver::next_barline_moment ( )
104 Moment
*e
= unsmob_moment (get_property ("measurePosition"));
105 Moment
*l
= unsmob_moment (get_property ("measureLength"));
108 programming_error ("No timing props set?");
116 Completion_heads_engraver::find_nearest_duration (Rational length
)
123 this could surely be done more efficient. Left to the reader as an
125 while (d
.length_mom () > length
&& d
.duration_log () < log_limit
)
129 d
= Duration (d
.duration_log (), d
.dot_count ()- 1);
134 d
= Duration (d
.duration_log () + 1, 2);
138 if (d
.duration_log () >= log_limit
)
141 d
= Duration (d
.duration_log (), 0);
144 d
= d
.compressed (length
/ d
.length_mom ());
151 Completion_heads_engraver::process_music ()
153 if (!first_b_
&& !left_to_do_
)
158 Moment now
= now_mom ();
159 if (do_nothing_until_
> now
.main_part_
)
166 note_dur
= find_nearest_duration (left_to_do_
);
170 orig
= unsmob_duration (note_reqs_
[0]->get_mus_property ("duration"));
173 Moment nb
= next_barline_moment ();
174 if (nb
< note_dur
.length_mom ())
176 note_dur
= find_nearest_duration (nb
.main_part_
);
179 next
.main_part_
+= note_dur
.length_mom ();
180 top_engraver ()->add_moment_to_process (next
);
181 do_nothing_until_
= next
.main_part_
;
186 left_to_do_
= orig
->length_mom ();
189 if (orig
&& note_dur
.length_mom() != orig
->length_mom())
191 if (!scratch_note_reqs_
.size ())
192 for (int i
= 0; i
< note_reqs_
.size (); i
++)
194 Music
* m
= note_reqs_
[i
]->clone ();
195 scratch_note_reqs_
.push (m
);
201 left_to_do_
&& i
< note_reqs_
.size (); i
++)
203 Item
*note
= new Item (get_property ("NoteHead"));
205 Music
* req
= note_reqs_
[i
];
206 if (scratch_note_reqs_
.size())
208 req
= scratch_note_reqs_
[i
];
209 SCM pits
= note_reqs_
[i
]->get_mus_property ("pitch");
210 req
->set_mus_property ("pitch",pits
);
213 req
->set_mus_property ("duration", note_dur
.smobbed_copy ());
214 note
->set_grob_property ("duration-log",
215 gh_int2scm (note_dur
.duration_log ()));
217 int dots
= note_dur
.dot_count ();
220 Item
* d
= new Item (get_property ("Dots"));
221 Rhythmic_head::set_dots (note
, d
);
224 measly attempt to save an eeny-weenie bit of memory.
226 if (dots
!= gh_scm2int (d
->get_grob_property ("dot-count")))
227 d
->set_grob_property ("dot-count", gh_int2scm (dots
));
229 d
->set_parent (note
, Y_AXIS
);
230 announce_grob (d
, SCM_EOL
);
234 Pitch
*pit
=unsmob_pitch (req
->get_mus_property ("pitch"));
236 int pos
= pit
->steps ();
237 SCM c0
= get_property ("centralCPosition");
238 if (gh_number_p (c0
))
239 pos
+= gh_scm2int (c0
);
241 note
->set_grob_property ("staff-position", gh_int2scm (pos
));
242 announce_grob (note
,req
->self_scm ());
246 left_to_do_
-= note_dur
.length_mom ();
250 don't do complicated arithmetic with grace notes.
253 && now_mom().grace_part_
)
255 left_to_do_
= Rational (0,0);
261 Completion_heads_engraver::stop_translation_timestep ()
263 for (int i
=0; i
< notes_
.size (); i
++)
265 typeset_grob (notes_
[i
]);
269 for (int i
=0; i
< dots_
.size (); i
++)
271 typeset_grob (dots_
[i
]);
275 for (int i
= scratch_note_reqs_
.size(); i
--;)
277 scm_gc_unprotect_object (scratch_note_reqs_
[i
]->self_scm () );
280 scratch_note_reqs_
.clear();
283 Tie_req
* tie_req
= 0;
286 Completion_heads_engraver::start_translation_timestep ()
288 Moment now
= now_mom ();
289 if (note_end_mom_
.main_part_
<= now
.main_part_
)
297 tie_req
= new Tie_req
;
299 bool succ
= daddy_trans_
->try_music (tie_req
);
302 programming_error ("Completion_heads_engraver: no-one to make tie.");
307 Completion_heads_engraver::Completion_heads_engraver()
311 ENTER_DESCRIPTION(Completion_heads_engraver
,
312 /* descr */ "This engraver replaces
313 @code{Note_heads_engraver}. It plays some trickery to
314 break long notes and automatically tie them into the next measure.",
315 /* creats*/ "NoteHead Dots",
317 /* reads */ "centralCPosition measurePosition measureLength",