]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
*** empty log message ***
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10
11 #include "event.hh"
12 #include "tie.hh"
13 #include "translator-group.hh"
14 #include "spanner.hh"
15 #include "tie-column.hh"
16 #include "engraver.hh"
17 #include "item.hh"
18 #include "grob-pitch-tuple.hh"
19 #include "warn.hh"
20 #include "note-head.hh"
21
22 /**
23    Manufacture ties.  Acknowledge noteheads, and put them into a
24    priority queue. If we have a TieEvent, connect the notes that finish
25    just at this time, and note that start at this time.
26
27    TODO: Remove the dependency on musical info. We should tie on the
28    basis of position and duration-log of the heads (not of the events).
29
30    New tie event happens at the time of the first note, the 
31 */
32 class Tie_engraver : public Engraver
33 {
34   Moment end_mom_;
35   Moment next_end_mom_;
36
37   Music *event_;
38   
39   Link_array<Grob> now_heads_;
40   Link_array<Grob> stopped_heads_;
41   Link_array<Grob> ties_;
42
43   Spanner * tie_column_;
44   
45   void set_melisma (bool);
46   
47 protected:
48   virtual void start_translation_timestep ();
49   virtual void stop_translation_timestep ();
50   virtual void acknowledge_grob (Grob_info);
51   virtual bool try_music (Music*);
52   virtual void process_acknowledged_grobs ();
53   void typeset_tie (Grob*);
54 public:
55   TRANSLATOR_DECLARATIONS(Tie_engraver);
56 };
57
58
59
60 Tie_engraver::Tie_engraver ()
61 {
62   event_ = 0;
63   tie_column_ = 0;
64 }
65
66
67 bool
68 Tie_engraver::try_music (Music *mus)
69 {
70   if(mus->is_mus_type( "tie-event"))
71     {
72       event_ = mus;
73     }
74
75   if (event_)
76     {
77       SCM m = get_property ("automaticMelismata");
78       bool am = gh_boolean_p (m) &&gh_scm2bool (m);
79       if (am)
80         {
81           set_melisma (true);
82         }
83     }
84   return true;
85 }
86
87 void
88 Tie_engraver::set_melisma (bool m)
89 {
90   daddy_trans_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
91 }
92
93 void
94 Tie_engraver::acknowledge_grob (Grob_info i)
95 {
96   if (Note_head::has_interface (i.grob_))
97     {
98       now_heads_.push (i.grob_);
99     }
100 }
101
102 int
103 head_pitch_compare (Grob  *const&a,Grob  *const&b)
104 {
105   Music *m1 =unsmob_music (a->get_grob_property ("cause"));
106   Music *m2 =unsmob_music (b->get_grob_property ("cause"));  
107
108   return Pitch::compare (* unsmob_pitch (m1->get_mus_property ("pitch")),
109                          * unsmob_pitch (m2->get_mus_property ("pitch")));
110 }
111
112 void
113 Tie_engraver::process_acknowledged_grobs ()
114 {
115   if (event_)
116     {
117       now_heads_.sort (&head_pitch_compare);
118       /*
119         We could sort stopped_heads_ as well (and use a linear alg. in
120         stead of nested loop), but we'd have to use a stable sorting
121         algorithm, since the ordering of the stopped heads (of the
122         same pitch) is relevant.
123        */
124
125       SCM head_list = SCM_EOL;
126       
127       for (int i = now_heads_.size(); i--;)
128         {
129           for (int j = stopped_heads_.size(); j--;)
130             {
131               int comp
132                 = head_pitch_compare (now_heads_[i], stopped_heads_[j]);
133
134               if (!comp)
135                 {
136                   head_list  = gh_cons (gh_cons (stopped_heads_[j]->self_scm (),
137                                                  now_heads_[i]->self_scm ()),
138                                         head_list);
139
140                   now_heads_.del (i);
141                   stopped_heads_.del (j);
142                   break ;
143                 }
144             }
145         }
146      
147       SCM basic = get_property ("Tie");
148       SCM sparse = get_property ("sparseTies");
149       if (to_boolean (sparse))
150         {
151           int i = scm_ilength (head_list);
152
153           if (!i)
154             return;
155           
156           SCM pair = scm_list_ref (head_list, gh_int2scm (i/2));
157           
158           Spanner * p = new Spanner (basic);
159
160           Tie::set_interface (p); // cannot remove.
161           Tie::set_head (p,LEFT, dynamic_cast<Item*> (unsmob_grob (ly_car (pair))));
162           Tie::set_head (p,RIGHT, dynamic_cast<Item*> (unsmob_grob (ly_cdr (pair))));
163           
164           ties_.push (p);
165           announce_grob(p, event_->self_scm());
166         }
167       else for (SCM s = head_list; gh_pair_p (s); s = ly_cdr (s))
168         {
169           Grob * p = new Spanner (basic);
170           Tie::set_interface (p); // cannot remove yet!
171           
172           Tie::set_head (p, LEFT, dynamic_cast<Item*> (unsmob_grob (ly_caar (s))));
173           Tie::set_head (p, RIGHT, dynamic_cast<Item*> (unsmob_grob (ly_cdar (s))));
174           
175           ties_.push (p);
176           announce_grob(p, event_->self_scm());
177         }
178
179       if (ties_.size () > 1 && !tie_column_)
180         {
181           tie_column_ = new Spanner (get_property ("TieColumn"));
182
183           for (int i = ties_.size (); i--;)
184             Tie_column::add_tie (tie_column_,ties_ [i]);
185           announce_grob(tie_column_, SCM_EOL);
186         }
187     }
188 }
189
190
191 void
192 Tie_engraver::stop_translation_timestep ()
193 {
194   event_ = 0;
195   now_heads_.clear ();
196
197   /*
198     we don't warn for no ties, since this happens naturally when you
199     use skipTypesetting.  */
200   
201   for (int i=0; i<  ties_.size (); i++)
202    {
203       typeset_tie (ties_[i]);
204     }
205   ties_.clear ();
206   if (tie_column_)
207     {
208       typeset_grob (tie_column_);
209       tie_column_ =0;
210     }
211 }
212
213 void
214 Tie_engraver::typeset_tie (Grob *her)
215 {
216   if (! (Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
217     warning (_ ("lonely tie"));
218
219   Direction d = LEFT;
220   Drul_array<Grob *> new_head_drul;
221   new_head_drul[LEFT] = Tie::head (her,LEFT);
222   new_head_drul[RIGHT] = Tie::head (her,RIGHT);  
223   do {
224     if (!Tie::head (her,d))
225       new_head_drul[d] = Tie::head (her, (Direction)-d);
226   } while (flip (&d) != LEFT);
227
228   index_set_cell (her->get_grob_property ("heads"), LEFT, new_head_drul[LEFT]->self_scm ());
229   index_set_cell (her->get_grob_property ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm ());
230
231   typeset_grob (her);
232 }
233
234 void
235 Tie_engraver::start_translation_timestep ()
236 {
237   SCM m = get_property ("automaticMelismata");
238   if (to_boolean (m))
239     {
240       set_melisma (false);
241     }
242
243   SCM grobs = get_property ("busyGrobs");
244   Moment now = now_mom();
245   stopped_heads_.clear ();
246   
247   for (; gh_pair_p (grobs); grobs = gh_cdr (grobs))
248     {
249       Grob * grob  = unsmob_grob (gh_cdar (grobs));
250       Moment end  =*unsmob_moment (gh_caar (grobs));
251       
252       /*
253         This is slightly ugh: we are now confunding the frontend
254         (iterators) and the backend (note heads) */
255       if (end > now)
256         break;
257       else if (end == now
258                && Note_head::has_interface (grob))
259         stopped_heads_.push (grob);
260     }
261
262
263   /*
264     
265     The list starts with entries that start earlier. By going through
266     it, we reverse the order, where as we'd like to use the `last'
267     heads first.
268
269     This makes  a difference for grace notes. If we have
270
271     c4 \grace c8 ~ c4
272
273     Then busyGrobs will have ((1/4 . gc8) (1/4 . c4)). 
274
275     We want stopped_heads_ to contain (c4 gc8), because we start with
276     it at the top.
277    */
278   stopped_heads_.reverse();
279
280   event_ = 0;
281 }
282
283
284 ENTER_DESCRIPTION(Tie_engraver,
285 /* descr */       "Generate ties between noteheads of equal pitch.",
286 /* creats*/       "Tie TieColumn",
287 /* accepts */     "tie-event",
288 /* acks  */      "rhythmic-head-interface",
289 /* reads */       "sparseTies tieMelismaBusy",
290 /* write */       "");