2 tie-engraver.cc -- implement Tie_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include "translator-group.hh"
15 #include "tie-column.hh"
16 #include "engraver.hh"
18 #include "grob-pitch-tuple.hh"
20 #include "note-head.hh"
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.
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).
30 New tie event happens at the time of the first note, the
32 class Tie_engraver : public Engraver
39 Link_array<Grob> now_heads_;
40 Link_array<Grob> stopped_heads_;
41 Link_array<Grob> ties_;
43 Spanner * tie_column_;
45 void set_melisma (bool);
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*);
55 TRANSLATOR_DECLARATIONS(Tie_engraver);
60 Tie_engraver::Tie_engraver ()
68 Tie_engraver::try_music (Music *mus)
70 if (mus->is_mus_type ("tie-event"))
77 SCM m = get_property ("automaticMelismata");
78 bool am = gh_boolean_p (m) &&gh_scm2bool (m);
88 Tie_engraver::set_melisma (bool m)
90 daddy_trans_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
94 Tie_engraver::acknowledge_grob (Grob_info i)
96 if (Note_head::has_interface (i.grob_))
98 now_heads_.push (i.grob_);
103 head_pitch_compare (Grob *const&a,Grob *const&b)
105 Music *m1 =unsmob_music (a->get_grob_property ("cause"));
106 Music *m2 =unsmob_music (b->get_grob_property ("cause"));
108 return Pitch::compare (* unsmob_pitch (m1->get_mus_property ("pitch")),
109 * unsmob_pitch (m2->get_mus_property ("pitch")));
113 Tie_engraver::process_acknowledged_grobs ()
117 now_heads_.sort (&head_pitch_compare);
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.
125 SCM head_list = SCM_EOL;
127 for (int i = now_heads_.size(); i--;)
129 for (int j = stopped_heads_.size(); j--;)
132 = head_pitch_compare (now_heads_[i], stopped_heads_[j]);
136 head_list = gh_cons (gh_cons (stopped_heads_[j]->self_scm (),
137 now_heads_[i]->self_scm ()),
141 stopped_heads_.del (j);
147 SCM basic = get_property ("Tie");
148 SCM sparse = get_property ("sparseTies");
149 if (to_boolean (sparse))
151 int i = scm_ilength (head_list);
156 SCM pair = scm_list_ref (head_list, gh_int2scm (i/2));
158 Spanner * p = new Spanner (basic);
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))));
165 announce_grob(p, event_->self_scm());
167 else for (SCM s = head_list; gh_pair_p (s); s = ly_cdr (s))
169 Grob * p = new Spanner (basic);
170 Tie::set_interface (p); // cannot remove yet!
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))));
176 announce_grob(p, event_->self_scm());
179 if (ties_.size () > 1 && !tie_column_)
181 tie_column_ = new Spanner (get_property ("TieColumn"));
183 for (int i = ties_.size (); i--;)
184 Tie_column::add_tie (tie_column_,ties_ [i]);
185 announce_grob(tie_column_, SCM_EOL);
192 Tie_engraver::stop_translation_timestep ()
198 we don't warn for no ties, since this happens naturally when you
199 use skipTypesetting. */
201 for (int i=0; i< ties_.size (); i++)
203 typeset_tie (ties_[i]);
208 typeset_grob (tie_column_);
214 Tie_engraver::typeset_tie (Grob *her)
216 if (! (Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
217 warning (_ ("lonely tie"));
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);
224 if (!Tie::head (her,d))
225 new_head_drul[d] = Tie::head (her, (Direction)-d);
226 } while (flip (&d) != LEFT);
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 ());
235 Tie_engraver::start_translation_timestep ()
237 SCM m = get_property ("automaticMelismata");
243 SCM grobs = get_property ("busyGrobs");
244 Moment now = now_mom();
245 stopped_heads_.clear ();
247 for (; gh_pair_p (grobs); grobs = gh_cdr (grobs))
249 Grob * grob = unsmob_grob (gh_cdar (grobs));
250 Moment end =*unsmob_moment (gh_caar (grobs));
253 This is slightly ugh: we are now confunding the frontend
254 (iterators) and the backend (note heads) */
258 && Note_head::has_interface (grob))
259 stopped_heads_.push (grob);
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'
269 This makes a difference for grace notes. If we have
273 Then busyGrobs will have ((1/4 . gc8) (1/4 . c4)).
275 We want stopped_heads_ to contain (c4 gc8), because we start with
278 stopped_heads_.reverse();
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",