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