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