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