]> git.donarmstrong.com Git - lilypond.git/blob - lily/completion-note-heads-engraver.cc
Completion_heads_engraver: retain note shape for non-tuplets. Fixes #1532.
[lilypond.git] / lily / completion-note-heads-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <cctype>
21 using namespace std;
22
23 #include "dot-column.hh"
24 #include "dots.hh"
25 #include "duration.hh"
26 #include "global-context.hh"
27 #include "item.hh"
28 #include "output-def.hh"
29 #include "pitch.hh"
30 #include "pqueue.hh"
31 #include "rhythmic-head.hh"
32 #include "score-engraver.hh"
33 #include "spanner.hh"
34 #include "staff-symbol-referencer.hh"
35 #include "stream-event.hh"
36 #include "tie.hh"
37 #include "warn.hh"
38
39 #include "translator.icc"
40
41 /*
42   TODO: make matching rest engraver.
43 */
44 struct Pending_tie
45 {
46   Moment when_;
47   Stream_event* tie_event_;
48   Pending_tie() { tie_event_ = 0; }
49 };
50
51 int compare(Pending_tie const &a, Pending_tie const &b)
52 {
53   return compare(a.when_, b.when_);
54 }
55
56
57 /*
58   How does this work?
59
60   When we catch the note, we predict the end of the note. We keep the
61   events living until we reach the predicted end-time.
62
63   Every time process_music () is called and there are note events, we
64   figure out how long the note to typeset should be. It should be no
65   longer than what's specified, than what is left to do and it should
66   not cross barlines.
67
68   We copy the events into scratch note events, to make sure that we get
69   all durations exactly right.
70 */
71
72 class Completion_heads_engraver : public Engraver
73 {
74   vector<Item*> notes_;
75   vector<Item*> prev_notes_;
76
77   // Must remember notes for explicit ties.
78   vector<Item*> tie_note_candidates_;
79   vector<Stream_event*> tie_note_candidate_events_;
80   vector<Grob*> ties_;
81   PQueue<Pending_tie> pending_ties_;
82   vector<Stream_event*> note_events_;
83
84   Stream_event *current_tie_event_;
85   Moment note_end_mom_;
86   bool is_first_;
87   Rational left_to_do_;
88   Rational do_nothing_until_;
89
90   Rational factor_;
91
92   Moment next_barline_moment ();
93   Item *make_note_head (Stream_event*);
94
95 public:
96   TRANSLATOR_DECLARATIONS (Completion_heads_engraver);
97
98 protected:
99   virtual void initialize ();
100   void make_tie (Grob *, Grob *);
101   void start_translation_timestep ();
102   void process_music ();
103   void stop_translation_timestep ();
104   DECLARE_TRANSLATOR_LISTENER (note);
105   DECLARE_TRANSLATOR_LISTENER (tie);
106 };
107
108 void
109 Completion_heads_engraver::initialize ()
110 {
111   is_first_ = false;
112   current_tie_event_ = 0;
113 }
114
115 IMPLEMENT_TRANSLATOR_LISTENER (Completion_heads_engraver, note);
116 void
117 Completion_heads_engraver::listen_note (Stream_event *ev)
118 {
119   note_events_.push_back (ev);
120   
121   is_first_ = true;
122   Moment now = now_mom ();
123   Moment musiclen = get_event_length (ev, now);
124
125   note_end_mom_ = max (note_end_mom_, (now + musiclen));
126   do_nothing_until_ = Rational (0, 0);
127 }
128
129 IMPLEMENT_TRANSLATOR_LISTENER (Completion_heads_engraver, tie);
130 void
131 Completion_heads_engraver::listen_tie (Stream_event *ev)
132 {
133   is_first_ = true;
134   current_tie_event_ = ev;
135 }
136
137 /*
138   The duration _until_ the next barline.
139 */
140 Moment
141 Completion_heads_engraver::next_barline_moment ()
142 {
143   Moment *e = unsmob_moment (get_property ("measurePosition"));
144   Moment *l = unsmob_moment (get_property ("measureLength"));
145   if (!e || !l || !to_boolean (get_property ("timing")))
146     {
147       return Moment (0, 0);
148     }
149
150   return (*l - *e);
151 }
152
153 Item*
154 Completion_heads_engraver::make_note_head (Stream_event *ev)
155 {
156   Item *note = make_item ("NoteHead", ev->self_scm ());
157   Pitch *pit = unsmob_pitch (ev->get_property ("pitch"));
158
159   int pos = pit->steps ();
160   SCM c0 = get_property ("middleCPosition");
161   if (scm_is_number (c0))
162     pos += scm_to_int (c0);
163
164   note->set_property ("staff-position", scm_from_int (pos));
165
166   return note;
167 }
168
169 void
170 Completion_heads_engraver::process_music ()
171 {
172   if (!is_first_ && !left_to_do_)
173     return;
174
175   if (current_tie_event_)
176     {
177       Pending_tie pending;
178       pending.when_ = note_end_mom_;
179       pending.tie_event_ = current_tie_event_;
180       pending_ties_.insert (pending);
181     }
182   
183   is_first_ = false;
184
185   Moment now = now_mom ();
186   if (do_nothing_until_ > now.main_part_)
187     return;
188
189   Duration note_dur;
190   Duration *orig = 0;
191   if (left_to_do_)
192     {
193       /*
194         note that note_dur may be strictly less than left_to_do_
195         (say, if left_to_do_ == 5/8)
196       */
197       note_dur = Duration (left_to_do_ / factor_, false).compressed (factor_);
198     }
199   else
200     {
201       orig = unsmob_duration (note_events_[0]->get_property ("duration"));
202       note_dur = *orig;
203       factor_ = note_dur.factor ();
204       left_to_do_ = orig->get_length ();
205     }
206   Moment nb = next_barline_moment ();
207   if (nb.main_part_ && nb < note_dur.get_length ())
208     note_dur = Duration (nb.main_part_ / factor_, false).compressed (factor_);
209
210   do_nothing_until_ = now.main_part_ + note_dur.get_length ();
211
212   for (vsize i = 0; left_to_do_ && i < note_events_.size (); i++)
213     {
214       bool need_clone = !orig || *orig != note_dur;
215       Stream_event *event = note_events_[i];
216
217       if (need_clone)
218         event = event->clone ();
219
220       SCM pits = note_events_[i]->get_property ("pitch");
221       Duration appearance = note_dur;
222       if (factor_.denominator () == 1 && factor_ > Rational (1, 1))
223         appearance = Duration (note_dur.get_length (), false);
224
225       event->set_property ("pitch", pits);
226       event->set_property ("duration", appearance.smobbed_copy ());
227       event->set_property ("length", Moment (appearance.get_length ()).smobbed_copy ());
228       event->set_property ("duration-log", scm_from_int (appearance.duration_log ()));
229
230       Item *note = make_note_head (event);
231       if (need_clone)
232         event->unprotect ();
233       notes_.push_back (note);
234     }
235   
236   if (pending_ties_.size ()
237       && pending_ties_.front().when_ == now_mom())
238     {
239       for (vsize i = 0; i < tie_note_candidate_events_.size(); i++)
240         for (vsize j = 0; j < note_events_.size(); j++)
241           {
242             Pitch *p =  unsmob_pitch (note_events_[j]->get_property ("pitch"));
243             Pitch *p_last
244               = unsmob_pitch (tie_note_candidate_events_[j]->get_property ("pitch"));
245             if (p && p_last && *p == *p_last)
246               make_tie (tie_note_candidates_[i], notes_[j]);
247           }
248     }
249       
250   if (prev_notes_.size () == notes_.size ())
251     {
252       for (vsize i = 0; i < notes_.size (); i++)
253         make_tie (prev_notes_[i], notes_[i]);
254     }
255
256   left_to_do_ -= note_dur.get_length ();
257   if (left_to_do_)
258     get_global_context ()->add_moment_to_process (now.main_part_ + note_dur.get_length());
259   /*
260     don't do complicated arithmetic with grace notes.
261   */
262   if (orig && now_mom ().grace_part_)
263     left_to_do_ = Rational (0, 0);
264 }
265
266 void
267 Completion_heads_engraver::make_tie (Grob *left, Grob *right)
268 {
269   Grob *p = make_spanner ("Tie", SCM_EOL);
270   Tie::set_head (p, LEFT, left);
271   Tie::set_head (p, RIGHT, right);
272   ties_.push_back (p);
273 }
274                                      
275 void
276 Completion_heads_engraver::stop_translation_timestep ()
277 {
278   ties_.clear ();
279
280   if (notes_.size ())
281     prev_notes_ = notes_;
282   notes_.clear ();
283 }
284
285 void
286 Completion_heads_engraver::start_translation_timestep ()
287 {
288   Moment now = now_mom ();
289   while (pending_ties_.size() && pending_ties_.front().when_ < now)
290     {
291       pending_ties_.delmin();
292     }
293   current_tie_event_ = 0;
294   if (note_end_mom_.main_part_ <= now.main_part_)
295     {
296       tie_note_candidate_events_ = note_events_;
297       tie_note_candidates_ = prev_notes_;
298
299       note_events_.clear ();
300       prev_notes_.clear ();
301     }
302   context ()->set_property ("completionBusy",
303                             ly_bool2scm (note_events_.size ()));
304 }
305
306 Completion_heads_engraver::Completion_heads_engraver ()
307 {
308 }
309
310 ADD_TRANSLATOR (Completion_heads_engraver,
311                 /* doc */
312                 "This engraver replaces @code{Note_heads_engraver}.  It plays"
313                 " some trickery to break long notes and automatically tie them"
314                 " into the next measure.",
315
316                 /* create */
317                 "NoteHead "
318                 "Dots "
319                 "Tie ",
320
321                 /* read */
322                 "middleCPosition "
323                 "measurePosition "
324                 "measureLength ",
325
326                 /* write */
327                 "completionBusy "
328                 );