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