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