]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
Run `make grand-replace'.
[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--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "engraver.hh"
10
11 #include "context.hh"
12 #include "international.hh"
13 #include "item.hh"
14 #include "note-head.hh"
15 #include "protected-scm.hh"
16 #include "spanner.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "stream-event.hh"
19 #include "tie-column.hh"
20 #include "tie.hh"
21 #include "warn.hh"
22
23 #include "translator.icc"
24
25 /**
26    Manufacture ties.  Acknowledge noteheads, and put them into a
27    priority queue. If we have a TieEvent, connect the notes that finish
28    just at this time, and note that start at this time.
29
30    TODO: Remove the dependency on musical info. We should tie on the
31    basis of position and duration-log of the heads (not of the events).
32 */
33
34 struct Head_event_tuple
35 {
36   Grob *head_;
37   Moment end_moment_;
38   SCM tie_definition_;
39   Stream_event *tie_stream_event_;
40   Stream_event *tie_event_;
41   
42   Head_event_tuple ()
43   {
44     head_ = 0;
45     tie_definition_ = SCM_EOL;
46     tie_event_ = 0;
47     tie_stream_event_ = 0;
48   }
49 };
50
51 class Tie_engraver : public Engraver
52 {
53   Stream_event *event_;
54   vector<Grob*> now_heads_;
55   vector<Head_event_tuple> heads_to_tie_;
56   vector<Grob*> ties_;
57
58   Spanner *tie_column_;
59
60 protected:
61   void stop_translation_timestep ();
62   virtual void derived_mark () const;
63   void start_translation_timestep ();
64   DECLARE_ACKNOWLEDGER (note_head);
65   DECLARE_TRANSLATOR_LISTENER (tie);
66   void process_music ();
67   void typeset_tie (Grob *);
68 public:
69   TRANSLATOR_DECLARATIONS (Tie_engraver);
70 };
71
72 void
73 Tie_engraver::derived_mark () const
74 {
75   Engraver::derived_mark ();
76   for (vsize i = 0; i < heads_to_tie_.size (); i++)
77     scm_gc_mark (heads_to_tie_[i].tie_definition_);
78 }
79
80 Tie_engraver::Tie_engraver ()
81 {
82   event_ = 0;
83   tie_column_ = 0;
84 }
85
86 IMPLEMENT_TRANSLATOR_LISTENER (Tie_engraver, tie);
87 void
88 Tie_engraver::listen_tie (Stream_event *ev)
89 {
90   ASSIGN_EVENT_ONCE (event_, ev);
91 }
92
93 void
94 Tie_engraver::process_music ()
95 {
96   bool busy = event_;
97   for (vsize i = 0; !busy && i < heads_to_tie_.size (); i++)
98     busy |=  (heads_to_tie_[i].tie_event_
99               || heads_to_tie_[i].tie_stream_event_);
100
101   if (busy)
102     context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
103 }
104
105 void
106 Tie_engraver::acknowledge_note_head (Grob_info i)
107 {
108   Grob *h = i.grob ();
109
110   now_heads_.push_back (h);
111   for (vsize i = heads_to_tie_.size (); i--;)
112     {
113       Grob *th = heads_to_tie_[i].head_;
114       Stream_event *right_ev = unsmob_stream_event (h->get_property ("cause"));
115       Stream_event *left_ev = unsmob_stream_event (th->get_property ("cause"));
116
117       /*
118         maybe should check positions too.
119       */
120       if (!right_ev || !left_ev)
121         continue;
122       
123       if (ly_is_equal (right_ev->get_property ("pitch"),
124                        left_ev->get_property ("pitch")))
125         {
126           Grob *p = new Spanner (heads_to_tie_[i].tie_definition_);
127
128           SCM cause = heads_to_tie_[i].tie_event_
129             ? heads_to_tie_[i].tie_event_->self_scm ()
130             : heads_to_tie_[i].tie_stream_event_->self_scm ();
131           
132           announce_grob (p, cause);
133           Tie::set_head (p, LEFT, th);
134           Tie::set_head (p, RIGHT, h);
135
136
137           if (is_direction (unsmob_stream_event (cause)->get_property ("direction")))
138             {
139               Direction d = to_dir (unsmob_stream_event (cause)->get_property ("direction"));
140               p->set_property ("direction", scm_from_int (d)); 
141             }
142           
143           ties_.push_back (p);
144           heads_to_tie_.erase (heads_to_tie_.begin () + i);
145         }
146     }
147
148   if (ties_.size () && ! tie_column_)
149     tie_column_ = make_spanner ("TieColumn", ties_[0]->self_scm ());
150
151   if (tie_column_)
152     for (vsize i = ties_.size (); i--;)
153       Tie_column::add_tie (tie_column_, ties_[i]);
154 }
155
156 void
157 Tie_engraver::start_translation_timestep ()
158 {
159   context ()->set_property ("tieMelismaBusy",
160                             ly_bool2scm (heads_to_tie_.size ()));
161
162   if (heads_to_tie_.size () && !to_boolean (get_property ("tieWaitForNote")))
163     {
164       Moment now = now_mom ();
165       for (vsize i = heads_to_tie_.size ();  i--; )
166         {
167           if (now > heads_to_tie_[i].end_moment_)
168             heads_to_tie_.erase (heads_to_tie_.begin () + i);
169         }
170     }
171 }
172
173 void
174 Tie_engraver::stop_translation_timestep ()
175 {
176   bool wait = to_boolean (get_property ("tieWaitForNote"));
177   if (ties_.size ())
178     {
179       if (!wait)
180         heads_to_tie_.clear ();
181
182       for (vsize i = 0; i < ties_.size (); i++)
183           typeset_tie (ties_[i]);
184
185       ties_.clear ();
186       tie_column_ = 0;
187     }
188
189   vector<Head_event_tuple> new_heads_to_tie;
190   
191   for (vsize i = 0; i < now_heads_.size (); i++)
192     {
193       Grob *head = now_heads_[i];
194       Stream_event *left_ev
195         = unsmob_stream_event (head->get_property ("cause"));
196
197       if (!left_ev)
198         {
199           // may happen for ambituses
200           continue;
201         }
202             
203       
204       SCM left_articulations = left_ev->get_property ("articulations");
205
206       Stream_event *tie_event = 0;
207       Stream_event *tie_stream_event = event_;
208       for (SCM s = left_articulations;
209            !tie_event && !tie_stream_event && scm_is_pair (s);
210            s = scm_cdr (s))
211         {
212           Stream_event *ev = unsmob_stream_event (scm_car (s));
213           if (!ev)
214             continue;
215           
216           if (ev->in_event_class ("tie-event"))
217             tie_event = ev;
218         }
219           
220       if (left_ev && (tie_event || tie_stream_event))
221         {
222           Head_event_tuple event_tup;
223
224           SCM start_definition
225             = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
226
227           event_tup.head_ = head;
228           event_tup.tie_definition_ = start_definition;
229           event_tup.tie_event_ = tie_event;
230           event_tup.tie_stream_event_ = tie_stream_event;
231
232           Moment end = now_mom ();
233           if (end.grace_part_)
234             {
235               end.grace_part_ += get_event_length (left_ev).main_part_;
236             }
237           else
238             {
239               end += get_event_length (left_ev);
240             }
241           event_tup.end_moment_ = end;
242             
243           new_heads_to_tie.push_back (event_tup);
244         }
245     }
246
247   if (!wait && new_heads_to_tie.size ())
248     heads_to_tie_.clear ();
249
250   // hmmm, how to do with copy () ?
251   for (vsize i = 0; i < new_heads_to_tie.size (); i++)
252     heads_to_tie_.push_back (new_heads_to_tie[i]);
253   
254   event_ = 0;
255   now_heads_.clear ();
256 }
257
258 void
259 Tie_engraver::typeset_tie (Grob *her)
260 {
261   if (! (Tie::head (her, LEFT) && Tie::head (her, RIGHT)))
262     warning (_ ("lonely tie"));
263
264   Direction d = LEFT;
265   Drul_array<Grob *> new_head_drul;
266   new_head_drul[LEFT] = Tie::head (her, LEFT);
267   new_head_drul[RIGHT] = Tie::head (her, RIGHT);
268   do
269     {
270       if (!Tie::head (her, d))
271         new_head_drul[d] = Tie::head (her, (Direction) - d);
272     }
273   while (flip (&d) != LEFT);
274
275   Spanner *sp = dynamic_cast<Spanner*> (her);
276   sp->set_bound (LEFT, new_head_drul[LEFT]);
277   sp->set_bound (RIGHT, new_head_drul[RIGHT]);
278 }
279
280 ADD_ACKNOWLEDGER (Tie_engraver, note_head);
281 ADD_TRANSLATOR (Tie_engraver,
282                 /* doc */
283                 "Generate ties between note heads of equal pitch.",
284
285                 /* create */
286                 "Tie "
287                 "TieColumn ",
288
289                 /* read */
290                 "tieWaitForNote ",
291
292                 /* write */
293                 "tieMelismaBusy "
294                 );