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