]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
release: 1.3.131
[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--2001 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   Grob *head_l_;
25   Moment end_;
26   CHead_melodic_tuple ();
27   CHead_melodic_tuple (Grob*, 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; the PQ is overkill if we assume that no
44    different durations occur in parallel.
45 */
46 class Tie_engraver : public Engraver
47 {
48   PQueue<CHead_melodic_tuple> past_notes_pq_;
49   Moment end_mom_;
50   Moment next_end_mom_;
51
52   Tie_req *req_l_;
53   Array<CHead_melodic_tuple> now_heads_;
54   Array<CHead_melodic_tuple> stopped_heads_;
55   Link_array<Grob> tie_p_arr_;
56
57   Spanner * tie_column_p_;
58   
59   void set_melisma (bool);
60   
61 protected:
62   virtual void start_translation_timestep ();
63   virtual void stop_translation_timestep ();
64   virtual void acknowledge_grob (Grob_info);
65   virtual bool try_music (Music*);
66   virtual void create_grobs ();
67   void typeset_tie (Grob*);
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::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_grob (Grob_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
121 void
122 Tie_engraver::create_grobs ()
123 {
124   if (req_l_)
125     {
126       now_heads_.sort (CHead_melodic_tuple::pitch_compare);
127       stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
128
129       SCM head_list = SCM_EOL;
130       
131       int j = stopped_heads_.size ()-1;
132       int i = now_heads_.size ()-1;
133
134       while  (i >= 0 && j >=0)
135         {
136           int comp
137             = Pitch::compare (*unsmob_pitch (now_heads_[i].req_l_->get_mus_property ("pitch") ),
138                                       *unsmob_pitch (stopped_heads_[j].req_l_->get_mus_property ("pitch")));
139
140           if (comp)
141             {
142               (comp < 0) ? j -- : i--;
143               continue;
144             }
145           else
146             {
147               head_list  = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm (),
148                                              now_heads_[i].head_l_->self_scm ()),
149                                     head_list);
150
151               past_notes_pq_. insert (now_heads_[i]);
152               now_heads_.del (i);
153               stopped_heads_.del (j);
154               i--;
155               j--;
156             }
157         }
158
159       SCM basic = get_property ("Tie");
160       SCM sparse = get_property ("sparseTies");
161       if (to_boolean (sparse))
162         {
163           int i = scm_ilength (head_list);
164
165           if (!i)
166             return;
167           
168           SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
169           
170           Spanner * p = new Spanner (basic);
171           Tie::set_head (p,LEFT, dynamic_cast<Item*> (unsmob_grob (gh_car (pair))));
172           Tie::set_head (p,RIGHT, dynamic_cast<Item*> (unsmob_grob (gh_cdr (pair))));
173           
174           tie_p_arr_.push (p);
175           announce_grob (p, req_l_);
176         }
177       else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
178         {
179           Grob * p = new Spanner (basic);
180           Tie::set_interface (p);
181           
182           Tie::set_head (p, LEFT, dynamic_cast<Item*> (unsmob_grob (gh_caar (s))));
183           Tie::set_head (p, RIGHT, dynamic_cast<Item*> (unsmob_grob (gh_cdar (s))));
184           
185           tie_p_arr_.push (p);
186           announce_grob (p, req_l_);
187         }
188
189       if (tie_p_arr_.size () > 1 && !tie_column_p_)
190         {
191           tie_column_p_ = new Spanner (get_property ("TieColumn"));
192           Tie_column::set_interface (tie_column_p_);
193           for (int i = tie_p_arr_.size (); i--; )
194             Tie_column::add_tie (tie_column_p_,tie_p_arr_ [i]);
195           announce_grob (tie_column_p_, 0);
196         }
197     }
198 }
199
200
201 void
202 Tie_engraver::stop_translation_timestep ()
203 {
204   for (int i=0; i < now_heads_.size (); i++)
205     {
206       past_notes_pq_.insert (now_heads_[i]);
207     }
208   now_heads_.clear ();
209
210   if (req_l_ && !tie_p_arr_.size ())
211     {
212       req_l_->origin ()->warning (_ ("No ties were created!"));
213     }
214   
215   for (int i=0; i<  tie_p_arr_.size (); i++)
216    {
217       typeset_tie (tie_p_arr_[i]);
218     }
219   tie_p_arr_.clear ();
220   if (tie_column_p_)
221     {
222       typeset_grob (tie_column_p_);
223       tie_column_p_ =0;
224     }
225 }
226
227 void
228 Tie_engraver::typeset_tie (Grob *her)
229 {
230   if (!(Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
231     warning (_ ("lonely tie"));
232
233   Direction d = LEFT;
234   Drul_array<Grob *> new_head_drul;
235   new_head_drul[LEFT] = Tie::head(her,LEFT);
236   new_head_drul[RIGHT] = Tie::head (her,RIGHT);  
237   do {
238     if (!Tie::head (her,d))
239       new_head_drul[d] = Tie::head(her,(Direction)-d);
240   } while (flip(&d) != LEFT);
241
242   index_set_cell (her->get_grob_property ("heads"), LEFT, new_head_drul[LEFT]->self_scm () );
243   index_set_cell (her->get_grob_property ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm () );
244
245   typeset_grob (her);
246 }
247
248 void
249 Tie_engraver::start_translation_timestep ()
250 {
251   SCM m = get_property ("automaticMelismata");
252   if (to_boolean (m))
253     {
254       set_melisma (false);
255     }
256   req_l_ = 0;
257   Moment now = now_mom ();
258   while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
259     past_notes_pq_.delmin ();
260
261
262   stopped_heads_.clear ();
263   while (past_notes_pq_.size ()
264          && past_notes_pq_.front ().end_ == now)
265     stopped_heads_.push (past_notes_pq_.get ());
266
267 }
268
269 ADD_THIS_TRANSLATOR(Tie_engraver);
270
271
272 CHead_melodic_tuple::CHead_melodic_tuple ()
273 {
274   head_l_ =0;
275   req_l_ =0;
276   end_ = 0;
277 }
278
279 CHead_melodic_tuple::CHead_melodic_tuple (Grob *h, Melodic_req*m, Moment mom)
280 {
281   head_l_ = h;
282   req_l_ = m;
283   end_ = mom;
284 }
285
286 /*
287   signed compare, should use pitch<? 
288  */
289 int
290 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
291                              CHead_melodic_tuple const &h2)
292 {
293   SCM p1  = h1.req_l_->get_mus_property ("pitch");
294   SCM p2  = h2.req_l_->get_mus_property ("pitch");
295   
296   return Pitch::compare (*unsmob_pitch (p1),
297                          *unsmob_pitch (p2));
298 }
299
300 int
301 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
302                                    CHead_melodic_tuple const &h2)
303 {
304   return (h1.end_ - h2.end_ ).sign ();
305 }