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"
20 struct CHead_melodic_tuple {
22 Rhythmic_head *head_l_;
24 CHead_melodic_tuple ();
25 CHead_melodic_tuple (Rhythmic_head*, Melodic_req*, Moment);
26 static int pitch_compare (CHead_melodic_tuple const &, CHead_melodic_tuple const &);
27 static int time_compare (CHead_melodic_tuple const &, CHead_melodic_tuple const &);
30 inline int compare (CHead_melodic_tuple const &a, CHead_melodic_tuple const &b)
32 return CHead_melodic_tuple::time_compare (a,b);
37 Manufacture ties. Acknowledge noteheads, and put them into a
38 priority queue. If we have a Tie_req, connect the notes that finish
39 just at this time, and note that start at this time.
41 TODO: should share code with Beam_engraver, Extender_engraver?
43 class Tie_engraver : public Engraver
45 PQueue<CHead_melodic_tuple> past_notes_pq_;
47 Array<CHead_melodic_tuple> now_heads_;
48 Array<CHead_melodic_tuple> stopped_heads_;
49 Link_array<Tie> tie_p_arr_;
51 Tie_column * tie_column_p_;
53 void set_melisma (bool);
56 virtual void do_post_move_processing ();
57 virtual void do_pre_move_processing ();
58 virtual void acknowledge_element (Score_element_info);
59 virtual bool do_try_music (Music*);
60 virtual void do_process_music ();
61 virtual void process_acknowledged ();
62 void typeset_tie (Score_element*);
64 VIRTUAL_COPY_CONS(Translator);
70 Tie_engraver::Tie_engraver()
78 Tie_engraver::do_try_music (Music *m)
80 if (Tie_req * c = dynamic_cast<Tie_req*> (m))
83 SCM m = get_property ("automaticMelismata");
84 bool am = gh_boolean_p (m) &&gh_scm2bool (m);
95 Tie_engraver::set_melisma (bool m)
97 daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
101 Tie_engraver::acknowledge_element (Score_element_info i)
103 if (Rhythmic_head *nh = dynamic_cast<Rhythmic_head *> (i.elem_l_))
105 Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
108 now_heads_.push (CHead_melodic_tuple (nh, m, now_mom()+ m->length_mom ()));
113 Tie_engraver::do_process_music ()
117 Moment now = now_mom ();
118 Link_array<Note_head> nharr;
120 stopped_heads_.clear ();
121 while (past_notes_pq_.size ()
122 && past_notes_pq_.front ().end_ == now)
123 stopped_heads_.push (past_notes_pq_.get ());
128 Tie_engraver::process_acknowledged ()
132 now_heads_.sort (CHead_melodic_tuple::pitch_compare);
133 stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
135 SCM head_list = SCM_EOL;
137 int j = stopped_heads_.size ()-1;
138 int i = now_heads_.size ()-1;
140 while (i >= 0 && j >=0)
143 = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
144 stopped_heads_[j].req_l_->pitch_);
148 (comp < 0) ? j -- : i--;
153 head_list = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm_,
154 now_heads_[i].head_l_->self_scm_),
157 past_notes_pq_. insert (now_heads_[i]);
159 stopped_heads_.del (j);
165 SCM basic = get_property ("basicTieProperties");
166 SCM sparse = get_property ("sparseTies");
167 if (to_boolean (sparse))
169 int i = scm_ilength (head_list);
174 SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
176 Tie * p = new Tie (basic);
177 Tie::set_head (p,LEFT, dynamic_cast<Item*> (unsmob_element (gh_car (pair))));
178 Tie::set_head (p,RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdr (pair))));
181 announce_element (Score_element_info (p, req_l_));
183 else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
185 Tie * p = new Tie (basic);
186 Tie::set_interface (p);
188 Tie::set_head (p, LEFT, dynamic_cast<Item*> (unsmob_element (gh_caar (s))));
189 Tie::set_head (p, RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdar (s))));
192 announce_element (Score_element_info (p, req_l_));
195 if (!tie_p_arr_.size ())
197 req_l_->warning (_ ("No ties were created!"));
199 else if (tie_p_arr_.size () > 1 && !tie_column_p_)
201 tie_column_p_ = new Tie_column (get_property ("basicTieColumnProperties"));
202 Tie_column::set_interface (tie_column_p_);
203 for (int i = tie_p_arr_.size (); i--; )
204 Tie_column::add_tie (tie_column_p_,tie_p_arr_ [i]);
205 announce_element (Score_element_info (tie_column_p_, 0));
212 Tie_engraver::do_pre_move_processing ()
214 for (int i=0; i < now_heads_.size (); i++)
216 past_notes_pq_.insert (now_heads_[i]);
220 for (int i=0; i< tie_p_arr_.size (); i++)
222 typeset_tie (tie_p_arr_[i]);
227 typeset_tie (tie_column_p_);
233 Tie_engraver::typeset_tie (Score_element *her)
235 if (!(Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
236 warning (_ ("lonely tie"));
239 Drul_array<Item *> new_head_drul;
240 new_head_drul[LEFT] = Tie::head(her,LEFT);
241 new_head_drul[RIGHT] = Tie::head (her,RIGHT);
243 if (!Tie::head (her,d))
244 new_head_drul[d] = Tie::head(her,(Direction)-d);
245 } while (flip(&d) != LEFT);
247 index_set_cell (her->get_elt_pointer ("heads"), LEFT, new_head_drul[LEFT]->self_scm_ );
248 index_set_cell (her->get_elt_pointer ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm_ );
250 typeset_element (her);
254 Tie_engraver::do_post_move_processing ()
256 SCM m = get_property ("automaticMelismata");
262 Moment now = now_mom ();
263 while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
264 past_notes_pq_.delmin ();
267 ADD_THIS_TRANSLATOR(Tie_engraver);
270 CHead_melodic_tuple::CHead_melodic_tuple ()
277 CHead_melodic_tuple::CHead_melodic_tuple (Rhythmic_head *h, Melodic_req*m, Moment mom)
285 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
286 CHead_melodic_tuple const &h2)
288 return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
292 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
293 CHead_melodic_tuple const &h2)
295 return (h1.end_ - h2.end_ ).sign ();