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"
16 #include "tie-column.hh"
18 #include "engraver.hh"
21 struct CHead_melodic_tuple {
23 Score_element *head_l_;
25 CHead_melodic_tuple ();
26 CHead_melodic_tuple (Score_element*, 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.
44 class Tie_engraver : public Engraver
46 PQueue<CHead_melodic_tuple> past_notes_pq_;
51 Array<CHead_melodic_tuple> now_heads_;
52 Array<CHead_melodic_tuple> stopped_heads_;
53 Link_array<Tie> tie_p_arr_;
55 Spanner * tie_column_p_;
57 void set_melisma (bool);
60 virtual void do_post_move_processing ();
61 virtual void do_pre_move_processing ();
62 virtual void acknowledge_element (Score_element_info);
63 virtual bool do_try_music (Music*);
64 virtual void do_process_music ();
65 virtual void process_acknowledged ();
66 void typeset_tie (Score_element*);
68 VIRTUAL_COPY_CONS(Translator);
74 Tie_engraver::Tie_engraver()
82 Tie_engraver::do_try_music (Music *m)
84 if (Tie_req * c = dynamic_cast<Tie_req*> (m))
86 /* if (end_mom_ > now_mom ())
90 SCM m = get_property ("automaticMelismata");
91 bool am = gh_boolean_p (m) &&gh_scm2bool (m);
102 Tie_engraver::set_melisma (bool m)
104 daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
108 Tie_engraver::acknowledge_element (Score_element_info i)
110 if (Rhythmic_head::has_interface (i.elem_l_))
112 Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
115 now_heads_.push (CHead_melodic_tuple (i.elem_l_, m, now_mom()+ m->length_mom ()));
120 Tie_engraver::do_process_music ()
124 Moment now = now_mom ();
125 stopped_heads_.clear ();
126 while (past_notes_pq_.size ()
127 && past_notes_pq_.front ().end_ == now)
128 stopped_heads_.push (past_notes_pq_.get ());
133 Tie_engraver::process_acknowledged ()
137 now_heads_.sort (CHead_melodic_tuple::pitch_compare);
138 stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
140 SCM head_list = SCM_EOL;
142 int j = stopped_heads_.size ()-1;
143 int i = now_heads_.size ()-1;
145 while (i >= 0 && j >=0)
148 = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
149 stopped_heads_[j].req_l_->pitch_);
153 (comp < 0) ? j -- : i--;
158 head_list = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm_,
159 now_heads_[i].head_l_->self_scm_),
162 past_notes_pq_. insert (now_heads_[i]);
164 stopped_heads_.del (j);
170 SCM basic = get_property ("basicTieProperties");
171 SCM sparse = get_property ("sparseTies");
172 if (to_boolean (sparse))
174 int i = scm_ilength (head_list);
179 SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
181 Tie * p = new Tie (basic);
182 Tie::set_head (p,LEFT, dynamic_cast<Item*> (unsmob_element (gh_car (pair))));
183 Tie::set_head (p,RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdr (pair))));
186 announce_element (Score_element_info (p, req_l_));
188 else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
190 Tie * p = new Tie (basic);
191 Tie::set_interface (p);
193 Tie::set_head (p, LEFT, dynamic_cast<Item*> (unsmob_element (gh_caar (s))));
194 Tie::set_head (p, RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdar (s))));
197 announce_element (Score_element_info (p, req_l_));
200 if (!tie_p_arr_.size ())
202 req_l_->warning (_ ("No ties were created!"));
204 else if (tie_p_arr_.size () > 1 && !tie_column_p_)
206 tie_column_p_ = new Spanner (get_property ("basicTieColumnProperties"));
207 Tie_column::set_interface (tie_column_p_);
208 for (int i = tie_p_arr_.size (); i--; )
209 Tie_column::add_tie (tie_column_p_,tie_p_arr_ [i]);
210 announce_element (Score_element_info (tie_column_p_, 0));
217 Tie_engraver::do_pre_move_processing ()
219 for (int i=0; i < now_heads_.size (); i++)
221 past_notes_pq_.insert (now_heads_[i]);
225 for (int i=0; i< tie_p_arr_.size (); i++)
227 typeset_tie (tie_p_arr_[i]);
232 typeset_element (tie_column_p_);
238 Tie_engraver::typeset_tie (Score_element *her)
240 if (!(Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
241 warning (_ ("lonely tie"));
244 Drul_array<Score_element *> new_head_drul;
245 new_head_drul[LEFT] = Tie::head(her,LEFT);
246 new_head_drul[RIGHT] = Tie::head (her,RIGHT);
248 if (!Tie::head (her,d))
249 new_head_drul[d] = Tie::head(her,(Direction)-d);
250 } while (flip(&d) != LEFT);
252 index_set_cell (her->get_elt_property ("heads"), LEFT, new_head_drul[LEFT]->self_scm_ );
253 index_set_cell (her->get_elt_property ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm_ );
255 typeset_element (her);
259 Tie_engraver::do_post_move_processing ()
261 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 return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
297 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
298 CHead_melodic_tuple const &h2)
300 return (h1.end_ - h2.end_ ).sign ();