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