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