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