]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-performer.cc
(start_translation_timestep): remove first_b_
[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) 1999--2003 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9
10
11 #include "audio-item.hh"
12 #include "event.hh"
13 #include "pqueue.hh"
14 #include "performer.hh"
15
16 struct CNote_melodic_tuple {
17   Music *event_ ;
18   Audio_note *note_;
19   Moment end_;
20   CNote_melodic_tuple ();
21   CNote_melodic_tuple (Audio_note*, Music*, Moment);
22   static int pitch_compare (CNote_melodic_tuple const &, CNote_melodic_tuple const &);
23   static int time_compare (CNote_melodic_tuple const &, CNote_melodic_tuple const &);  
24 };
25
26 inline int compare (CNote_melodic_tuple const &a, CNote_melodic_tuple const &b)
27 {
28   return CNote_melodic_tuple::time_compare (a,b);
29 }
30
31
32 /**
33    Manufacture ties.  Acknowledge notes, and put them into a
34    priority queue. If we have a Music, connect the notes that finish
35    just at this time, and note that start at this time.
36
37    TODO: should share code with Tie_engraver ?
38  */
39 class Tie_performer : public Performer
40 {
41 public:
42   TRANSLATOR_DECLARATIONS(Tie_performer);
43 private:
44
45   bool ties_created_;
46   Array<CNote_melodic_tuple> now_notes_;
47   Array<CNote_melodic_tuple> tied_notes_;
48
49   Music *event_;
50   Music * prev_event_;
51   
52   Link_array<Audio_tie> ties_;
53   
54 protected:
55   virtual void start_translation_timestep ();
56   virtual void stop_translation_timestep ();
57   virtual void acknowledge_audio_element (Audio_element_info);
58   virtual bool try_music (Music*);
59   virtual void create_audio_elements ();
60 };
61
62
63 Tie_performer::Tie_performer ()
64 {
65   event_ = 0;
66   ties_created_ = false;
67   prev_event_ = 0;
68 }
69
70 ENTER_DESCRIPTION (Tie_performer, "", "",
71                    "tie-event",
72                    "", "", "");
73
74
75
76 bool
77 Tie_performer::try_music (Music *m)
78 {
79   if (!event_)
80     {
81       event_ = m;
82       return true;
83     }
84   return false;
85 }
86
87 void
88 Tie_performer::acknowledge_audio_element (Audio_element_info i)
89 {
90   if (Audio_note *nh = dynamic_cast<Audio_note *> (i.elem_))
91     {
92       Music *m = i.event_;
93       if (m->is_mus_type ("note-event"))
94         now_notes_.push (CNote_melodic_tuple (nh, m, now_mom ()+ m->get_length ()));
95     }
96 }
97
98 void
99 Tie_performer::create_audio_elements ()
100 {
101   /*
102     This is a nested loop. Not optimal, but good enough.
103    */
104   if (tied_notes_.size ())
105     {
106       Moment now = now_mom();
107       for (int i = tied_notes_.size (); i--; )
108         {
109           if (tied_notes_[i].end_ != now)
110             continue;
111
112           for (int j = now_notes_.size(); j--;)
113             {
114               int comp
115                 = Pitch::compare (*unsmob_pitch (tied_notes_[i].event_->get_mus_property ("pitch")),
116                                   *unsmob_pitch (now_notes_[j].event_->get_mus_property ("pitch")));
117
118               if (comp == 0)
119                 {
120                   
121                   Audio_tie * p = new Audio_tie;
122                   p->set_note (LEFT, tied_notes_[i].note_);
123                   p->set_note (RIGHT, now_notes_[j].note_);
124                   ties_.push (p);
125                   announce_element (Audio_element_info (p, event_));
126                   ties_created_ = true;
127
128                   tied_notes_.del (i);
129                   break ; 
130                 }
131             }
132         }
133     }
134 }
135
136
137 void
138 Tie_performer::stop_translation_timestep ()
139 {
140   if (prev_event_ && tied_notes_.size () && !ties_.size ()
141       && now_notes_.size ())
142     {
143       prev_event_->origin ()->warning (_ ("No ties were performed."));
144     }
145
146   if (ties_created_)
147     {
148       prev_event_ = 0;
149       tied_notes_.clear();
150     }
151   
152   if (event_)
153     {
154       tied_notes_ = now_notes_ ;
155       prev_event_ = event_;
156     }
157
158   event_ = 0;
159   now_notes_ .clear ();
160
161   for (int i=ties_.size (); i--;)
162     {
163       ties_[i]->note_drul_[RIGHT]->tie_to (ties_[i]->note_drul_[LEFT]);
164     }
165   
166   ties_.clear ();
167 }
168
169 void
170 Tie_performer::start_translation_timestep ()
171 {
172   event_ =0;
173   ties_created_ = false;
174   Moment now = now_mom ();
175   for (int i= tied_notes_.size (); i-- ;)
176     {
177       if (tied_notes_[i].end_ < now)
178         tied_notes_.del (i);
179       else
180         break ;
181     }
182 }
183
184
185 CNote_melodic_tuple::CNote_melodic_tuple ()
186 {
187   note_ =0;
188   event_ =0;
189   end_ = 0;
190 }
191
192 CNote_melodic_tuple::CNote_melodic_tuple (Audio_note *h, Music*m, Moment mom)
193 {
194   note_ = h;
195   event_ = m;
196   end_ = mom;
197 }
198
199 int
200 CNote_melodic_tuple::pitch_compare (CNote_melodic_tuple const&h1,
201                                     CNote_melodic_tuple const &h2)
202 {
203   SCM p1  = h1.event_->get_mus_property ("pitch");
204   SCM p2  = h2.event_->get_mus_property ("pitch");  
205   return Pitch::compare (*unsmob_pitch (p1),
206                                *unsmob_pitch (p2));
207 }
208
209 int
210 CNote_melodic_tuple::time_compare (CNote_melodic_tuple const&h1,
211                                    CNote_melodic_tuple const &h2)
212 {
213   return (h1.end_ - h2.end_).main_part_.sign ();
214 }
215