]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-performer.cc
3ca52b6c4920b9c42d8306b8719fca40ada12ac5
[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 initialize ();
56   virtual void start_translation_timestep ();
57   virtual void stop_translation_timestep ();
58   virtual void acknowledge_audio_element (Audio_element_info);
59   virtual bool try_music (Music*);
60   virtual void create_audio_elements ();
61 };
62
63
64 Tie_performer::Tie_performer ()
65 {
66   event_ = 0;
67   ties_created_ = false;
68 }
69
70 ENTER_DESCRIPTION (Tie_performer, "", "",
71                    "tie-event",
72                    "", "", "");
73
74
75 void
76 Tie_performer::initialize ()
77 {
78   event_ = 0;
79 }
80
81
82 bool
83 Tie_performer::try_music (Music *m)
84 {
85   if (!event_)
86     {
87       event_ = m;
88       return true;
89     }
90   return false;
91 }
92
93 void
94 Tie_performer::acknowledge_audio_element (Audio_element_info i)
95 {
96   if (Audio_note *nh = dynamic_cast<Audio_note *> (i.elem_))
97     {
98       Music *m = i.event_;
99       if (m->is_mus_type ("note-event"))
100         now_notes_.push (CNote_melodic_tuple (nh, m, now_mom ()+ m->get_length ()));
101     }
102 }
103
104 void
105 Tie_performer::create_audio_elements ()
106 {
107   /*
108     This is a nested loop. Not optimal, but good enough.
109    */
110   if (tied_notes_.size ())
111     {
112       Moment now = now_mom();
113       for (int i = tied_notes_.size (); i--; )
114         {
115           if (tied_notes_[i].end_ != now)
116             continue;
117
118           for (int j = now_notes_.size(); j--;)
119             {
120               int comp
121                 = Pitch::compare (*unsmob_pitch (tied_notes_[i].event_->get_mus_property ("pitch")),
122                                   *unsmob_pitch (now_notes_[j].event_->get_mus_property ("pitch")));
123
124               if (comp == 0)
125                 {
126                   
127                   Audio_tie * p = new Audio_tie;
128                   p->set_note (LEFT, tied_notes_[i].note_);
129                   p->set_note (RIGHT, now_notes_[j].note_);
130                   ties_.push (p);
131                   announce_element (Audio_element_info (p, event_));
132                   ties_created_ = true;
133
134                   tied_notes_.del (i);
135                   break ; 
136                 }
137             }
138         }
139     }
140 }
141
142
143 void
144 Tie_performer::stop_translation_timestep ()
145 {
146   if (prev_event_ && tied_notes_.size () && !ties_.size ()
147       && now_notes_.size ())
148     {
149       prev_event_->origin ()->warning (_ ("No ties were performed."));
150     }
151
152   if (ties_created_)
153     {
154       prev_event_ = 0;
155       tied_notes_.clear();
156     }
157   
158   if (event_)
159     {
160       tied_notes_ = now_notes_ ;
161       prev_event_ = event_;
162     }
163
164   event_ = 0;
165   now_notes_ .clear ();
166
167   for (int i=ties_.size (); i--;)
168     {
169       ties_[i]->note_drul_[RIGHT]->tie_to (ties_[i]->note_drul_[LEFT]);
170     }
171   
172   ties_.clear ();
173 }
174
175 void
176 Tie_performer::start_translation_timestep ()
177 {
178   event_ =0;
179   ties_created_ = false;
180   Moment now = now_mom ();
181   for (int i= tied_notes_.size (); i-- ;)
182     {
183       if (tied_notes_[i].end_ < now)
184         tied_notes_.del (i);
185       else
186         break ;
187     }
188 }
189
190
191 CNote_melodic_tuple::CNote_melodic_tuple ()
192 {
193   note_ =0;
194   event_ =0;
195   end_ = 0;
196 }
197
198 CNote_melodic_tuple::CNote_melodic_tuple (Audio_note *h, Music*m, Moment mom)
199 {
200   note_ = h;
201   event_ = m;
202   end_ = mom;
203 }
204
205 int
206 CNote_melodic_tuple::pitch_compare (CNote_melodic_tuple const&h1,
207                                     CNote_melodic_tuple const &h2)
208 {
209   SCM p1  = h1.event_->get_mus_property ("pitch");
210   SCM p2  = h2.event_->get_mus_property ("pitch");  
211   return Pitch::compare (*unsmob_pitch (p1),
212                                *unsmob_pitch (p2));
213 }
214
215 int
216 CNote_melodic_tuple::time_compare (CNote_melodic_tuple const&h1,
217                                    CNote_melodic_tuple const &h2)
218 {
219   return (h1.end_ - h2.end_).main_part_.sign ();
220 }
221