2002->2003
[lilypond.git] / lily / completion-note-heads-engraver.cc
blob676c39a6ef483a61ecb6f33cf3927325394f3429
1 /*
2 head-grav.cc -- part of GNU LilyPond
4 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
5 */
7 #include <ctype.h>
9 #include "rhythmic-head.hh"
10 #include "paper-def.hh"
11 #include "event.hh"
12 #include "dots.hh"
13 #include "dot-column.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "item.hh"
16 #include "score-engraver.hh"
17 #include "warn.hh"
21 How does this work?
23 When we catch the note, we predict the end of the note. We keep the
24 events living until we reach the predicted end-time.
26 Every time process_music() is called and there are note events, 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
29 not cross barlines.
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_;
43 Moment note_end_mom_;
44 bool first_b_;
45 Rational left_to_do_;
46 Rational do_nothing_until_;
48 Moment next_barline_moment ();
49 Duration find_nearest_duration (Rational length);
51 public:
52 TRANSLATOR_DECLARATIONS(Completion_heads_engraver);
54 protected:
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 ();
62 void
63 Completion_heads_engraver::initialize ()
65 first_b_ = false;
68 bool
69 Completion_heads_engraver::try_music (Music *m)
71 if (m->is_mus_type ("note-event"))
73 note_reqs_.push (m);
75 first_b_ = true;
76 Moment musiclen = m->get_length ();
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);
87 return true;
89 else if (m->is_mus_type ("busy-playing-event"))
91 return note_reqs_.size ();
94 return false;
99 The duration _until_ the next barline.
101 Moment
102 Completion_heads_engraver::next_barline_moment ( )
104 Moment *e = unsmob_moment (get_property ("measurePosition"));
105 Moment *l = unsmob_moment (get_property ("measureLength"));
106 if (!e || !l)
108 programming_error ("No timing props set?");
109 return Moment (1,1);
112 return (*l - *e);
115 Duration
116 Completion_heads_engraver::find_nearest_duration (Rational length)
118 int log_limit= 6;
120 Duration d(0,0);
123 this could surely be done more efficient. Left to the reader as an
124 excercise. */
125 while (d.get_length () > length && d.duration_log () < log_limit)
127 if (d.dot_count ())
129 d = Duration (d.duration_log (), d.dot_count ()- 1);
130 continue;
132 else
134 d = Duration (d.duration_log () + 1, 2);
138 if (d.duration_log () >= log_limit)
140 // junk the dots.
141 d = Duration (d.duration_log (), 0);
143 // scale up.
144 d = d.compressed (length / d.get_length ());
147 return d;
150 void
151 Completion_heads_engraver::process_music ()
153 if (!first_b_ && !left_to_do_)
154 return ;
156 first_b_ = false;
158 Moment now = now_mom ();
159 if (do_nothing_until_ > now.main_part_)
160 return ;
162 Duration note_dur;
163 Duration *orig = 0;
164 if (left_to_do_)
166 note_dur = find_nearest_duration (left_to_do_);
168 else
170 orig = unsmob_duration (note_reqs_[0]->get_mus_property ("duration"));
171 note_dur = *orig;
173 Moment nb = next_barline_moment ();
174 if (nb < note_dur.get_length ())
176 note_dur = find_nearest_duration (nb.main_part_);
178 Moment next = now;
179 next.main_part_ += note_dur.get_length ();
180 top_engraver ()->add_moment_to_process (next);
181 do_nothing_until_ = next.main_part_;
184 if (orig)
186 left_to_do_ = orig->get_length ();
189 if (orig && note_dur.get_length () != orig->get_length ())
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);
200 for (int i = 0;
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 ();
218 if (dots)
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);
231 dots_.push (d);
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 ());
243 notes_.push (note);
246 left_to_do_ -= note_dur.get_length ();
250 don't do complicated arithmetic with grace notes.
252 if (orig
253 && now_mom().grace_part_ )
255 left_to_do_ = Rational (0,0);
260 void
261 Completion_heads_engraver::stop_translation_timestep ()
263 for (int i=0; i < notes_.size (); i++)
265 typeset_grob (notes_[i]);
267 notes_.clear ();
269 for (int i=0; i < dots_.size (); i++)
271 typeset_grob (dots_[i]);
273 dots_.clear ();
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 Music * tie_req = 0;
285 void
286 Completion_heads_engraver::start_translation_timestep ()
288 Moment now = now_mom ();
289 if (note_end_mom_.main_part_ <= now.main_part_)
291 note_reqs_.clear ();
294 if (left_to_do_)
296 if (!tie_req)
297 tie_req = make_music_by_name (ly_symbol2scm ("TieEvent"));
299 bool succ = daddy_trans_->try_music (tie_req);
300 if (!succ)
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",
316 /* accepts */ "busy-playing-event note-event",
317 /* acks */ "",
318 /* reads */ "centralCPosition measurePosition measureLength",
319 /* write */ "");