]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
release: 1.5.44
[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_l_;
37   Link_array<Grob> now_heads_;
38   Link_array<Grob> stopped_heads_;
39   Link_array<Grob> tie_p_arr_;
40
41   Spanner * tie_column_p_;
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 create_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_l_ = 0;
61   tie_column_p_ = 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_l_ = 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_l_->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_l_))
95     {
96       now_heads_.push (i.grob_l_);
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::create_grobs ()
112 {
113   if (req_l_)
114     {
115       now_heads_.sort (&head_pitch_compare);
116       stopped_heads_.sort (&head_pitch_compare);
117
118       SCM head_list = SCM_EOL;
119       
120       int j = stopped_heads_.size ()-1;
121       int i = now_heads_.size ()-1;
122
123       while (i >= 0 && j >=0)
124         {
125           int comp
126             = head_pitch_compare (now_heads_[i], stopped_heads_[j]);
127
128           if (comp)
129             {
130               (comp < 0) ? j -- : i--;
131               continue;
132             }
133           else
134             {
135               head_list  = gh_cons (gh_cons (stopped_heads_[j]->self_scm (),
136                                              now_heads_[i]->self_scm ()),
137                                     head_list);
138
139               now_heads_.del (i);
140               stopped_heads_.del (j);
141               i--;
142               j--;
143             }
144         }
145
146       SCM basic = get_property ("Tie");
147       SCM sparse = get_property ("sparseTies");
148       if (to_boolean (sparse))
149         {
150           int i = scm_ilength (head_list);
151
152           if (!i)
153             return;
154           
155           SCM pair = scm_list_ref (head_list, gh_int2scm (i/2));
156           
157           Spanner * p = new Spanner (basic);
158
159           Tie::set_interface (p);
160           Tie::set_head (p,LEFT, dynamic_cast<Item*> (unsmob_grob (ly_car (pair))));
161           Tie::set_head (p,RIGHT, dynamic_cast<Item*> (unsmob_grob (ly_cdr (pair))));
162           
163           tie_p_arr_.push (p);
164           announce_grob(p, req_l_->self_scm());
165         }
166       else for (SCM s = head_list; gh_pair_p (s); s = ly_cdr (s))
167         {
168           Grob * p = new Spanner (basic);
169           Tie::set_interface (p);
170           
171           Tie::set_head (p, LEFT, dynamic_cast<Item*> (unsmob_grob (ly_caar (s))));
172           Tie::set_head (p, RIGHT, dynamic_cast<Item*> (unsmob_grob (ly_cdar (s))));
173           
174           tie_p_arr_.push (p);
175           announce_grob(p, req_l_->self_scm());
176         }
177
178       if (tie_p_arr_.size () > 1 && !tie_column_p_)
179         {
180           tie_column_p_ = new Spanner (get_property ("TieColumn"));
181           Tie_column::set_interface (tie_column_p_);
182           for (int i = tie_p_arr_.size (); i--;)
183             Tie_column::add_tie (tie_column_p_,tie_p_arr_ [i]);
184           announce_grob(tie_column_p_, SCM_EOL);
185         }
186     }
187 }
188
189
190 void
191 Tie_engraver::stop_translation_timestep ()
192 {
193   req_l_ = 0;
194
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<  tie_p_arr_.size (); i++)
202    {
203       typeset_tie (tie_p_arr_[i]);
204     }
205   tie_p_arr_.clear ();
206   if (tie_column_p_)
207     {
208       typeset_grob (tie_column_p_);
209       tie_column_p_ =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 ENTER_DESCRIPTION(Tie_engraver,
265 /* descr */       "Generate ties between noteheads of equal pitch.",
266 /* creats*/       "Tie TieColumn",
267 /* acks  */       "rhythmic-head-interface",
268 /* reads */       "sparseTies tieMelismaBusy",
269 /* write */       "");