2 tie-engraver.cc -- implement Tie_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "command-request.hh"
11 #include "rhythmic-head.hh"
12 #include "musical-request.hh"
14 #include "translator-group.hh"
16 #include "tie-column.hh"
18 #include "engraver.hh"
21 struct CHead_melodic_tuple {
25 CHead_melodic_tuple ();
26 CHead_melodic_tuple (Grob*, Melodic_req*, Moment);
27 static int pitch_compare (CHead_melodic_tuple const &, CHead_melodic_tuple const &);
28 static int time_compare (CHead_melodic_tuple const &, CHead_melodic_tuple const &);
31 inline int compare (CHead_melodic_tuple const &a, CHead_melodic_tuple const &b)
33 return CHead_melodic_tuple::time_compare (a,b);
38 Manufacture ties. Acknowledge noteheads, and put them into a
39 priority queue. If we have a Tie_req, connect the notes that finish
40 just at this time, and note that start at this time.
42 TODO: junk the pq; the PQ is overkill if we assume that no
43 different durations occur in parallel.
45 class Tie_engraver : public Engraver
47 PQueue<CHead_melodic_tuple> past_notes_pq_;
52 Array<CHead_melodic_tuple> now_heads_;
53 Array<CHead_melodic_tuple> stopped_heads_;
54 Link_array<Grob> tie_p_arr_;
56 Spanner * tie_column_p_;
58 void set_melisma (bool);
61 virtual void start_translation_timestep ();
62 virtual void stop_translation_timestep ();
63 virtual void acknowledge_grob (Grob_info);
64 virtual bool try_music (Music*);
65 virtual void create_grobs ();
66 void typeset_tie (Grob*);
68 TRANSLATOR_DECLARATIONS(Tie_engraver);
73 Tie_engraver::Tie_engraver ()
81 Tie_engraver::try_music (Music *m)
83 if (Tie_req * c = dynamic_cast<Tie_req*> (m))
85 /* if (end_mom_ > now_mom ())
89 SCM m = get_property ("automaticMelismata");
90 bool am = gh_boolean_p (m) &&gh_scm2bool (m);
101 Tie_engraver::set_melisma (bool m)
103 daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
107 Tie_engraver::acknowledge_grob (Grob_info i)
109 if (Rhythmic_head::has_interface (i.grob_l_))
111 Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
114 now_heads_.push (CHead_melodic_tuple (i.grob_l_, m, now_mom ()+ m->length_mom ()));
120 Tie_engraver::create_grobs ()
124 now_heads_.sort (CHead_melodic_tuple::pitch_compare);
125 stopped_heads_.sort (CHead_melodic_tuple::pitch_compare);
127 SCM head_list = SCM_EOL;
129 int j = stopped_heads_.size ()-1;
130 int i = now_heads_.size ()-1;
132 while (i >= 0 && j >=0)
135 = Pitch::compare (*unsmob_pitch (now_heads_[i].req_l_->get_mus_property ("pitch")),
136 *unsmob_pitch (stopped_heads_[j].req_l_->get_mus_property ("pitch")));
140 (comp < 0) ? j -- : i--;
145 head_list = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm (),
146 now_heads_[i].head_l_->self_scm ()),
149 past_notes_pq_. insert (now_heads_[i]);
151 stopped_heads_.del (j);
157 SCM basic = get_property ("Tie");
158 SCM sparse = get_property ("sparseTies");
159 if (to_boolean (sparse))
161 int i = scm_ilength (head_list);
166 SCM pair = scm_list_ref (head_list, gh_int2scm (i/2));
168 Spanner * p = new Spanner (basic);
170 Tie::set_interface (p);
171 Tie::set_head (p,LEFT, dynamic_cast<Item*> (unsmob_grob (ly_car (pair))));
172 Tie::set_head (p,RIGHT, dynamic_cast<Item*> (unsmob_grob (ly_cdr (pair))));
175 announce_grob (p, req_l_);
177 else for (SCM s = head_list; gh_pair_p (s); s = ly_cdr (s))
179 Grob * p = new Spanner (basic);
180 Tie::set_interface (p);
182 Tie::set_head (p, LEFT, dynamic_cast<Item*> (unsmob_grob (ly_caar (s))));
183 Tie::set_head (p, RIGHT, dynamic_cast<Item*> (unsmob_grob (ly_cdar (s))));
186 announce_grob (p, req_l_);
189 if (tie_p_arr_.size () > 1 && !tie_column_p_)
191 tie_column_p_ = new Spanner (get_property ("TieColumn"));
192 Tie_column::set_interface (tie_column_p_);
193 for (int i = tie_p_arr_.size (); i--;)
194 Tie_column::add_tie (tie_column_p_,tie_p_arr_ [i]);
195 announce_grob (tie_column_p_, 0);
202 Tie_engraver::stop_translation_timestep ()
204 for (int i=0; i < now_heads_.size (); i++)
206 past_notes_pq_.insert (now_heads_[i]);
211 we don't warn for no ties, since this happens naturally when you
212 use skipTypesetting. */
215 if (req_l_ && !tie_p_arr_.size ())
217 /* How to shut up this warning, when no notes appeared because
218 they were suicided by Thread_devnull_engraver? */
219 req_l_->origin ()->warning (_ ("No ties were created!"));
223 for (int i=0; i< tie_p_arr_.size (); i++)
225 typeset_tie (tie_p_arr_[i]);
230 typeset_grob (tie_column_p_);
236 Tie_engraver::typeset_tie (Grob *her)
238 if (! (Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
239 warning (_ ("lonely tie"));
242 Drul_array<Grob *> new_head_drul;
243 new_head_drul[LEFT] = Tie::head (her,LEFT);
244 new_head_drul[RIGHT] = Tie::head (her,RIGHT);
246 if (!Tie::head (her,d))
247 new_head_drul[d] = Tie::head (her, (Direction)-d);
248 } while (flip (&d) != LEFT);
250 index_set_cell (her->get_grob_property ("heads"), LEFT, new_head_drul[LEFT]->self_scm ());
251 index_set_cell (her->get_grob_property ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm ());
257 Tie_engraver::start_translation_timestep ()
259 SCM m = get_property ("automaticMelismata");
265 Moment now = now_mom ();
266 while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
267 past_notes_pq_.delmin ();
270 stopped_heads_.clear ();
271 while (past_notes_pq_.size ()
272 && past_notes_pq_.front ().end_ == now)
273 stopped_heads_.push (past_notes_pq_.get ());
280 CHead_melodic_tuple::CHead_melodic_tuple ()
287 CHead_melodic_tuple::CHead_melodic_tuple (Grob *h, Melodic_req*m, Moment mom)
295 signed compare, should use pitch<?
298 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
299 CHead_melodic_tuple const &h2)
301 SCM p1 = h1.req_l_->get_mus_property ("pitch");
302 SCM p2 = h2.req_l_->get_mus_property ("pitch");
304 return Pitch::compare (*unsmob_pitch (p1),
309 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
310 CHead_melodic_tuple const &h2)
312 return Moment::compare(h1.end_, h2.end_);
314 ENTER_DESCRIPTION(Tie_engraver,
315 /* descr */ "Generate ties between noteheads of equal pitch.",
316 /* creats*/ "Tie TieColumn",
317 /* acks */ "rhythmic-head-interface",
318 /* reads */ "sparseTies tieMelismaBusy",