]> git.donarmstrong.com Git - lilypond.git/blob - lily/completion-note-heads-engraver.cc
27d950b690ecbd4f8aff608bc0f3aa33946df4aa
[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--2015 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
22 #include "dot-column.hh"
23 #include "dots.hh"
24 #include "duration.hh"
25 #include "global-context.hh"
26 #include "item.hh"
27 #include "output-def.hh"
28 #include "pitch.hh"
29 #include "rhythmic-head.hh"
30 #include "score-engraver.hh"
31 #include "spanner.hh"
32 #include "staff-symbol-referencer.hh"
33 #include "stream-event.hh"
34 #include "tie.hh"
35 #include "tie-column.hh"
36 #include "warn.hh"
37 #include "misc.hh"
38
39 #include "translator.icc"
40
41 using std::vector;
42
43 /*
44   How does this work?
45
46   When we catch the note, we predict the end of the note. We keep the
47   events living until we reach the predicted end-time.
48
49   Every time process_music () is called and there are note events, we
50   figure out how long the note to typeset should be. It should be no
51   longer than what's specified, than what is left to do and it should
52   not cross barlines or sub-bar units.
53
54   We copy the events into scratch note events, to make sure that we get
55   all durations exactly right.
56 */
57
58 class Completion_heads_engraver : public Engraver
59 {
60   vector<Item *> notes_;
61   vector<Item *> prev_notes_;
62   // Must remember notes for explicit ties.
63   vector<Spanner *> ties_;
64   vector<Stream_event *> note_events_;
65   Spanner *tie_column_;
66   Moment note_end_mom_;
67   bool is_first_;
68   Rational left_to_do_;
69   Rational do_nothing_until_;
70   Rational factor_;
71
72   Moment next_moment (Rational const &);
73   Item *make_note_head (Stream_event *);
74
75 public:
76   TRANSLATOR_DECLARATIONS (Completion_heads_engraver);
77
78 protected:
79   virtual void initialize ();
80   void make_tie (Grob *, Grob *);
81   void start_translation_timestep ();
82   void process_music ();
83   void stop_translation_timestep ();
84   DECLARE_TRANSLATOR_LISTENER (note);
85 };
86
87 void
88 Completion_heads_engraver::initialize ()
89 {
90   is_first_ = false;
91 }
92
93 IMPLEMENT_TRANSLATOR_LISTENER (Completion_heads_engraver, note);
94 void
95 Completion_heads_engraver::listen_note (Stream_event *ev)
96 {
97   note_events_.push_back (ev);
98
99   is_first_ = true;
100   Moment now = now_mom ();
101   Moment musiclen = get_event_length (ev, now);
102
103   note_end_mom_ = std::max (note_end_mom_, (now + musiclen));
104   do_nothing_until_ = Rational (0, 0);
105 }
106
107 /*
108   The duration _until_ the next bar line or completion unit
109 */
110 Moment
111 Completion_heads_engraver::next_moment (Rational const &note_len)
112 {
113   Moment *e = unsmob<Moment> (get_property ("measurePosition"));
114   Moment *l = unsmob<Moment> (get_property ("measureLength"));
115   if (!e || !l || !to_boolean (get_property ("timing")))
116     {
117       return Moment (0, 0);
118     }
119
120   Moment result = *l - *e;
121   if (result < 0)
122     {
123       programming_error ("invalid measure position: "
124                          + e->to_string () + " of " + l->to_string ());
125       return 0;
126     }
127   Moment const *unit = unsmob<Moment> (get_property ("completionUnit"));
128
129   if (unit)
130     {
131       Rational const now_unit = e->main_part_ / unit->main_part_;
132       if (now_unit.den () > 1)
133         {
134           /*
135             within a unit - go to the end of that
136           */
137           result = unit->main_part_
138                    * (Rational (1) - (now_unit - now_unit.trunc_rat ()));
139         }
140       else
141         {
142           /*
143             at the beginning of a unit:
144             take a power-of-two number of units, but not more than required,
145             since then the Duration constructor destroys the unit structure
146           */
147           if (note_len < result.main_part_)
148             result.main_part_ = note_len;
149           Rational const step_unit = result.main_part_ / unit->main_part_;
150           if (step_unit.den () < step_unit.num ())
151             {
152               int const log2
153                 = intlog2 (int (step_unit.num () / step_unit.den ()));
154               result.main_part_ = unit->main_part_ * Rational (1 << log2);
155             }
156         }
157     }
158
159   return result;
160 }
161
162 Item *
163 Completion_heads_engraver::make_note_head (Stream_event *ev)
164 {
165   Item *note = make_item ("NoteHead", ev->self_scm ());
166   Pitch *pit = unsmob<Pitch> (ev->get_property ("pitch"));
167
168   int pos = pit->steps ();
169   SCM c0 = get_property ("middleCPosition");
170   if (scm_is_number (c0))
171     pos += scm_to_int (c0);
172
173   note->set_property ("staff-position", scm_from_int (pos));
174
175   return note;
176 }
177
178 void
179 Completion_heads_engraver::process_music ()
180 {
181   if (!is_first_ && !left_to_do_)
182     return;
183
184   is_first_ = false;
185
186   Moment now = now_mom ();
187   if (do_nothing_until_ > now.main_part_)
188     return;
189
190   Duration note_dur;
191   Duration *orig = 0;
192   if (left_to_do_)
193     {
194       /*
195         note that note_dur may be strictly less than left_to_do_
196         (say, if left_to_do_ == 5/8)
197       */
198       note_dur = Duration (left_to_do_ / factor_, false).compressed (factor_);
199     }
200   else
201     {
202       orig = unsmob<Duration> (note_events_[0]->get_property ("duration"));
203       note_dur = *orig;
204       SCM factor = get_property ("completionFactor");
205       if (ly_is_procedure (factor))
206         factor = scm_call_2 (factor,
207                              context ()->self_scm (),
208                              note_dur.smobbed_copy ());
209       factor_ = robust_scm2rational (factor, note_dur.factor ());
210       left_to_do_ = orig->get_length ();
211     }
212   Moment nb = next_moment (note_dur.get_length ());
213   if (nb.main_part_ && nb < note_dur.get_length ())
214     {
215       note_dur = Duration (nb.main_part_ / factor_, false).compressed (factor_);
216     }
217
218   do_nothing_until_ = now.main_part_ + note_dur.get_length ();
219
220   for (vsize i = 0; left_to_do_ && i < note_events_.size (); i++)
221     {
222       bool need_clone = !orig || *orig != note_dur;
223       Stream_event *event = note_events_[i];
224
225       if (need_clone)
226         event = event->clone ();
227
228       SCM pits = note_events_[i]->get_property ("pitch");
229       event->set_property ("pitch", pits);
230       event->set_property ("duration", note_dur.smobbed_copy ());
231       event->set_property ("length", Moment (note_dur.get_length ()).smobbed_copy ());
232       event->set_property ("duration-log", scm_from_int (note_dur.duration_log ()));
233
234       /*
235         The Completion_heads_engraver splits an event into a group of consecutive events.
236         For each event in the group, the property "autosplit-end" denotes whether the current event
237         was truncated during splitting. Based on "autosplit-end", the Tie_engraver decides whether a
238         tie event should be processed.
239       */
240       event->set_property ("autosplit-end",
241                            ly_bool2scm (left_to_do_ - note_dur.get_length () > Rational (0)));
242
243       Item *note = make_note_head (event);
244       if (need_clone)
245         event->unprotect ();
246       notes_.push_back (note);
247     }
248
249   if (prev_notes_.size () == notes_.size ())
250     {
251       for (vsize i = 0; i < notes_.size (); i++)
252         make_tie (prev_notes_[i], notes_[i]);
253     }
254
255   if (ties_.size () && !tie_column_)
256     tie_column_ = make_spanner ("TieColumn", ties_[0]->self_scm ());
257
258   if (tie_column_)
259     for (vsize i = ties_.size (); i--;)
260       Tie_column::add_tie (tie_column_, ties_[i]);
261
262   left_to_do_ -= note_dur.get_length ();
263   if (left_to_do_)
264     get_global_context ()->add_moment_to_process (now.main_part_ + note_dur.get_length ());
265   /*
266     don't do complicated arithmetic with grace notes.
267   */
268   if (orig && now_mom ().grace_part_)
269     left_to_do_ = Rational (0, 0);
270 }
271
272 void
273 Completion_heads_engraver::make_tie (Grob *left, Grob *right)
274 {
275   Spanner *p = make_spanner ("Tie", SCM_EOL);
276   Tie::set_head (p, LEFT, left);
277   Tie::set_head (p, RIGHT, right);
278   announce_end_grob (p, SCM_EOL);
279   ties_.push_back (p);
280 }
281
282 void
283 Completion_heads_engraver::stop_translation_timestep ()
284 {
285   ties_.clear ();
286   tie_column_ = 0;
287
288   if (notes_.size ())
289     prev_notes_ = notes_;
290   notes_.clear ();
291 }
292
293 void
294 Completion_heads_engraver::start_translation_timestep ()
295 {
296   Moment now = now_mom ();
297   if (note_end_mom_.main_part_ <= now.main_part_)
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   tie_column_ = 0;
309 }
310
311 ADD_TRANSLATOR (Completion_heads_engraver,
312                 /* doc */
313                 "This engraver replaces @code{Note_heads_engraver}.  It plays"
314                 " some trickery to break long notes and automatically tie them"
315                 " into the next measure.",
316
317                 /* create */
318                 "NoteHead "
319                 "Tie "
320                 "TieColumn ",
321
322                 /* read */
323                 "completionFactor "
324                 "completionUnit "
325                 "measureLength "
326                 "measurePosition "
327                 "middleCPosition "
328                 "timing ",
329
330                 /* write */
331                 "completionBusy "
332                );