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 "note-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 {
24 CHead_melodic_tuple ();
25 CHead_melodic_tuple (Note_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_requests ();
61 virtual void process_acknowledged ();
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",0);
84 bool am = gh_boolean_p (m) &&gh_scm2bool (m);
95 Tie_engraver::set_melisma (bool m)
97 Translator_group *where = daddy_trans_l_;
98 get_property ("tieMelismaBusy", &where);
100 where = daddy_trans_l_;
102 daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
106 Tie_engraver::acknowledge_element (Score_element_info i)
108 if (Note_head *nh = dynamic_cast<Note_head *> (i.elem_l_))
110 Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
113 now_heads_.push (CHead_melodic_tuple (nh, m, now_mom()+ m->length_mom ()));
118 Tie_engraver::do_process_requests ()
122 Moment now = now_mom ();
123 Link_array<Note_head> nharr;
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);
171 SCM sparse = get_property ("sparseTies", 0);
172 if (to_boolean (sparse))
174 int i = scm_ilength (head_list);
179 SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
182 p->set_head (LEFT, dynamic_cast<Item*> (unsmob_element (gh_car (pair))));
183 p->set_head (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))
191 p->set_head (LEFT, dynamic_cast<Item*> (unsmob_element (gh_caar (s))));
192 p->set_head (RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdar (s))));
195 announce_element (Score_element_info (p, req_l_));
198 if (!tie_p_arr_.size ())
200 req_l_->warning (_ ("No ties were created!"));
202 else if (tie_p_arr_.size () > 1 && !tie_column_p_)
204 tie_column_p_ = new Tie_column;
205 for (int i = tie_p_arr_.size (); i--; )
206 tie_column_p_->add_tie (tie_p_arr_ [i]);
207 announce_element (Score_element_info (tie_column_p_, 0));
214 Tie_engraver::do_pre_move_processing ()
216 for (int i=0; i < now_heads_.size (); i++)
218 past_notes_pq_.insert (now_heads_[i]);
222 for (int i=0; i< tie_p_arr_.size (); i++)
224 typeset_element (tie_p_arr_[i]);
229 typeset_element (tie_column_p_);
235 Tie_engraver::do_post_move_processing ()
237 SCM m = get_property ("automaticMelismata",0);
243 Moment now = now_mom ();
244 while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
245 past_notes_pq_.delmin ();
248 ADD_THIS_TRANSLATOR(Tie_engraver);
251 CHead_melodic_tuple::CHead_melodic_tuple ()
258 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
266 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
267 CHead_melodic_tuple const &h2)
269 return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
273 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
274 CHead_melodic_tuple const &h2)
276 return (h1.end_ - h2.end_ ).sign ();