2 ctie-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_;
48 Array<CHead_melodic_tuple> now_heads_;
49 Array<CHead_melodic_tuple> stopped_heads_;
50 Link_array<Tie> tie_p_arr_;
52 Tie_column * tie_column_p_;
54 void set_melisma (bool);
57 virtual void do_post_move_processing ();
58 virtual void do_pre_move_processing ();
59 virtual void acknowledge_element (Score_element_info);
60 virtual bool do_try_music (Music*);
61 virtual void do_process_music ();
62 virtual void process_acknowledged ();
63 void typeset_tie (Score_element*);
65 VIRTUAL_COPY_CONS(Translator);
71 Tie_engraver::Tie_engraver()
79 Tie_engraver::do_try_music (Music *m)
81 if (Tie_req * c = dynamic_cast<Tie_req*> (m))
84 SCM m = get_property ("automaticMelismata");
85 bool am = gh_boolean_p (m) &&gh_scm2bool (m);
96 Tie_engraver::set_melisma (bool m)
98 daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
102 Tie_engraver::acknowledge_element (Score_element_info i)
104 if (Rhythmic_head::has_interface (i.elem_l_))
106 Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
109 now_heads_.push (CHead_melodic_tuple (i.elem_l_, m, now_mom()+ m->length_mom ()));
114 Tie_engraver::do_process_music ()
118 Moment now = now_mom ();
119 Link_array<Note_head> nharr;
121 stopped_heads_.clear ();
122 while (past_notes_pq_.size ()
123 && past_notes_pq_.front ().end_ == now)
124 stopped_heads_.push (past_notes_pq_.get ());
129 Tie_engraver::process_acknowledged ()
133 now_heads_.sort (CHead_melodic_tuple::pitch_compare);
134 stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
136 SCM head_list = SCM_EOL;
138 int j = stopped_heads_.size ()-1;
139 int i = now_heads_.size ()-1;
141 while (i >= 0 && j >=0)
144 = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
145 stopped_heads_[j].req_l_->pitch_);
149 (comp < 0) ? j -- : i--;
154 head_list = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm_,
155 now_heads_[i].head_l_->self_scm_),
158 past_notes_pq_. insert (now_heads_[i]);
160 stopped_heads_.del (j);
166 SCM basic = get_property ("basicTieProperties");
167 SCM sparse = get_property ("sparseTies");
168 if (to_boolean (sparse))
170 int i = scm_ilength (head_list);
175 SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
177 Tie * p = new Tie (basic);
178 Tie::set_head (p,LEFT, dynamic_cast<Item*> (unsmob_element (gh_car (pair))));
179 Tie::set_head (p,RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdr (pair))));
182 announce_element (Score_element_info (p, req_l_));
184 else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
186 Tie * p = new Tie (basic);
187 Tie::set_interface (p);
189 Tie::set_head (p, LEFT, dynamic_cast<Item*> (unsmob_element (gh_caar (s))));
190 Tie::set_head (p, RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdar (s))));
193 announce_element (Score_element_info (p, req_l_));
196 if (!tie_p_arr_.size ())
198 req_l_->warning (_ ("No ties were created!"));
200 else if (tie_p_arr_.size () > 1 && !tie_column_p_)
202 tie_column_p_ = new Tie_column (get_property ("basicTieColumnProperties"));
203 Tie_column::set_interface (tie_column_p_);
204 for (int i = tie_p_arr_.size (); i--; )
205 Tie_column::add_tie (tie_column_p_,tie_p_arr_ [i]);
206 announce_element (Score_element_info (tie_column_p_, 0));
213 Tie_engraver::do_pre_move_processing ()
215 for (int i=0; i < now_heads_.size (); i++)
217 past_notes_pq_.insert (now_heads_[i]);
221 for (int i=0; i< tie_p_arr_.size (); i++)
223 typeset_tie (tie_p_arr_[i]);
228 typeset_element (tie_column_p_);
234 Tie_engraver::typeset_tie (Score_element *her)
236 if (!(Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
237 warning (_ ("lonely tie"));
240 Drul_array<Score_element *> new_head_drul;
241 new_head_drul[LEFT] = Tie::head(her,LEFT);
242 new_head_drul[RIGHT] = Tie::head (her,RIGHT);
244 if (!Tie::head (her,d))
245 new_head_drul[d] = Tie::head(her,(Direction)-d);
246 } while (flip(&d) != LEFT);
248 index_set_cell (her->get_elt_property ("heads"), LEFT, new_head_drul[LEFT]->self_scm_ );
249 index_set_cell (her->get_elt_property ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm_ );
251 typeset_element (her);
255 Tie_engraver::do_post_move_processing ()
257 SCM m = get_property ("automaticMelismata");
263 Moment now = now_mom ();
264 while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
265 past_notes_pq_.delmin ();
268 ADD_THIS_TRANSLATOR(Tie_engraver);
271 CHead_melodic_tuple::CHead_melodic_tuple ()
278 CHead_melodic_tuple::CHead_melodic_tuple (Score_element *h, Melodic_req*m, Moment mom)
286 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
287 CHead_melodic_tuple const &h2)
289 return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
293 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
294 CHead_melodic_tuple const &h2)
296 return (h1.end_ - h2.end_ ).sign ();