]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
patch::: 1.3.18.jcn1
[lilypond.git] / lily / tie-engraver.cc
1 /*   
2   tie-engraver.cc --  implement Tie_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--1999 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
17 ADD_THIS_TRANSLATOR (Tie_engraver);
18
19 Tie_engraver::Tie_engraver()
20 {
21   req_l_ = 0;
22 }
23
24
25 bool
26 Tie_engraver::do_try_music (Music *m)
27 {
28   if (Tie_req * c = dynamic_cast<Tie_req*> (m))
29     {
30       req_l_ = c;
31       SCM m = get_property ("automaticMelismata",0);
32       bool am = gh_boolean_p (m) &&gh_scm2bool (m);
33       if (am)
34         {
35           set_melisma (true);
36         }
37       return true;
38     }
39   return false;
40 }
41
42 void
43 Tie_engraver::set_melisma (bool m)
44 {
45   Translator_group *where = daddy_trans_l_;
46   get_property ("tieMelismaBusy", &where);
47   if (!where)
48     where = daddy_trans_l_;
49     
50   daddy_trans_l_->set_property ("tieMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
51 }
52
53 void
54 Tie_engraver::acknowledge_element (Score_element_info i)
55 {
56   if (Note_head *nh = dynamic_cast<Note_head *> (i.elem_l_))
57     {
58       Note_req * m = dynamic_cast<Note_req* > (i.req_l_);
59       if (!m)
60         return;
61       now_heads_.push (CHead_melodic_tuple (nh, m, now_mom()+ m->length_mom ()));
62     }
63 }
64
65 void
66 Tie_engraver::do_process_requests ()
67 {
68   if (req_l_)
69     {
70       Moment now = now_mom ();
71       Link_array<Note_head> nharr;
72       
73       stopped_heads_.clear ();
74       while (past_notes_pq_.size ()
75              && past_notes_pq_.front ().end_ == now)
76         stopped_heads_.push (past_notes_pq_.get ());
77     }
78 }
79
80 void
81 Tie_engraver::process_acknowledged ()
82 {
83   if (req_l_)
84     {
85       now_heads_.sort (CHead_melodic_tuple::pitch_compare);
86       stopped_heads_.sort(CHead_melodic_tuple::pitch_compare);
87
88       SCM head_list = SCM_EOL;
89       
90       int j = stopped_heads_.size ()-1;
91       int i = now_heads_.size ()-1;
92
93       while  (i >= 0 && j >=0)
94         {
95           int comp
96             = Musical_pitch::compare (now_heads_[i].req_l_->pitch_ ,
97                                       stopped_heads_[j].req_l_->pitch_);
98
99           if (comp)
100             {
101               (comp < 0) ? j -- : i--;
102               continue;
103             }
104           else
105             {
106               head_list  = gh_cons (gh_cons (stopped_heads_[j].head_l_->self_scm_,
107                                              now_heads_[i].head_l_->self_scm_),
108                                     head_list);
109
110               past_notes_pq_. insert (now_heads_[i]);
111               now_heads_.del (i);
112               stopped_heads_.del (j);
113               i--;
114               j--;
115             }
116         }
117
118
119       SCM sparse = get_property ("sparseTies", 0);
120       if (to_boolean (sparse))
121         {
122           int i = scm_ilength (head_list);
123
124           if (!i)
125             return;
126           
127           SCM pair = gh_list_ref (head_list, gh_int2scm (i/2));
128           
129           Tie * p = new Tie;
130           p->set_head (LEFT, dynamic_cast<Item*> (unsmob_element (gh_car (pair))));
131           p->set_head (RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdr (pair))));
132           
133           tie_p_arr_.push (p);
134           announce_element (Score_element_info (p, req_l_));
135         }
136       else for (SCM s = head_list; gh_pair_p (s); s = gh_cdr (s))
137         {
138           Tie * p = new Tie;
139           p->set_head (LEFT, dynamic_cast<Item*> (unsmob_element (gh_caar (s))));
140           p->set_head (RIGHT, dynamic_cast<Item*> (unsmob_element (gh_cdar (s))));
141           
142           tie_p_arr_.push (p);
143           announce_element (Score_element_info (p, req_l_));
144         }
145
146       if (!tie_p_arr_.size ())
147         {
148           req_l_->warning (_ ("No ties were created!"));
149         }
150           
151     }
152 }
153
154
155 void
156 Tie_engraver::do_pre_move_processing ()
157 {
158   for (int i=0; i < now_heads_.size (); i++)
159     {
160       past_notes_pq_.insert (now_heads_[i]);
161     }
162   now_heads_.clear ();
163
164   for (int i=0; i<  tie_p_arr_.size (); i++)
165    {
166       typeset_element (tie_p_arr_[i]);
167     }
168   tie_p_arr_.clear ();
169 }
170
171 void
172 Tie_engraver::do_post_move_processing ()
173 {
174   SCM m = get_property ("automaticMelismata",0);
175   if (to_boolean (m))
176     {
177       set_melisma (false);
178     }
179   req_l_ = 0;
180   Moment now = now_mom ();
181   while (past_notes_pq_.size () && past_notes_pq_.front ().end_ < now)
182     past_notes_pq_.delmin ();
183 }
184
185 CHead_melodic_tuple::CHead_melodic_tuple ()
186 {
187   head_l_ =0;
188   req_l_ =0;
189   end_ = 0;
190 }
191
192 CHead_melodic_tuple::CHead_melodic_tuple (Note_head *h, Melodic_req*m, Moment mom)
193 {
194   head_l_ = h;
195   req_l_ = m;
196   end_ = mom;
197 }
198
199 int
200 CHead_melodic_tuple::pitch_compare (CHead_melodic_tuple const&h1,
201                              CHead_melodic_tuple const &h2)
202 {
203   return Melodic_req::compare (*h1.req_l_, *h2.req_l_);
204 }
205
206 int
207 CHead_melodic_tuple::time_compare (CHead_melodic_tuple const&h1,
208                              CHead_melodic_tuple const &h2)
209 {
210   return (h1.end_ - h2.end_ ).sign ();
211 }