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