]> git.donarmstrong.com Git - lilypond.git/blob - lily/completion-note-heads-engraver.cc
(all-grob-descriptions): remove gap from
[lilypond.git] / lily / completion-note-heads-engraver.cc
1 /*
2   completion-note-heads-engraver.cc -- Completion_heads_engraver
3
4   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
5 */
6
7 #include <ctype.h>
8
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"
18 #include "spanner.hh"
19 #include "tie.hh"
20 #include "global-context.hh"
21
22 /*
23 TODO: make matching rest engraver.
24 */
25
26 /*
27
28   How does this work?
29
30   When we catch the note, we predict the end of the note. We keep the
31   events living until we reach the predicted end-time.
32
33   Every time process_music () is called and there are note events, we
34   figure out how long the note to typeset should be. It should be no
35   longer than what's specified, than what is left to do and it should
36   not cross barlines.
37   
38   We copy the reqs into scratch note reqs, to make sure that we get
39   all durations exactly right.
40 */
41
42 class Completion_heads_engraver : public Engraver
43 {
44   Link_array<Item> notes_;
45   Link_array<Item> prev_notes_;
46   Link_array<Grob> ties_;
47   
48   Link_array<Item> dots_;
49   Link_array<Music> note_reqs_;
50   Link_array<Music> scratch_note_reqs_;
51
52   Moment note_end_mom_;
53   bool first_b_;
54   Rational left_to_do_;
55   Rational do_nothing_until_;
56   
57   Moment next_barline_moment ();
58   Duration find_nearest_duration (Rational length);
59   
60 public:
61   TRANSLATOR_DECLARATIONS (Completion_heads_engraver);
62
63 protected:
64   virtual void initialize ();
65   virtual void start_translation_timestep ();
66   virtual bool try_music (Music *req) ;
67   virtual void process_music ();
68   virtual void stop_translation_timestep ();
69 };
70
71 void
72 Completion_heads_engraver::initialize ()
73 {
74   first_b_ = false;
75 }
76
77 bool
78 Completion_heads_engraver::try_music (Music *m) 
79 {
80   if (m->is_mus_type ("note-event"))
81     {
82       note_reqs_.push (m);
83
84       first_b_ = true;
85       Moment musiclen = m->get_length ();
86       Moment now = now_mom ();
87
88       if (now_mom ().grace_part_)
89         {
90           musiclen.grace_part_ = musiclen.main_part_ ;
91           musiclen.main_part_ = Rational (0,1);
92         }
93       note_end_mom_  = note_end_mom_ >? (now + musiclen);
94       do_nothing_until_ = Rational (0,0);
95       
96       return true;
97     }
98   else if  (m->is_mus_type ("busy-playing-event"))
99     {
100       return note_reqs_.size ();
101     }
102   
103   return false;
104   
105 }
106
107 /*
108   The duration _until_ the next barline.
109  */
110 Moment
111 Completion_heads_engraver::next_barline_moment ( )
112 {
113   Moment *e = unsmob_moment (get_property ("measurePosition"));
114   Moment *l = unsmob_moment (get_property ("measureLength"));
115   if (!e || !l)
116     {
117       programming_error ("No timing props set?");
118       return Moment (1,1);
119     }
120
121   return (*l - *e);
122 }
123
124 Duration  
125 Completion_heads_engraver::find_nearest_duration (Rational length)
126 {
127   int log_limit= 6;
128
129   Duration d (0,0);
130
131   /*
132     this could surely be done more efficient. Left to the reader as an
133     excercise.  */
134   while (d.get_length () > length && d.duration_log () < log_limit)
135     {
136       if (d.dot_count ())
137         {
138           d = Duration (d.duration_log (), d.dot_count ()- 1);
139           continue;
140         }
141       else
142         {
143           d = Duration (d.duration_log () + 1, 2);
144         }
145     }
146
147   if (d.duration_log () >= log_limit)
148     {
149       // junk the dots.
150       d = Duration (d.duration_log (), 0);
151
152       // scale up.
153       d = d.compressed (length / d.get_length ());
154     }
155   
156   return d;
157 }
158
159 void
160 Completion_heads_engraver::process_music ()
161 {
162   if (!first_b_ && !left_to_do_)
163     return ;
164   
165   first_b_ = false;
166
167   Moment now =  now_mom ();
168   if (do_nothing_until_ > now.main_part_)
169     return ;
170   
171   Duration note_dur;
172   Duration *orig = 0;
173   if (left_to_do_)
174     {
175       note_dur = find_nearest_duration (left_to_do_);
176     }
177   else
178     {
179       orig = unsmob_duration (note_reqs_[0]->get_property ("duration"));
180       note_dur = *orig;
181     }
182   Moment nb = next_barline_moment ();
183   if (nb < note_dur.get_length ())
184     {
185       note_dur = find_nearest_duration (nb.main_part_);
186
187       Moment next = now;
188       next.main_part_ += note_dur.get_length ();
189       
190       get_global_context ()->add_moment_to_process (next);
191       do_nothing_until_ = next.main_part_;
192     }
193
194   if (orig)
195     {
196       left_to_do_ = orig->get_length ();
197     }
198
199   if (orig && note_dur.get_length () != orig->get_length ())
200     {
201       if (!scratch_note_reqs_.size ())
202         for (int i = 0; i < note_reqs_.size (); i++)
203           {
204             Music * m = note_reqs_[i]->clone ();
205             scratch_note_reqs_.push (m);
206           }
207     }
208
209   
210   for (int i = 0;
211        left_to_do_ && i < note_reqs_.size (); i++)
212     {
213       Item *note  = make_item ("NoteHead");
214       
215       Music * req =  note_reqs_[i];
216       if (scratch_note_reqs_.size ())
217         {
218           req = scratch_note_reqs_[i];
219           SCM pits = note_reqs_[i]->get_property ("pitch");
220           req->set_property ("pitch",pits);
221         }
222       
223       req->set_property ("duration", note_dur.smobbed_copy ());
224       note->set_property ("duration-log",
225                                  gh_int2scm (note_dur.duration_log ()));
226       
227       int dots= note_dur.dot_count ();
228       if (dots)
229         {
230           Item * d = make_item ("Dots");
231           Rhythmic_head::set_dots (note, d);
232
233           /*
234            measly attempt to save an eeny-weenie bit of memory.
235           */
236           if (dots != gh_scm2int (d->get_property ("dot-count")))
237             d->set_property ("dot-count", gh_int2scm (dots));
238
239           d->set_parent (note, Y_AXIS);
240           announce_grob (d, SCM_EOL);
241           dots_.push (d);
242         }
243
244       Pitch *pit =unsmob_pitch (req->get_property ("pitch"));
245
246       int pos = pit->steps ();
247       SCM c0 = get_property ("middleCPosition");
248       if (gh_number_p (c0))
249         pos += gh_scm2int (c0);
250
251       note->set_property ("staff-position",   gh_int2scm (pos));
252       announce_grob (note,req->self_scm ());
253       notes_.push (note);
254     }
255   
256   if (prev_notes_.size () == notes_.size ())
257     {
258       for (int i= 0; i < notes_.size (); i++)
259         {
260           Grob * p = make_spanner ("Tie");
261           Tie::set_interface (p); // cannot remove yet!
262           
263           Tie::set_head (p, LEFT, prev_notes_[i]);
264           Tie::set_head (p, RIGHT, notes_[i]);
265           
266           ties_.push (p);
267           announce_grob (p, SCM_EOL);
268         }
269     }
270
271   left_to_do_ -= note_dur.get_length ();
272
273   /*
274     don't do complicated arithmetic with grace notes.
275    */
276   if (orig
277       &&  now_mom ().grace_part_ )
278     {
279       left_to_do_ = Rational (0,0);
280     }
281 }
282  
283 void
284 Completion_heads_engraver::stop_translation_timestep ()
285 {
286   for (int i = ties_.size (); i--;)
287     typeset_grob (ties_[i]); 
288   ties_.clear ();
289   
290   for (int i=0; i < notes_.size (); i++)
291     {
292       typeset_grob (notes_[i]);
293     }
294   if (notes_.size ())
295     prev_notes_ = notes_;
296   notes_.clear ();
297   
298   for (int i=0; i < dots_.size (); i++)
299     {
300       typeset_grob (dots_[i]);
301     }
302   dots_.clear ();
303
304   for (int i = scratch_note_reqs_.size (); i--;)
305     {
306       scm_gc_unprotect_object (scratch_note_reqs_[i]->self_scm () );
307     }
308   
309   scratch_note_reqs_.clear ();
310 }
311
312 void
313 Completion_heads_engraver::start_translation_timestep ()
314 {
315   Moment now = now_mom ();
316   if (note_end_mom_.main_part_ <= now.main_part_)
317     {
318       note_reqs_.clear ();
319       prev_notes_.clear ();
320     }
321 }
322
323 Completion_heads_engraver::Completion_heads_engraver ()
324 {
325 }
326
327 ENTER_DESCRIPTION (Completion_heads_engraver,
328 /* descr */       "This engraver replaces "
329 "@code{Note_heads_engraver}. It plays some trickery to "
330 "break long notes and automatically tie them into the next measure.",
331 /* creats*/       "NoteHead Dots Tie",
332 /* accepts */     "busy-playing-event note-event",
333 /* acks  */      "",
334 /* reads */       "middleCPosition measurePosition measureLength",
335 /* write */       "");