]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-performer.cc
release: 1.5.13
[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--2001 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9
10 #include "command-request.hh"
11 #include "audio-item.hh"
12 #include "musical-request.hh"
13 #include "pqueue.hh"
14 #include "performer.hh"
15
16 struct CNote_melodic_tuple {
17   Melodic_req *req_l_ ;
18   Audio_note *note_l_;
19   Moment end_;
20   CNote_melodic_tuple ();
21   CNote_melodic_tuple (Audio_note*, Melodic_req*, 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 Tie_req, 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   bool done_;
45   PQueue<CNote_melodic_tuple> past_notes_pq_;
46   Tie_req *req_l_;
47   Array<CNote_melodic_tuple> now_notes_;
48   Array<CNote_melodic_tuple> stopped_notes_;
49   Link_array<Audio_tie> tie_p_arr_;
50   
51 protected:
52   virtual void initialize ();
53   virtual void start_translation_timestep ();
54   virtual void stop_translation_timestep ();
55   virtual void acknowledge_audio_element (Audio_element_info);
56   virtual bool try_music (Music*);
57   virtual void create_audio_elements ();
58 };
59
60
61 Tie_performer::Tie_performer ()
62 {
63   req_l_ = 0;
64   done_ = false;
65 }
66
67 ENTER_DESCRIPTION (Tie_performer, "", "", "", "", "");
68
69
70 void
71 Tie_performer::initialize ()
72 {
73   req_l_ = 0;
74 }
75
76
77 bool
78 Tie_performer::try_music (Music *m)
79 {
80   if (!req_l_)
81     {
82       if (Tie_req * c = dynamic_cast<Tie_req*> (m))
83         {
84           req_l_ = c;
85           return true;
86         }
87     }
88   return false;
89 }
90
91 void
92 Tie_performer::acknowledge_audio_element (Audio_element_info i)
93 {
94   if (Audio_note *nh = dynamic_cast<Audio_note *> (i.elem_l_))
95     {
96       Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
97       if (!m)
98         return;
99       now_notes_.push (CNote_melodic_tuple (nh, m, now_mom ()+ m->length_mom ()));
100     }
101 }
102
103 void
104 Tie_performer::create_audio_elements ()
105 {
106   if (req_l_ && ! done_)
107     {
108       Moment now = now_mom ();
109       Link_array<Audio_note> nharr;
110       
111       stopped_notes_.clear ();
112       while (past_notes_pq_.size ()
113              && past_notes_pq_.front ().end_ == now)
114         stopped_notes_.push (past_notes_pq_.get ());
115       done_ = true;
116       return;
117     }
118
119   if (req_l_)
120     {
121       now_notes_.sort (CNote_melodic_tuple::pitch_compare);
122       stopped_notes_.sort (CNote_melodic_tuple::pitch_compare);
123       int i=0;
124       int j=0;
125       int tie_count=0;
126       while (i < now_notes_.size () && j < stopped_notes_.size ())
127         {
128           int comp
129             = Pitch::compare (*unsmob_pitch (now_notes_[i].req_l_->get_mus_property ("pitch")),
130                                       *unsmob_pitch (stopped_notes_[j].req_l_->get_mus_property ("pitch")));
131
132           if (comp)
133             {
134  (comp < 0) ? i ++ : j++;
135               continue;
136             }
137           else
138             {
139               tie_count ++;
140
141               /* don't go around recreating ties that were already
142                  made. Not infallible. Due to reordering in sort (),
143                  we will make the wrong ties when notenotes are
144                  added.  */
145               if (tie_count > tie_p_arr_.size ())
146                 {
147                   Audio_tie * p = new Audio_tie;
148                   p->set_note (LEFT, stopped_notes_[j].note_l_);
149                   p->set_note (RIGHT, now_notes_[i].note_l_);
150                   tie_p_arr_.push (p);
151                       announce_element (Audio_element_info (p, req_l_));
152                 }
153               i++;
154               j++;
155               
156             }
157         }
158       
159       if (!tie_p_arr_.size ())
160         {
161           req_l_->origin ()->warning (_ ("No ties were created!"));
162         }
163     }
164 }
165
166
167 void
168 Tie_performer::stop_translation_timestep ()
169 {
170   for (int i=0; i < now_notes_.size (); i++)
171     {
172       past_notes_pq_.insert (now_notes_[i]);
173     }
174   now_notes_.clear ();
175
176   for (int i=0; i<  tie_p_arr_.size (); i++)
177    {
178      //play_element (tie_p_arr_[i]);
179      tie_p_arr_[i]->note_l_drul_[RIGHT]->tie_to (tie_p_arr_[i]->note_l_drul_[LEFT]);
180    }
181   tie_p_arr_.clear ();
182 }
183
184 void
185 Tie_performer::start_translation_timestep ()
186 {
187   req_l_ =0;
188   done_ = false;
189   Moment now = now_mom ();
190   while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
191     past_notes_pq_.delmin ();
192 }
193
194
195 CNote_melodic_tuple::CNote_melodic_tuple ()
196 {
197   note_l_ =0;
198   req_l_ =0;
199   end_ = 0;
200 }
201
202 CNote_melodic_tuple::CNote_melodic_tuple (Audio_note *h, Melodic_req*m, Moment mom)
203 {
204   note_l_ = h;
205   req_l_ = m;
206   end_ = mom;
207 }
208
209 int
210 CNote_melodic_tuple::pitch_compare (CNote_melodic_tuple const&h1,
211                                     CNote_melodic_tuple const &h2)
212 {
213   SCM p1  = h1.req_l_->get_mus_property ("pitch");
214   SCM p2  = h2.req_l_->get_mus_property ("pitch");  
215   return Pitch::compare (*unsmob_pitch (p1),
216                                *unsmob_pitch (p2));
217 }
218
219 int
220 CNote_melodic_tuple::time_compare (CNote_melodic_tuple const&h1,
221                                    CNote_melodic_tuple const &h2)
222 {
223   return (h1.end_ - h2.end_).main_part_.sign ();
224 }
225