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