]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
release: 1.3.26
[lilypond.git] / lily / tie-engraver.cc
1 /*   
2   ctie-engraver.cc --  implement Tie_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
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"
19
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 &);  
28 };
29
30 inline int compare (CHead_melodic_tuple const &a, CHead_melodic_tuple const &b)
31 {
32   return CHead_melodic_tuple::time_compare (a,b);
33 }
34
35
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.
40
41    TODO: should share code with Beam_engraver, Extender_engraver?
42  */
43 class Tie_engraver : public Engraver
44 {
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_;
50
51   Tie_column * tie_column_p_;
52   
53   void set_melisma (bool);
54   
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 ();
62
63 public:
64   VIRTUAL_COPY_CONS(Translator);
65   Tie_engraver();
66 };
67
68
69
70 Tie_engraver::Tie_engraver()
71 {
72   req_l_ = 0;
73   tie_column_p_ = 0;
74 }
75
76
77 bool
78 Tie_engraver::do_try_music (Music *m)
79 {
80   if (Tie_req * c = dynamic_cast<Tie_req*> (m))
81     {
82       req_l_ = c;
83       SCM m = get_property ("automaticMelismata",0);
84       bool am = gh_boolean_p (m) &&gh_scm2bool (m);
85       if (am)
86         {
87           set_melisma (true);
88         }
89       return true;
90     }
91   return false;
92 }
93
94 void
95 Tie_engraver::set_melisma (bool m)
96 {
97   Translator_group *where = daddy_trans_l_;
98   get_property ("tieMelismaBusy", &where);
99   if (!where)
100     where = daddy_trans_l_;
101     
102   daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
103 }
104
105 void
106 Tie_engraver::acknowledge_element (Score_element_info i)
107 {
108   if (Note_head *nh = dynamic_cast<Note_head *> (i.elem_l_))
109     {
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 ()));
114     }
115 }
116
117 void
118 Tie_engraver::do_process_requests ()
119 {
120   if (req_l_)
121     {
122       Moment now = now_mom ();
123       Link_array<Note_head> nharr;
124       
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 ());
129     }
130 }
131
132 void
133 Tie_engraver::process_acknowledged ()
134 {
135   if (req_l_)
136     {
137       now_heads_.sort (CHead_melodic_tuple::pitch_compare);
138       stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
139
140       SCM head_list = SCM_EOL;
141       
142       int j = stopped_heads_.size ()-1;
143       int i = now_heads_.size ()-1;
144
145       while  (i >= 0 && j >=0)
146         {
147           int comp
148             = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
149                                       stopped_heads_[j].req_l_->pitch_);
150
151           if (comp)
152             {
153               (comp < 0) ? j -- : i--;
154               continue;
155             }
156           else
157             {
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);
161
162               past_notes_pq_. insert (now_heads_[i]);
163               now_heads_.del (i);
164               stopped_heads_.del (j);
165               i--;
166               j--;
167             }
168         }
169
170
171       SCM sparse = get_property ("sparseTies", 0);
172       if (to_boolean (sparse))
173         {
174           int i = scm_ilength (head_list);
175
176           if (!i)
177             return;
178           
179           SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
180           
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))));
184           
185           tie_p_arr_.push (p);
186           announce_element (Score_element_info (p, req_l_));
187         }
188       else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
189         {
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))));
193           
194           tie_p_arr_.push (p);
195           announce_element (Score_element_info (p, req_l_));
196         }
197
198       if (!tie_p_arr_.size ())
199         {
200           req_l_->warning (_ ("No ties were created!"));
201         }
202       else if (tie_p_arr_.size () > 1 && !tie_column_p_)
203         {
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));
208         }
209     }
210 }
211
212
213 void
214 Tie_engraver::do_pre_move_processing ()
215 {
216   for (int i=0; i < now_heads_.size (); i++)
217     {
218       past_notes_pq_.insert (now_heads_[i]);
219     }
220   now_heads_.clear ();
221
222   for (int i=0; i<  tie_p_arr_.size (); i++)
223    {
224       typeset_element (tie_p_arr_[i]);
225     }
226   tie_p_arr_.clear ();
227   if (tie_column_p_)
228     {
229       typeset_element (tie_column_p_);
230       tie_column_p_ =0;
231     }
232 }
233
234 void
235 Tie_engraver::do_post_move_processing ()
236 {
237   SCM m = get_property ("automaticMelismata",0);
238   if (to_boolean (m))
239     {
240       set_melisma (false);
241     }
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 ();
246 }
247
248 ADD_THIS_TRANSLATOR(Tie_engraver);
249
250
251 CHead_melodic_tuple::CHead_melodic_tuple ()
252 {
253   head_l_ =0;
254   req_l_ =0;
255   end_ = 0;
256 }
257
258 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
259 {
260   head_l_ = h;
261   req_l_ = m;
262   end_ = mom;
263 }
264
265 int
266 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
267                              CHead_melodic_tuple const &h2)
268 {
269   return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
270 }
271
272 int
273 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
274                              CHead_melodic_tuple const &h2)
275 {
276   return (h1.end_ - h2.end_ ).sign ();
277 }