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