2 tie-engraver.cc -- implement Tie_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "command-request.hh"
11 #include "musical-request.hh"
13 #include "translator-group.hh"
15 #include "tie-column.hh"
16 #include "engraver.hh"
18 #include "grob-pitch-tuple.hh"
19 #include "note-head.hh"
22 Manufacture ties. Acknowledge noteheads, and put them into a
23 priority queue. If we have a Tie_req, connect the notes that finish
24 just at this time, and note that start at this time.
26 TODO: Remove the dependency on musical info. We should tie on the
27 basis of position and duration-log of the heads (not of the reqs).
30 class Tie_engraver : public Engraver
36 Link_array<Grob> now_heads_;
37 Link_array<Grob> stopped_heads_;
38 Link_array<Grob> tie_p_arr_;
40 Spanner * tie_column_p_;
42 void set_melisma (bool);
45 virtual void start_translation_timestep ();
46 virtual void stop_translation_timestep ();
47 virtual void acknowledge_grob (Grob_info);
48 virtual bool try_music (Music*);
49 virtual void create_grobs ();
50 void typeset_tie (Grob*);
52 TRANSLATOR_DECLARATIONS(Tie_engraver);
57 Tie_engraver::Tie_engraver ()
65 Tie_engraver::try_music (Music *m)
67 if (Tie_req * c = dynamic_cast<Tie_req*> (m))
69 /* if (end_mom_ > now_mom ())
73 SCM m = get_property ("automaticMelismata");
74 bool am = gh_boolean_p (m) &&gh_scm2bool (m);
85 Tie_engraver::set_melisma (bool m)
87 daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
91 Tie_engraver::acknowledge_grob (Grob_info i)
93 if (Note_head::has_interface (i.grob_l_))
95 now_heads_.push (i.grob_l_);
100 head_pitch_compare (Grob *const&a,Grob *const&b)
102 Music *m1 =unsmob_music (a->get_grob_property ("cause"));
103 Music *m2 =unsmob_music (b->get_grob_property ("cause"));
105 return Pitch::compare (* unsmob_pitch (m1->get_mus_property ("pitch")),
106 * unsmob_pitch (m2->get_mus_property ("pitch")));
110 Tie_engraver::create_grobs ()
114 now_heads_.sort (&head_pitch_compare);
115 stopped_heads_.sort (&head_pitch_compare);
117 SCM head_list = SCM_EOL;
119 int j = stopped_heads_.size ()-1;
120 int i = now_heads_.size ()-1;
122 while (i >= 0 && j >=0)
125 = head_pitch_compare (now_heads_[i], stopped_heads_[j]);
129 (comp < 0) ? j -- : i--;
134 head_list = gh_cons (gh_cons (stopped_heads_[j]->self_scm (),
135 now_heads_[i]->self_scm ()),
139 stopped_heads_.del (j);
145 SCM basic = get_property ("Tie");
146 SCM sparse = get_property ("sparseTies");
147 if (to_boolean (sparse))
149 int i = scm_ilength (head_list);
154 SCM pair = scm_list_ref (head_list, gh_int2scm (i/2));
156 Spanner * p = new Spanner (basic);
158 Tie::set_interface (p);
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))));
163 announce_grob(p, req_l_->self_scm());
165 else for (SCM s = head_list; gh_pair_p (s); s = ly_cdr (s))
167 Grob * p = new Spanner (basic);
168 Tie::set_interface (p);
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))));
174 announce_grob(p, req_l_->self_scm());
177 if (tie_p_arr_.size () > 1 && !tie_column_p_)
179 tie_column_p_ = new Spanner (get_property ("TieColumn"));
180 Tie_column::set_interface (tie_column_p_);
181 for (int i = tie_p_arr_.size (); i--;)
182 Tie_column::add_tie (tie_column_p_,tie_p_arr_ [i]);
183 announce_grob(tie_column_p_, SCM_EOL);
190 Tie_engraver::stop_translation_timestep ()
197 we don't warn for no ties, since this happens naturally when you
198 use skipTypesetting. */
200 for (int i=0; i< tie_p_arr_.size (); i++)
202 typeset_tie (tie_p_arr_[i]);
207 typeset_grob (tie_column_p_);
213 Tie_engraver::typeset_tie (Grob *her)
215 if (! (Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
216 warning (_ ("lonely tie"));
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);
223 if (!Tie::head (her,d))
224 new_head_drul[d] = Tie::head (her, (Direction)-d);
225 } while (flip (&d) != LEFT);
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 ());
234 Tie_engraver::start_translation_timestep ()
236 SCM m = get_property ("automaticMelismata");
242 SCM grobs = get_property ("busyGrobs");
243 Moment now = now_mom();
244 stopped_heads_.clear ();
246 for (; gh_pair_p (grobs); grobs = gh_cdr (grobs))
248 Grob * grob = unsmob_grob (gh_cdar (grobs));
249 Moment end =*unsmob_moment (gh_caar (grobs));
252 This is slightly ugh: we are now confunding the frontend
253 (iterators) and the backend (note heads) */
257 && Note_head::has_interface (grob))
258 stopped_heads_.push (grob);
263 ENTER_DESCRIPTION(Tie_engraver,
264 /* descr */ "Generate ties between noteheads of equal pitch.",
265 /* creats*/ "Tie TieColumn",
266 /* acks */ "rhythmic-head-interface",
267 /* reads */ "sparseTies tieMelismaBusy",