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