]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
release: 1.3.69
[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 "rhythmic-head.hh"
13 #include "musical-request.hh"
14 #include "tie.hh"
15 #include "translator-group.hh"
16 #include "tie-column.hh"
17 #include "pqueue.hh"
18 #include "engraver.hh"
19 #include "item.hh"
20
21 struct CHead_melodic_tuple {
22   Melodic_req *req_l_ ;
23   Score_element *head_l_;
24   Moment end_;
25   CHead_melodic_tuple ();
26   CHead_melodic_tuple (Score_element*, Melodic_req*, Moment);
27   static int pitch_compare (CHead_melodic_tuple const &, CHead_melodic_tuple const &);
28   static int time_compare (CHead_melodic_tuple const &, CHead_melodic_tuple const &);  
29 };
30
31 inline int compare (CHead_melodic_tuple const &a, CHead_melodic_tuple const &b)
32 {
33   return CHead_melodic_tuple::time_compare (a,b);
34 }
35
36
37 /**
38    Manufacture ties.  Acknowledge noteheads, and put them into a
39    priority queue. If we have a Tie_req, connect the notes that finish
40    just at this time, and note that start at this time.
41
42    TODO: junk the pq.
43  */
44 class Tie_engraver : public Engraver
45 {
46   PQueue<CHead_melodic_tuple> past_notes_pq_;
47   Tie_req *req_l_;
48   Array<CHead_melodic_tuple> now_heads_;
49   Array<CHead_melodic_tuple> stopped_heads_;
50   Link_array<Tie> tie_p_arr_;
51
52   Tie_column * tie_column_p_;
53   
54   void set_melisma (bool);
55   
56 protected:
57   virtual void do_post_move_processing ();
58   virtual void do_pre_move_processing ();
59   virtual void acknowledge_element (Score_element_info);
60   virtual bool do_try_music (Music*);
61   virtual void do_process_music ();
62   virtual void process_acknowledged ();
63   void typeset_tie (Score_element*);
64 public:
65   VIRTUAL_COPY_CONS(Translator);
66   Tie_engraver();
67 };
68
69
70
71 Tie_engraver::Tie_engraver()
72 {
73   req_l_ = 0;
74   tie_column_p_ = 0;
75 }
76
77
78 bool
79 Tie_engraver::do_try_music (Music *m)
80 {
81   if (Tie_req * c = dynamic_cast<Tie_req*> (m))
82     {
83       req_l_ = c;
84       SCM m = get_property ("automaticMelismata");
85       bool am = gh_boolean_p (m) &&gh_scm2bool (m);
86       if (am)
87         {
88           set_melisma (true);
89         }
90       return true;
91     }
92   return false;
93 }
94
95 void
96 Tie_engraver::set_melisma (bool m)
97 {
98   daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
99 }
100
101 void
102 Tie_engraver::acknowledge_element (Score_element_info i)
103 {
104   if (Rhythmic_head::has_interface (i.elem_l_))
105     {
106       Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
107       if (!m)
108         return;
109       now_heads_.push (CHead_melodic_tuple (i.elem_l_, m, now_mom()+ m->length_mom ()));
110     }
111 }
112
113 void
114 Tie_engraver::do_process_music ()
115 {
116   if (req_l_)
117     {
118       Moment now = now_mom ();
119       Link_array<Note_head> nharr;
120       
121       stopped_heads_.clear ();
122       while (past_notes_pq_.size ()
123              && past_notes_pq_.front ().end_ == now)
124         stopped_heads_.push (past_notes_pq_.get ());
125     }
126 }
127
128 void
129 Tie_engraver::process_acknowledged ()
130 {
131   if (req_l_)
132     {
133       now_heads_.sort (CHead_melodic_tuple::pitch_compare);
134       stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
135
136       SCM head_list = SCM_EOL;
137       
138       int j = stopped_heads_.size ()-1;
139       int i = now_heads_.size ()-1;
140
141       while  (i >= 0 && j >=0)
142         {
143           int comp
144             = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
145                                       stopped_heads_[j].req_l_->pitch_);
146
147           if (comp)
148             {
149               (comp < 0) ? j -- : i--;
150               continue;
151             }
152           else
153             {
154               head_list  = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm_,
155                                              now_heads_[i].head_l_->self_scm_),
156                                     head_list);
157
158               past_notes_pq_. insert (now_heads_[i]);
159               now_heads_.del (i);
160               stopped_heads_.del (j);
161               i--;
162               j--;
163             }
164         }
165
166       SCM basic = get_property ("basicTieProperties");
167       SCM sparse = get_property ("sparseTies");
168       if (to_boolean (sparse))
169         {
170           int i = scm_ilength (head_list);
171
172           if (!i)
173             return;
174           
175           SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
176           
177           Tie * p = new Tie (basic);
178           Tie::set_head (p,LEFT, dynamic_cast<Item*> (unsmob_element (gh_car (pair))));
179           Tie::set_head (p,RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdr (pair))));
180           
181           tie_p_arr_.push (p);
182           announce_element (Score_element_info (p, req_l_));
183         }
184       else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
185         {
186           Tie * p = new Tie (basic);
187           Tie::set_interface (p);
188           
189           Tie::set_head (p, LEFT, dynamic_cast<Item*> (unsmob_element (gh_caar (s))));
190           Tie::set_head (p, RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdar (s))));
191           
192           tie_p_arr_.push (p);
193           announce_element (Score_element_info (p, req_l_));
194         }
195
196       if (!tie_p_arr_.size ())
197         {
198           req_l_->warning (_ ("No ties were created!"));
199         }
200       else if (tie_p_arr_.size () > 1 && !tie_column_p_)
201         {
202           tie_column_p_ = new Tie_column (get_property ("basicTieColumnProperties"));
203           Tie_column::set_interface (tie_column_p_);
204           for (int i = tie_p_arr_.size (); i--; )
205             Tie_column::add_tie (tie_column_p_,tie_p_arr_ [i]);
206           announce_element (Score_element_info (tie_column_p_, 0));
207         }
208     }
209 }
210
211
212 void
213 Tie_engraver::do_pre_move_processing ()
214 {
215   for (int i=0; i < now_heads_.size (); i++)
216     {
217       past_notes_pq_.insert (now_heads_[i]);
218     }
219   now_heads_.clear ();
220
221   for (int i=0; i<  tie_p_arr_.size (); i++)
222    {
223       typeset_tie (tie_p_arr_[i]);
224     }
225   tie_p_arr_.clear ();
226   if (tie_column_p_)
227     {
228       typeset_element (tie_column_p_);
229       tie_column_p_ =0;
230     }
231 }
232
233 void
234 Tie_engraver::typeset_tie (Score_element *her)
235 {
236   if (!(Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
237     warning (_ ("lonely tie"));
238
239   Direction d = LEFT;
240   Drul_array<Score_element *> new_head_drul;
241   new_head_drul[LEFT] = Tie::head(her,LEFT);
242   new_head_drul[RIGHT] = Tie::head (her,RIGHT);  
243   do {
244     if (!Tie::head (her,d))
245       new_head_drul[d] = Tie::head(her,(Direction)-d);
246   } while (flip(&d) != LEFT);
247
248   index_set_cell (her->get_elt_property ("heads"), LEFT, new_head_drul[LEFT]->self_scm_ );
249   index_set_cell (her->get_elt_property ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm_ );
250
251   typeset_element (her);
252 }
253
254 void
255 Tie_engraver::do_post_move_processing ()
256 {
257   SCM m = get_property ("automaticMelismata");
258   if (to_boolean (m))
259     {
260       set_melisma (false);
261     }
262   req_l_ = 0;
263   Moment now = now_mom ();
264   while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
265     past_notes_pq_.delmin ();
266 }
267
268 ADD_THIS_TRANSLATOR(Tie_engraver);
269
270
271 CHead_melodic_tuple::CHead_melodic_tuple ()
272 {
273   head_l_ =0;
274   req_l_ =0;
275   end_ = 0;
276 }
277
278 CHead_melodic_tuple::CHead_melodic_tuple (Score_element *h, Melodic_req*m, Moment mom)
279 {
280   head_l_ = h;
281   req_l_ = m;
282   end_ = mom;
283 }
284
285 int
286 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
287                              CHead_melodic_tuple const &h2)
288 {
289   return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
290 }
291
292 int
293 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
294                              CHead_melodic_tuple const &h2)
295 {
296   return (h1.end_ - h2.end_ ).sign ();
297 }