lilypond-1.3.28
[lilypond.git] / lily / tie-engraver.cc
blob0090dbd9b576a943a214e89c832a98cd5d55d7d7
1 /*
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>
8 */
10 #include "tie-engraver.hh"
11 #include "command-request.hh"
12 #include "note-head.hh"
13 #include "musical-request.hh"
14 #include "tie.hh"
15 #include "translator-group.hh"
16 #include "tie-column.hh"
17 #include "pqueue.hh"
18 #include "engraver.hh"
20 struct CHead_melodic_tuple {
21 Melodic_req *req_l_ ;
22 Note_head *head_l_;
23 Moment end_;
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);
36 /**
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_;
46 Tie_req *req_l_;
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);
55 protected:
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 ();
63 public:
64 VIRTUAL_COPY_CONS(Translator);
65 Tie_engraver();
70 Tie_engraver::Tie_engraver()
72 req_l_ = 0;
73 tie_column_p_ = 0;
77 bool
78 Tie_engraver::do_try_music (Music *m)
80 if (Tie_req * c = dynamic_cast<Tie_req*> (m))
82 req_l_ = c;
83 SCM m = get_property ("automaticMelismata",0);
84 bool am = gh_boolean_p (m) &&gh_scm2bool (m);
85 if (am)
87 set_melisma (true);
89 return true;
91 return false;
94 void
95 Tie_engraver::set_melisma (bool m)
97 Translator_group *where = daddy_trans_l_;
98 get_property ("tieMelismaBusy", &where);
99 if (!where)
100 where = daddy_trans_l_;
102 daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
105 void
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_);
111 if (!m)
112 return;
113 now_heads_.push (CHead_melodic_tuple (nh, m, now_mom()+ m->length_mom ()));
117 void
118 Tie_engraver::do_process_requests ()
120 if (req_l_)
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 ());
132 void
133 Tie_engraver::process_acknowledged ()
135 if (req_l_)
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)
147 int comp
148 = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
149 stopped_heads_[j].req_l_->pitch_);
151 if (comp)
153 (comp < 0) ? j -- : i--;
154 continue;
156 else
158 head_list = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm_,
159 now_heads_[i].head_l_->self_scm_),
160 head_list);
162 past_notes_pq_. insert (now_heads_[i]);
163 now_heads_.del (i);
164 stopped_heads_.del (j);
165 i--;
166 j--;
171 SCM sparse = get_property ("sparseTies", 0);
172 if (to_boolean (sparse))
174 int i = scm_ilength (head_list);
176 if (!i)
177 return;
179 SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
181 Tie * p = new Tie;
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))));
185 tie_p_arr_.push (p);
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;
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))));
194 tie_p_arr_.push (p);
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));
213 void
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]);
220 now_heads_.clear ();
222 for (int i=0; i< tie_p_arr_.size (); i++)
224 typeset_element (tie_p_arr_[i]);
226 tie_p_arr_.clear ();
227 if (tie_column_p_)
229 typeset_element (tie_column_p_);
230 tie_column_p_ =0;
234 void
235 Tie_engraver::do_post_move_processing ()
237 SCM m = get_property ("automaticMelismata",0);
238 if (to_boolean (m))
240 set_melisma (false);
242 req_l_ = 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 ()
253 head_l_ =0;
254 req_l_ =0;
255 end_ = 0;
258 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
260 head_l_ = h;
261 req_l_ = m;
262 end_ = 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 ();