]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-performer.cc
tie-performer: using a vector always leads to memory corruption
[lilypond.git] / lily / tie-performer.cc
1 /*
2   tie-performer.cc -- implement Tie_performer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "performer.hh"
10
11 #include "audio-item.hh"
12 #include "context.hh"
13 #include "stream-event.hh"
14 #include "translator.icc"
15 // #include "international.hh"
16 #include <deque>
17
18 struct Head_event_tuple
19 {
20   Audio_element_info head_;
21   Moment moment_;
22   Head_event_tuple () { }
23   Head_event_tuple (Audio_element_info h, Moment m)
24   {
25     head_ = h;
26     moment_ = m;
27   }
28 };
29
30
31 class Tie_performer : public Performer
32 {
33   Stream_event *event_;
34   // We don't really need a deque here. A vector would suffice. However,
35   // for some strange reason, using vectors always leads to memory 
36   // corruption in the STL templates! (i.e. after the first
37   // now_heads_.push_back (inf_mom), the now_heads_.size() will be 
38   // something like 3303820998 :(
39   deque<Head_event_tuple> now_heads_;
40   deque<Head_event_tuple> now_tied_heads_;
41   deque<Head_event_tuple> heads_to_tie_;
42
43 protected:
44   void stop_translation_timestep ();
45   void start_translation_timestep ();
46   virtual void acknowledge_audio_element (Audio_element_info);
47   void process_music ();
48   DECLARE_TRANSLATOR_LISTENER (tie);
49 public:
50   TRANSLATOR_DECLARATIONS (Tie_performer);
51 };
52
53 Tie_performer::Tie_performer ()
54 {
55   event_ = 0;
56 }
57
58 IMPLEMENT_TRANSLATOR_LISTENER (Tie_performer, tie);
59 void
60 Tie_performer::listen_tie (Stream_event *ev)
61 {
62   event_ = ev;
63 }
64
65 void
66 Tie_performer::process_music ()
67 {
68   if (event_)
69     context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
70 }
71
72 void
73 Tie_performer::acknowledge_audio_element (Audio_element_info inf)
74 {
75   if (Audio_note *an = dynamic_cast<Audio_note *> (inf.elem_))
76     {
77 //       message (_f ("acknowledge_audio_element, Size of now_heads_=%d", now_heads_.size ()));
78       Head_event_tuple inf_mom (inf, now_mom ());
79       if (an->tie_event_)
80         now_tied_heads_.push_back (inf_mom);
81       else
82         now_heads_.push_back (inf_mom);
83
84 //       message (_f ("acknowledge_audio_element, added, Size of now_heads_=%d", now_heads_.size ()));
85       // Find a previous note that ties to the current note. If it exists, 
86       // remove it from the heads_to_tie vector and create the tie
87       deque<Head_event_tuple>::iterator it;
88       bool found = false;
89       Stream_event *right_mus = inf.event_;
90       for ( it = heads_to_tie_.begin() ; (!found) && (it < heads_to_tie_.end()); it++ )
91         {
92           Audio_element_info et = (*it).head_;
93           Audio_note *th = dynamic_cast<Audio_note *> (et.elem_);
94           Stream_event *left_mus = et.event_;
95
96           if (th && right_mus && left_mus
97               && ly_is_equal (right_mus->get_property ("pitch"),
98                               left_mus->get_property ("pitch")))
99             {
100               found = true;
101               Moment skip = now_mom() - (*it).moment_ - th->length_mom_;
102               an->tie_to (th, skip);
103               // this invalidates the iterator, we are leaving the loop anyway
104               heads_to_tie_.erase (it);
105             }
106         }
107     }
108 }
109
110 void
111 Tie_performer::start_translation_timestep ()
112 {
113   context ()->set_property ("tieMelismaBusy",
114                             ly_bool2scm (heads_to_tie_.size ()));
115 }
116
117 void
118 Tie_performer::stop_translation_timestep ()
119 {
120   // We might have dangling open ties like c~ d. Close them, unless we have
121   // tieWaitForNote set...
122   if (!to_boolean (get_property ("tieWaitForNote")))
123     {
124       heads_to_tie_.clear ();
125     }
126
127   if (event_)
128     {
129       for (vsize i = now_heads_.size (); i--;)
130         heads_to_tie_.push_back (now_heads_[i]);
131     }
132
133   for (vsize i = now_tied_heads_.size (); i--;)
134     heads_to_tie_.push_back (now_tied_heads_[i]);
135
136   event_ = 0;
137   now_heads_.clear ();
138   now_tied_heads_.clear ();
139 }
140
141 ADD_TRANSLATOR (Tie_performer,
142                 /* doc */
143                 "Generate ties between note heads of equal pitch.",
144
145                 /* create */
146                 "",
147
148                 /* read */
149                 "tieWaitForNote",
150
151                 /* write */
152                 "tieMelismaBusy"
153                 );