]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
29f662c5f00eb4c9b65b86da5fe1b8d12ed6efe9
[lilypond.git] / lily / tie-engraver.cc
1 /*   
2   ctie-engraver.cc --  implement Tie_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "tie-engraver.hh"
11 #include "command-request.hh"
12 #include "note-head.hh"
13 #include "musical-request.hh"
14 #include "tie.hh"
15 #include "translator-group.hh"
16
17 Tie_engraver::Tie_engraver()
18 {
19   req_l_ = 0;
20 }
21
22
23 bool
24 Tie_engraver::do_try_music (Music *m)
25 {
26   if (Tie_req * c = dynamic_cast<Tie_req*> (m))
27     {
28       req_l_ = c;
29       SCM m = get_property ("automaticMelismata",0);
30       bool am = gh_boolean_p (m) &&gh_scm2bool (m);
31       if (am)
32         {
33           set_melisma (true);
34         }
35       return true;
36     }
37   return false;
38 }
39
40 void
41 Tie_engraver::set_melisma (bool m)
42 {
43   Translator_group *where = daddy_trans_l_;
44   get_property ("tieMelismaBusy", &where);
45   if (!where)
46     where = daddy_trans_l_;
47     
48   daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
49 }
50
51 void
52 Tie_engraver::acknowledge_element (Score_element_info i)
53 {
54   if (Note_head *nh = dynamic_cast<Note_head *> (i.elem_l_))
55     {
56       Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
57       if (!m)
58         return;
59       now_heads_.push (CHead_melodic_tuple (nh, m, now_mom()+ m->length_mom ()));
60     }
61 }
62
63 void
64 Tie_engraver::do_process_requests ()
65 {
66   if (req_l_)
67     {
68       Moment now = now_mom ();
69       Link_array<Note_head> nharr;
70       
71       stopped_heads_.clear ();
72       while (past_notes_pq_.size ()
73              && past_notes_pq_.front ().end_ == now)
74         stopped_heads_.push (past_notes_pq_.get ());
75     }
76 }
77
78 void
79 Tie_engraver::process_acknowledged ()
80 {
81   if (req_l_)
82     {
83       now_heads_.sort (CHead_melodic_tuple::pitch_compare);
84       stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
85
86       SCM head_list = SCM_EOL;
87       
88       int j = stopped_heads_.size ()-1;
89       int i = now_heads_.size ()-1;
90
91       while  (i >= 0 && j >=0)
92         {
93           int comp
94             = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
95                                       stopped_heads_[j].req_l_->pitch_);
96
97           if (comp)
98             {
99               (comp < 0) ? j -- : i--;
100               continue;
101             }
102           else
103             {
104               head_list  = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm_,
105                                              now_heads_[i].head_l_->self_scm_),
106                                     head_list);
107
108               past_notes_pq_. insert (now_heads_[i]);
109               now_heads_.del (i);
110               stopped_heads_.del (j);
111               i--;
112               j--;
113             }
114         }
115
116
117       SCM sparse = get_property ("sparseTies", 0);
118       if (to_boolean (sparse))
119         {
120           int i = scm_ilength (head_list);
121
122           if (!i)
123             return;
124           
125           SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
126           
127           Tie * p = new Tie;
128           p->set_head (LEFT, dynamic_cast<Item*> (unsmob_element (gh_car (pair))));
129           p->set_head (RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdr (pair))));
130           
131           tie_p_arr_.push (p);
132           announce_element (Score_element_info (p, req_l_));
133         }
134       else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
135         {
136           Tie * p = new Tie;
137           p->set_head (LEFT, dynamic_cast<Item*> (unsmob_element (gh_caar (s))));
138           p->set_head (RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdar (s))));
139           
140           tie_p_arr_.push (p);
141           announce_element (Score_element_info (p, req_l_));
142         }
143
144       if (!tie_p_arr_.size ())
145         {
146           req_l_->warning (_ ("No ties were created!"));
147         }
148           
149     }
150 }
151
152
153 void
154 Tie_engraver::do_pre_move_processing ()
155 {
156   for (int i=0; i < now_heads_.size (); i++)
157     {
158       past_notes_pq_.insert (now_heads_[i]);
159     }
160   now_heads_.clear ();
161
162   for (int i=0; i<  tie_p_arr_.size (); i++)
163    {
164       typeset_element (tie_p_arr_[i]);
165     }
166   tie_p_arr_.clear ();
167 }
168
169 void
170 Tie_engraver::do_post_move_processing ()
171 {
172   SCM m = get_property ("automaticMelismata",0);
173   if (to_boolean (m))
174     {
175       set_melisma (false);
176     }
177   req_l_ = 0;
178   Moment now = now_mom ();
179   while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
180     past_notes_pq_.delmin ();
181 }
182
183 ADD_THIS_TRANSLATOR(Tie_engraver);
184
185
186 CHead_melodic_tuple::CHead_melodic_tuple ()
187 {
188   head_l_ =0;
189   req_l_ =0;
190   end_ = 0;
191 }
192
193 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
194 {
195   head_l_ = h;
196   req_l_ = m;
197   end_ = mom;
198 }
199
200 int
201 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
202                              CHead_melodic_tuple const &h2)
203 {
204   return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
205 }
206
207 int
208 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
209                              CHead_melodic_tuple const &h2)
210 {
211   return (h1.end_ - h2.end_ ).sign ();
212 }