]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-performer.cc
a05b54cce6464150adc57d94326c56d2df95e950
[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 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9
10 #include "tie-performer.hh"
11 #include "command-request.hh"
12 #include "audio-item.hh"
13 #include "musical-request.hh"
14
15
16 ADD_THIS_TRANSLATOR (Tie_performer);
17
18 Tie_performer::Tie_performer()
19 {
20   req_l_ = 0;
21 }
22
23 bool
24 Tie_performer::do_try_music (Music *m)
25 {
26   if (Tie_req * c = dynamic_cast<Tie_req*> (m))
27     {
28       req_l_ = c;
29       return true;
30     }
31   return false;
32 }
33
34 void
35 Tie_performer::acknowledge_element (Audio_element_info i)
36 {
37   if (Audio_note *nh = dynamic_cast<Audio_note *> (i.elem_l_))
38     {
39       Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
40       if (!m)
41         return;
42       now_notes_.push (CNote_melodic_tuple (nh, m, now_mom()+ m->length_mom ()));
43     }
44 }
45
46 void
47 Tie_performer::do_process_requests ()
48 {
49   if (req_l_)
50     {
51       Moment now = now_mom ();
52       Link_array<Audio_note> nharr;
53       
54       stopped_notes_.clear ();
55       while (past_notes_pq_.size ()
56              && past_notes_pq_.front ().end_ == now)
57         stopped_notes_.push (past_notes_pq_.get ());
58     }
59 }
60
61 void
62 Tie_performer::process_acknowledged ()
63 {
64   if (req_l_)
65     {
66       now_notes_.sort (CNote_melodic_tuple::pitch_compare);
67       stopped_notes_.sort(CNote_melodic_tuple::pitch_compare);
68       int i=0;
69       int j=0;
70       int tie_count=0;
71       while  ( i < now_notes_.size () && j < stopped_notes_.size ())
72         {
73           int comp
74             = Musical_pitch::compare (now_notes_[i].req_l_->pitch_ ,
75                                       stopped_notes_[j].req_l_->pitch_);
76
77           if (comp)
78             {
79               (comp < 0) ? i ++ : j++;
80               continue;
81             }
82           else
83             {
84               tie_count ++;
85
86               /* don't go around recreating ties that were already
87                  made. Not infallible. Due to reordering in sort (),
88                  we will make the wrong ties when notenotes are
89                  added.  */
90               if (tie_count > tie_p_arr_.size ())
91                 {
92                   Audio_tie * p = new Audio_tie;
93                   p->set_note (LEFT, stopped_notes_[j].note_l_);
94                   p->set_note (RIGHT, now_notes_[i].note_l_);
95                   tie_p_arr_.push (p);
96                       announce_element (Audio_element_info (p, req_l_));
97                 }
98               i++;
99               j++;
100               
101             }
102         }
103       
104       if (!tie_p_arr_.size ())
105         {
106           req_l_->warning (_("No ties were created!"));
107         }
108       
109     }
110 }
111
112 void
113 Tie_performer::do_pre_move_processing ()
114 {
115   for (int i=0; i < now_notes_.size (); i++)
116     {
117       past_notes_pq_.insert (now_notes_[i]);
118     }
119   now_notes_.clear ();
120
121   for (int i=0; i<  tie_p_arr_.size (); i++)
122    {
123      //play_element (tie_p_arr_[i]);
124      tie_p_arr_[i]->note_l_drul_[RIGHT]->tie_to (tie_p_arr_[i]->note_l_drul_[LEFT]);
125    }
126   tie_p_arr_.clear ();
127 }
128
129 void
130 Tie_performer::do_post_move_processing ()
131 {
132   req_l_ =0;
133   Moment now = now_mom ();
134   while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
135     past_notes_pq_.delmin ();
136 }
137
138
139 CNote_melodic_tuple::CNote_melodic_tuple ()
140 {
141   note_l_ =0;
142   req_l_ =0;
143   end_ = 0;
144 }
145
146 CNote_melodic_tuple::CNote_melodic_tuple (Audio_note *h, Melodic_req*m, Moment mom)
147 {
148   note_l_ = h;
149   req_l_ = m;
150   end_ = mom;
151 }
152
153 int
154 CNote_melodic_tuple::pitch_compare (CNote_melodic_tuple const&h1,
155                                     CNote_melodic_tuple const &h2)
156 {
157   return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
158 }
159
160 int
161 CNote_melodic_tuple::time_compare (CNote_melodic_tuple const&h1,
162                                    CNote_melodic_tuple const &h2)
163 {
164   return (h1.end_ - h2.end_ ).sign ();
165 }