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