]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-performer.cc
Update source file headers. Fixes using standard GNU package conventions.
[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 "international.hh"
27 #include <deque>
28
29 struct Head_event_tuple
30 {
31   Audio_element_info head_;
32   Moment moment_;
33   Head_event_tuple () { }
34   Head_event_tuple (Audio_element_info h, Moment m)
35   {
36     head_ = h;
37     moment_ = m;
38   }
39 };
40
41
42 class Tie_performer : public Performer
43 {
44   Stream_event *event_;
45   // We don't really need a deque here. A vector would suffice. However,
46   // for some strange reason, using vectors always leads to memory 
47   // corruption in the STL templates! (i.e. after the first
48   // now_heads_.push_back (inf_mom), the now_heads_.size() will be 
49   // something like 3303820998 :(
50   deque<Head_event_tuple> now_heads_;
51   deque<Head_event_tuple> now_tied_heads_;
52   deque<Head_event_tuple> heads_to_tie_;
53
54 protected:
55   void stop_translation_timestep ();
56   void start_translation_timestep ();
57   virtual void acknowledge_audio_element (Audio_element_info);
58   void process_music ();
59   DECLARE_TRANSLATOR_LISTENER (tie);
60 public:
61   TRANSLATOR_DECLARATIONS (Tie_performer);
62 };
63
64 Tie_performer::Tie_performer ()
65 {
66   event_ = 0;
67 }
68
69 IMPLEMENT_TRANSLATOR_LISTENER (Tie_performer, tie);
70 void
71 Tie_performer::listen_tie (Stream_event *ev)
72 {
73   event_ = ev;
74 }
75
76 void
77 Tie_performer::process_music ()
78 {
79   if (event_)
80     context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
81 }
82
83 void
84 Tie_performer::acknowledge_audio_element (Audio_element_info inf)
85 {
86   if (Audio_note *an = dynamic_cast<Audio_note *> (inf.elem_))
87     {
88 //       message (_f ("acknowledge_audio_element, Size of now_heads_=%d", now_heads_.size ()));
89       Head_event_tuple inf_mom (inf, now_mom ());
90       if (an->tie_event_)
91         now_tied_heads_.push_back (inf_mom);
92       else
93         now_heads_.push_back (inf_mom);
94
95 //       message (_f ("acknowledge_audio_element, added, Size of now_heads_=%d", now_heads_.size ()));
96       // Find a previous note that ties to the current note. If it exists, 
97       // remove it from the heads_to_tie vector and create the tie
98       deque<Head_event_tuple>::iterator it;
99       bool found = false;
100       Stream_event *right_mus = inf.event_;
101       for ( it = heads_to_tie_.begin() ; (!found) && (it < heads_to_tie_.end()); 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               Moment skip = now_mom() - (*it).moment_ - th->length_mom_;
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 void
129 Tie_performer::stop_translation_timestep ()
130 {
131   // We might have dangling open ties like c~ d. Close them, unless we have
132   // tieWaitForNote set...
133   if (!to_boolean (get_property ("tieWaitForNote")))
134     {
135       heads_to_tie_.clear ();
136     }
137
138   if (event_)
139     {
140       for (vsize i = now_heads_.size (); i--;)
141         heads_to_tie_.push_back (now_heads_[i]);
142     }
143
144   for (vsize i = now_tied_heads_.size (); i--;)
145     heads_to_tie_.push_back (now_tied_heads_[i]);
146
147   event_ = 0;
148   now_heads_.clear ();
149   now_tied_heads_.clear ();
150 }
151
152 ADD_TRANSLATOR (Tie_performer,
153                 /* doc */
154                 "Generate ties between note heads of equal pitch.",
155
156                 /* create */
157                 "",
158
159                 /* read */
160                 "tieWaitForNote",
161
162                 /* write */
163                 "tieMelismaBusy"
164                 );