]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
copy 'direction from tie-cause to tie. Fixes #139.
[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
139           if (is_direction (unsmob_stream_event (cause)->get_property ("direction")))
140             {
141               Direction d = to_dir (unsmob_stream_event (cause)->get_property ("direction"));
142               p->set_property ("direction", scm_from_int (d)); 
143             }
144           
145           ties_.push_back (p);
146           heads_to_tie_.erase (heads_to_tie_.begin () + i);
147         }
148     }
149
150   if (ties_.size () && ! tie_column_)
151     tie_column_ = make_spanner ("TieColumn", ties_[0]->self_scm ());
152
153   if (tie_column_)
154     for (vsize i = ties_.size (); i--;)
155       Tie_column::add_tie (tie_column_, ties_[i]);
156 }
157
158 void
159 Tie_engraver::start_translation_timestep ()
160 {
161   context ()->set_property ("tieMelismaBusy",
162                             ly_bool2scm (heads_to_tie_.size ()));
163   
164   if (!to_boolean (get_property ("tieWaitForNote")))
165     {
166       Moment now = now_mom ();
167       for (vsize i = heads_to_tie_.size ();  i--; )
168         {
169           if (now > heads_to_tie_[i].end_moment_)
170             heads_to_tie_.erase (heads_to_tie_.begin () + i);
171         }
172     }
173 }
174
175 void
176 Tie_engraver::stop_translation_timestep ()
177 {
178   bool wait = to_boolean (get_property ("tieWaitForNote"));
179   if (ties_.size ())
180     {
181       if (!wait)
182         heads_to_tie_.clear ();
183
184       for (vsize i = 0; i < ties_.size (); i++)
185         typeset_tie (ties_[i]);
186
187       ties_.clear ();
188       tie_column_ = 0;
189     }
190
191   vector<Head_event_tuple> new_heads_to_tie;
192   
193   for (vsize i = 0; i < now_heads_.size (); i++)
194     {
195       Grob *head = now_heads_[i];
196       Stream_event *left_ev
197         = unsmob_stream_event (head->get_property ("cause"));
198
199       if (!left_ev)
200         {
201           // may happen for ambituses
202           continue;
203         }
204             
205       
206       SCM left_articulations = left_ev->get_property ("articulations");
207
208       Stream_event *tie_event = 0;
209       Stream_event *tie_stream_event = event_;
210       for (SCM s = left_articulations;
211            !tie_event && !tie_stream_event && scm_is_pair (s);
212            s = scm_cdr (s))
213         {
214           Stream_event *ev = unsmob_stream_event (scm_car (s));
215           if (!ev)
216             continue;
217           
218           if (ev->in_event_class ("tie-event"))
219             tie_event = ev;
220         }
221           
222       if (left_ev && (tie_event || tie_stream_event))
223         {
224           Head_event_tuple event_tup;
225
226           SCM start_definition
227             = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
228
229           event_tup.head_ = head;
230           event_tup.tie_definition_ = start_definition;
231           event_tup.tie_event_ = tie_event;
232           event_tup.tie_stream_event_ = tie_stream_event;
233
234           Moment end = now_mom ();
235           if (end.grace_part_)
236             {
237               end.grace_part_ += get_event_length (left_ev).main_part_;
238             }
239           else
240             {
241               end += get_event_length (left_ev);
242             }
243           event_tup.end_moment_ = end;
244             
245           new_heads_to_tie.push_back (event_tup);
246         }
247     }
248
249   if (!wait && new_heads_to_tie.size ())
250     heads_to_tie_.clear ();
251
252   // hmmm, how to do with copy() ?
253   for (vsize i = 0; i < new_heads_to_tie.size (); i++)
254     heads_to_tie_.push_back (new_heads_to_tie[i]);
255   
256   event_ = 0;
257   now_heads_.clear ();
258 }
259
260 void
261 Tie_engraver::typeset_tie (Grob *her)
262 {
263   if (! (Tie::head (her, LEFT) && Tie::head (her, RIGHT)))
264     warning (_ ("lonely tie"));
265
266   Direction d = LEFT;
267   Drul_array<Grob *> new_head_drul;
268   new_head_drul[LEFT] = Tie::head (her, LEFT);
269   new_head_drul[RIGHT] = Tie::head (her, RIGHT);
270   do
271     {
272       if (!Tie::head (her, d))
273         new_head_drul[d] = Tie::head (her, (Direction) - d);
274     }
275   while (flip (&d) != LEFT);
276
277   Spanner *sp = dynamic_cast<Spanner*> (her);
278   sp->set_bound (LEFT, new_head_drul[LEFT]);
279   sp->set_bound (RIGHT, new_head_drul[RIGHT]);
280 }
281
282 ADD_ACKNOWLEDGER (Tie_engraver, note_head);
283 ADD_TRANSLATOR (Tie_engraver,
284                 /* doc */ "Generate ties between noteheads of equal pitch.",
285                 /* create */
286                 "Tie "
287                 "TieColumn ",
288                 /* read */ "tieWaitForNote",
289                 /* write */ "tieMelismaBusy");