]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
release: 1.3.33
[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_music ();
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");
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   daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
98 }
99
100 void
101 Tie_engraver::acknowledge_element (Score_element_info i)
102 {
103   if (Note_head *nh = dynamic_cast<Note_head *> (i.elem_l_))
104     {
105       Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
106       if (!m)
107         return;
108       now_heads_.push (CHead_melodic_tuple (nh, m, now_mom()+ m->length_mom ()));
109     }
110 }
111
112 void
113 Tie_engraver::do_process_music ()
114 {
115   if (req_l_)
116     {
117       Moment now = now_mom ();
118       Link_array<Note_head> nharr;
119       
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 ());
124     }
125 }
126
127 void
128 Tie_engraver::process_acknowledged ()
129 {
130   if (req_l_)
131     {
132       now_heads_.sort (CHead_melodic_tuple::pitch_compare);
133       stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
134
135       SCM head_list = SCM_EOL;
136       
137       int j = stopped_heads_.size ()-1;
138       int i = now_heads_.size ()-1;
139
140       while  (i >= 0 && j >=0)
141         {
142           int comp
143             = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
144                                       stopped_heads_[j].req_l_->pitch_);
145
146           if (comp)
147             {
148               (comp < 0) ? j -- : i--;
149               continue;
150             }
151           else
152             {
153               head_list  = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm_,
154                                              now_heads_[i].head_l_->self_scm_),
155                                     head_list);
156
157               past_notes_pq_. insert (now_heads_[i]);
158               now_heads_.del (i);
159               stopped_heads_.del (j);
160               i--;
161               j--;
162             }
163         }
164
165
166       SCM sparse = get_property ("sparseTies");
167       if (to_boolean (sparse))
168         {
169           int i = scm_ilength (head_list);
170
171           if (!i)
172             return;
173           
174           SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
175           
176           Tie * p = new Tie;
177           p->set_head (LEFT, dynamic_cast<Item*> (unsmob_element (gh_car (pair))));
178           p->set_head (RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdr (pair))));
179           
180           tie_p_arr_.push (p);
181           announce_element (Score_element_info (p, req_l_));
182         }
183       else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
184         {
185           Tie * p = new Tie;
186           p->set_head (LEFT, dynamic_cast<Item*> (unsmob_element (gh_caar (s))));
187           p->set_head (RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdar (s))));
188           
189           tie_p_arr_.push (p);
190           announce_element (Score_element_info (p, req_l_));
191         }
192
193       if (!tie_p_arr_.size ())
194         {
195           req_l_->warning (_ ("No ties were created!"));
196         }
197       else if (tie_p_arr_.size () > 1 && !tie_column_p_)
198         {
199           tie_column_p_ = new Tie_column;
200           for (int i = tie_p_arr_.size (); i--; )
201             tie_column_p_->add_tie (tie_p_arr_ [i]);
202           announce_element (Score_element_info (tie_column_p_, 0));
203         }
204     }
205 }
206
207
208 void
209 Tie_engraver::do_pre_move_processing ()
210 {
211   for (int i=0; i < now_heads_.size (); i++)
212     {
213       past_notes_pq_.insert (now_heads_[i]);
214     }
215   now_heads_.clear ();
216
217   for (int i=0; i<  tie_p_arr_.size (); i++)
218    {
219       typeset_element (tie_p_arr_[i]);
220     }
221   tie_p_arr_.clear ();
222   if (tie_column_p_)
223     {
224       typeset_element (tie_column_p_);
225       tie_column_p_ =0;
226     }
227 }
228
229 void
230 Tie_engraver::do_post_move_processing ()
231 {
232   SCM m = get_property ("automaticMelismata");
233   if (to_boolean (m))
234     {
235       set_melisma (false);
236     }
237   req_l_ = 0;
238   Moment now = now_mom ();
239   while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
240     past_notes_pq_.delmin ();
241 }
242
243 ADD_THIS_TRANSLATOR(Tie_engraver);
244
245
246 CHead_melodic_tuple::CHead_melodic_tuple ()
247 {
248   head_l_ =0;
249   req_l_ =0;
250   end_ = 0;
251 }
252
253 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
254 {
255   head_l_ = h;
256   req_l_ = m;
257   end_ = mom;
258 }
259
260 int
261 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
262                              CHead_melodic_tuple const &h2)
263 {
264   return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
265 }
266
267 int
268 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
269                              CHead_melodic_tuple const &h2)
270 {
271   return (h1.end_ - h2.end_ ).sign ();
272 }