2 tie-engraver.cc -- implement Tie_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "tie-engraver.hh"
11 #include "command-request.hh"
12 #include "rhythmic-head.hh"
13 #include "musical-request.hh"
15 #include "translator-group.hh"
17 #include "tie-column.hh"
19 #include "engraver.hh"
22 struct CHead_melodic_tuple {
24 Score_element *head_l_;
26 CHead_melodic_tuple ();
27 CHead_melodic_tuple (Score_element*, Melodic_req*, Moment);
28 static int pitch_compare (CHead_melodic_tuple const &, CHead_melodic_tuple const &);
29 static int time_compare (CHead_melodic_tuple const &, CHead_melodic_tuple const &);
32 inline int compare (CHead_melodic_tuple const &a, CHead_melodic_tuple const &b)
34 return CHead_melodic_tuple::time_compare (a,b);
39 Manufacture ties. Acknowledge noteheads, and put them into a
40 priority queue. If we have a Tie_req, connect the notes that finish
41 just at this time, and note that start at this time.
45 class Tie_engraver : public Engraver
48 PQueue<CHead_melodic_tuple> past_notes_pq_;
53 Array<CHead_melodic_tuple> now_heads_;
54 Array<CHead_melodic_tuple> stopped_heads_;
55 Link_array<Score_element> tie_p_arr_;
57 Spanner * tie_column_p_;
59 void set_melisma (bool);
62 virtual void do_post_move_processing ();
63 virtual void do_pre_move_processing ();
64 virtual void acknowledge_element (Score_element_info);
65 virtual bool do_try_music (Music*);
66 virtual void process_acknowledged ();
67 void typeset_tie (Score_element*);
69 VIRTUAL_COPY_CONS(Translator);
75 Tie_engraver::Tie_engraver()
83 Tie_engraver::do_try_music (Music *m)
85 if (Tie_req * c = dynamic_cast<Tie_req*> (m))
87 /* if (end_mom_ > now_mom ())
91 SCM m = get_property ("automaticMelismata");
92 bool am = gh_boolean_p (m) &&gh_scm2bool (m);
103 Tie_engraver::set_melisma (bool m)
105 daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
109 Tie_engraver::acknowledge_element (Score_element_info i)
111 if (Rhythmic_head::has_interface (i.elem_l_))
113 Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
116 now_heads_.push (CHead_melodic_tuple (i.elem_l_, m, now_mom()+ m->length_mom ()));
121 Tie_engraver::process_acknowledged ()
123 if (req_l_ && !done_)
125 Moment now = now_mom ();
126 stopped_heads_.clear ();
127 while (past_notes_pq_.size ()
128 && past_notes_pq_.front ().end_ == now)
129 stopped_heads_.push (past_notes_pq_.get ());
136 now_heads_.sort (CHead_melodic_tuple::pitch_compare);
137 stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
139 SCM head_list = SCM_EOL;
141 int j = stopped_heads_.size ()-1;
142 int i = now_heads_.size ()-1;
144 while (i >= 0 && j >=0)
147 = Pitch::compare (*unsmob_pitch (now_heads_[i].req_l_->get_mus_property ("pitch") ),
148 *unsmob_pitch (stopped_heads_[j].req_l_->get_mus_property ("pitch")));
152 (comp < 0) ? j -- : i--;
157 head_list = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm (),
158 now_heads_[i].head_l_->self_scm ()),
161 past_notes_pq_. insert (now_heads_[i]);
163 stopped_heads_.del (j);
169 SCM basic = get_property ("Tie");
170 SCM sparse = get_property ("sparseTies");
171 if (to_boolean (sparse))
173 int i = scm_ilength (head_list);
178 SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
180 Spanner * p = new Spanner (basic);
181 Tie::set_head (p,LEFT, dynamic_cast<Item*> (unsmob_element (gh_car (pair))));
182 Tie::set_head (p,RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdr (pair))));
185 announce_element (p, req_l_);
187 else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
189 Score_element * p = new Spanner (basic);
190 Tie::set_interface (p);
192 Tie::set_head (p, LEFT, dynamic_cast<Item*> (unsmob_element (gh_caar (s))));
193 Tie::set_head (p, RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdar (s))));
196 announce_element (p, req_l_);
199 if (!tie_p_arr_.size ())
201 req_l_->origin ()->warning (_ ("No ties were created!"));
203 else if (tie_p_arr_.size () > 1 && !tie_column_p_)
205 tie_column_p_ = new Spanner (get_property ("TieColumn"));
206 Tie_column::set_interface (tie_column_p_);
207 for (int i = tie_p_arr_.size (); i--; )
208 Tie_column::add_tie (tie_column_p_,tie_p_arr_ [i]);
209 announce_element (tie_column_p_, 0);
216 Tie_engraver::do_pre_move_processing ()
218 for (int i=0; i < now_heads_.size (); i++)
220 past_notes_pq_.insert (now_heads_[i]);
224 for (int i=0; i< tie_p_arr_.size (); i++)
226 typeset_tie (tie_p_arr_[i]);
231 typeset_element (tie_column_p_);
237 Tie_engraver::typeset_tie (Score_element *her)
239 if (!(Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
240 warning (_ ("lonely tie"));
243 Drul_array<Score_element *> new_head_drul;
244 new_head_drul[LEFT] = Tie::head(her,LEFT);
245 new_head_drul[RIGHT] = Tie::head (her,RIGHT);
247 if (!Tie::head (her,d))
248 new_head_drul[d] = Tie::head(her,(Direction)-d);
249 } while (flip(&d) != LEFT);
251 index_set_cell (her->get_elt_property ("heads"), LEFT, new_head_drul[LEFT]->self_scm () );
252 index_set_cell (her->get_elt_property ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm () );
254 typeset_element (her);
258 Tie_engraver::do_post_move_processing ()
260 SCM m = get_property ("automaticMelismata");
267 Moment now = now_mom ();
268 while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
269 past_notes_pq_.delmin ();
272 ADD_THIS_TRANSLATOR(Tie_engraver);
275 CHead_melodic_tuple::CHead_melodic_tuple ()
282 CHead_melodic_tuple::CHead_melodic_tuple (Score_element *h, Melodic_req*m, Moment mom)
290 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
291 CHead_melodic_tuple const &h2)
293 SCM p1 = h1.req_l_->get_mus_property ("pitch");
294 SCM p2 = h2.req_l_->get_mus_property ("pitch");
296 return Pitch::equal_p (p1,p2) == SCM_BOOL_T;
300 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
301 CHead_melodic_tuple const &h2)
303 return (h1.end_ - h2.end_ ).sign ();