]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-performer.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / tie-performer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2014 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 "performer.hh"
21
22 #include "audio-item.hh"
23 #include "context.hh"
24 #include "stream-event.hh"
25 #include "translator.icc"
26 #include <list>
27
28 struct Head_audio_event_tuple
29 {
30   Audio_element_info head_;
31   // The end moment of the note, so we can calculate a skip and check whether
32   // the note still goes on
33   Moment end_moment_;
34   Head_audio_event_tuple () {}
35   Head_audio_event_tuple (Audio_element_info h, Moment m)
36   {
37     head_ = h;
38     end_moment_ = m;
39   }
40 };
41
42 class Tie_performer : public Performer
43 {
44   Stream_event *event_;
45   list<Head_audio_event_tuple> now_heads_;
46   list<Head_audio_event_tuple> now_tied_heads_; // new tied notes
47   list<Head_audio_event_tuple> heads_to_tie_; // heads waiting for closing tie
48
49 protected:
50   void stop_translation_timestep ();
51   void start_translation_timestep ();
52   virtual void acknowledge_audio_element (Audio_element_info);
53   void process_music ();
54   DECLARE_TRANSLATOR_LISTENER (tie);
55 public:
56   TRANSLATOR_DECLARATIONS (Tie_performer);
57 };
58
59 Tie_performer::Tie_performer ()
60 {
61   event_ = 0;
62 }
63
64 IMPLEMENT_TRANSLATOR_LISTENER (Tie_performer, tie);
65 void
66 Tie_performer::listen_tie (Stream_event *ev)
67 {
68   event_ = ev;
69 }
70
71 void
72 Tie_performer::process_music ()
73 {
74   if (event_)
75     context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
76 }
77
78 void
79 Tie_performer::acknowledge_audio_element (Audio_element_info inf)
80 {
81   if (Audio_note *an = dynamic_cast<Audio_note *> (inf.elem_))
82     {
83       // for each tied note, store the info and its end moment, so we can
84       // later on check whether (1) the note is still ongoing and (2) how
85       // long the skip is with tieWaitForNote
86       Head_audio_event_tuple inf_mom (inf, now_mom () + an->length_mom_);
87       if (an->tie_event_)
88         now_tied_heads_.push_back (inf_mom);
89       else
90         now_heads_.push_back (inf_mom);
91
92       // Find a previous note that ties to the current note. If it exists,
93       // remove it from the heads_to_tie vector and create the tie
94       list<Head_audio_event_tuple>::iterator it;
95       bool found = false;
96       Stream_event *right_mus = inf.event_;
97       for (it = heads_to_tie_.begin ();
98            !found && (it != heads_to_tie_.end ());
99            it++)
100         {
101           Audio_element_info et = (*it).head_;
102           Audio_note *th = dynamic_cast<Audio_note *> (et.elem_);
103           Stream_event *left_mus = et.event_;
104
105           if (th && right_mus && left_mus
106               && ly_is_equal (right_mus->get_property ("pitch"),
107                               left_mus->get_property ("pitch")))
108             {
109               found = true;
110               // (*it).moment_ already stores the end of the tied note!
111               Moment skip = now_mom () - (*it).end_moment_;
112               an->tie_to (th, skip);
113               it = heads_to_tie_.erase (it);
114             }
115         }
116     }
117 }
118
119 void
120 Tie_performer::start_translation_timestep ()
121 {
122   context ()->set_property ("tieMelismaBusy",
123                             ly_bool2scm (heads_to_tie_.size ()));
124 }
125
126 // a predicate implemented as a class, used to delete all tied notes with end
127 // moment in the past:
128 class end_moment_passed
129 {
130 protected:
131   Moment now;
132 public:
133   end_moment_passed (Moment mom) : now (mom) {}
134   bool operator () (const Head_audio_event_tuple &value)
135   {
136     return (value.end_moment_ <= now);
137   }
138 };
139
140 void
141 Tie_performer::stop_translation_timestep ()
142 {
143   // We might have dangling open ties like c~ d. Close them, unless the first
144   // note is still ongoing or we have we have tieWaitForNote set...
145   if (!to_boolean (get_property ("tieWaitForNote")))
146     {
147       heads_to_tie_.remove_if (end_moment_passed (now_mom ()));
148     }
149
150   // Append now_heads_ and now_tied_heads to heads_to_tie_ for the next time step
151   if (event_)
152     {
153       heads_to_tie_.splice (heads_to_tie_.end (), now_heads_);
154     }
155   heads_to_tie_.splice (heads_to_tie_.end (), now_tied_heads_);
156
157   event_ = 0;
158   now_heads_.clear ();
159   now_tied_heads_.clear ();
160 }
161
162 ADD_TRANSLATOR (Tie_performer,
163                 /* doc */
164                 "Generate ties between note heads of equal pitch.",
165
166                 /* create */
167                 "",
168
169                 /* read */
170                 "tieWaitForNote",
171
172                 /* write */
173                 "tieMelismaBusy"
174                );