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