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