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