]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / tie-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "engraver.hh"
21
22 #include "context.hh"
23 #include "international.hh"
24 #include "item.hh"
25 #include "note-head.hh"
26 #include "protected-scm.hh"
27 #include "spanner.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "stream-event.hh"
30 #include "tie-column.hh"
31 #include "tie.hh"
32 #include "warn.hh"
33
34 #include "translator.icc"
35
36 /**
37    Manufacture ties.  Acknowledge noteheads, and put them into a
38    priority queue. If we have a TieEvent, connect the notes that finish
39    just at this time, and note that start at this time.
40
41    TODO: Remove the dependency on musical info. We should tie on the
42    basis of position and duration-log of the heads (not of the events).
43 */
44
45 struct Head_event_tuple
46 {
47   Grob *head_;
48   Moment end_moment_;
49   SCM tie_definition_;
50   Stream_event *tie_stream_event_;
51   Stream_event *tie_event_;
52   // Indicate whether a tie from the same moment has been processed successfully
53   // This is needed for tied chords, e.g. <c e g>~ g, because otherwise the c
54   // and e will trigger a warning for an unterminated tie!
55   bool tie_from_chord_created;
56   
57   Head_event_tuple ()
58   {
59     head_ = 0;
60     tie_definition_ = SCM_EOL;
61     tie_event_ = 0;
62     tie_stream_event_ = 0;
63     tie_from_chord_created = false;
64   }
65 };
66
67 class Tie_engraver : public Engraver
68 {
69   Stream_event *event_;
70   vector<Grob*> now_heads_;
71   vector<Head_event_tuple> heads_to_tie_;
72   vector<Grob*> ties_;
73
74   Spanner *tie_column_;
75
76 protected:
77   void stop_translation_timestep ();
78   virtual void derived_mark () const;
79   void start_translation_timestep ();
80   DECLARE_ACKNOWLEDGER (note_head);
81   DECLARE_TRANSLATOR_LISTENER (tie);
82   void process_music ();
83   void typeset_tie (Grob *);
84   void report_unterminated_tie (Head_event_tuple const &);
85 public:
86   TRANSLATOR_DECLARATIONS (Tie_engraver);
87 };
88
89 void
90 Tie_engraver::derived_mark () const
91 {
92   Engraver::derived_mark ();
93   for (vsize i = 0; i < heads_to_tie_.size (); i++)
94     scm_gc_mark (heads_to_tie_[i].tie_definition_);
95 }
96
97 Tie_engraver::Tie_engraver ()
98 {
99   event_ = 0;
100   tie_column_ = 0;
101 }
102
103 IMPLEMENT_TRANSLATOR_LISTENER (Tie_engraver, tie);
104 void
105 Tie_engraver::listen_tie (Stream_event *ev)
106 {
107   ASSIGN_EVENT_ONCE (event_, ev);
108 }
109
110 void Tie_engraver::report_unterminated_tie (Head_event_tuple const &tie_start)
111 {
112   // If tie_from_chord_created is set, we have another note at the same
113   // moment that created a tie, so this is not necessarily an unterminated
114   // tie. Happens e.g. for <c e g>~ g
115   if (!tie_start.tie_from_chord_created)
116     tie_start.head_->warning (_("unterminated tie"));
117 }
118
119 void
120 Tie_engraver::process_music ()
121 {
122   bool busy = event_;
123   for (vsize i = 0; !busy && i < heads_to_tie_.size (); i++)
124     busy |=  (heads_to_tie_[i].tie_event_
125               || heads_to_tie_[i].tie_stream_event_);
126
127   if (busy)
128     context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
129 }
130
131 void
132 Tie_engraver::acknowledge_note_head (Grob_info i)
133 {
134   Grob *h = i.grob ();
135
136   now_heads_.push_back (h);
137   for (vsize i = heads_to_tie_.size (); i--;)
138     {
139       Grob *th = heads_to_tie_[i].head_;
140       Stream_event *right_ev = unsmob_stream_event (h->get_property ("cause"));
141       Stream_event *left_ev = unsmob_stream_event (th->get_property ("cause"));
142
143       /*
144         maybe should check positions too.
145       */
146       if (!right_ev || !left_ev)
147         continue;
148       
149       if (ly_is_equal (right_ev->get_property ("pitch"),
150                        left_ev->get_property ("pitch")))
151         {
152           Grob *p = new Spanner (heads_to_tie_[i].tie_definition_);
153           Moment end = heads_to_tie_[i].end_moment_;
154
155           SCM cause = heads_to_tie_[i].tie_event_
156             ? heads_to_tie_[i].tie_event_->self_scm ()
157             : heads_to_tie_[i].tie_stream_event_->self_scm ();
158           
159           announce_grob (p, cause);
160           Tie::set_head (p, LEFT, th);
161           Tie::set_head (p, RIGHT, h);
162
163
164           if (is_direction (unsmob_stream_event (cause)->get_property ("direction")))
165             {
166               Direction d = to_dir (unsmob_stream_event (cause)->get_property ("direction"));
167               p->set_property ("direction", scm_from_int (d)); 
168             }
169           
170           ties_.push_back (p);
171           heads_to_tie_.erase (heads_to_tie_.begin () + i);
172
173           // Prevent all other tied notes ending at the same moment (assume
174           // implicitly the notes have also started at the same moment!)
175           // from triggering an "unterminated tie" warning. Neede e.g. for
176           // <c e g>~ g
177           for (vsize j = heads_to_tie_.size (); j--;)
178             {
179               if (heads_to_tie_[j].end_moment_ == end)
180                 heads_to_tie_[i].tie_from_chord_created = true;
181             }
182         }
183     }
184
185   if (ties_.size () && ! tie_column_)
186     tie_column_ = make_spanner ("TieColumn", ties_[0]->self_scm ());
187
188   if (tie_column_)
189     for (vsize i = ties_.size (); i--;)
190       Tie_column::add_tie (tie_column_, ties_[i]);
191 }
192
193 void
194 Tie_engraver::start_translation_timestep ()
195 {
196   context ()->set_property ("tieMelismaBusy",
197                             ly_bool2scm (heads_to_tie_.size ()));
198
199   if (heads_to_tie_.size () && !to_boolean (get_property ("tieWaitForNote")))
200     {
201       Moment now = now_mom ();
202       for (vsize i = heads_to_tie_.size ();  i--; )
203         {
204           if (now > heads_to_tie_[i].end_moment_)
205             {
206               report_unterminated_tie (heads_to_tie_[i]);
207               heads_to_tie_.erase (heads_to_tie_.begin () + i);
208             }
209         }
210     }
211 }
212
213 void
214 Tie_engraver::stop_translation_timestep ()
215 {
216   bool wait = to_boolean (get_property ("tieWaitForNote"));
217   if (ties_.size ())
218     {
219       if (!wait)
220         {
221           vector<Head_event_tuple>::iterator it = heads_to_tie_.begin ();
222           for (; it < heads_to_tie_.end (); it++)
223             report_unterminated_tie (*it);
224           heads_to_tie_.clear ();
225         }
226
227       for (vsize i = 0; i < ties_.size (); i++)
228           typeset_tie (ties_[i]);
229
230       ties_.clear ();
231       tie_column_ = 0;
232     }
233
234   vector<Head_event_tuple> new_heads_to_tie;
235   
236   for (vsize i = 0; i < now_heads_.size (); i++)
237     {
238       Grob *head = now_heads_[i];
239       Stream_event *left_ev
240         = unsmob_stream_event (head->get_property ("cause"));
241
242       if (!left_ev)
243         {
244           // may happen for ambituses
245           continue;
246         }
247             
248       
249       SCM left_articulations = left_ev->get_property ("articulations");
250
251       Stream_event *tie_event = 0;
252       Stream_event *tie_stream_event = event_;
253       for (SCM s = left_articulations;
254            !tie_event && !tie_stream_event && scm_is_pair (s);
255            s = scm_cdr (s))
256         {
257           Stream_event *ev = unsmob_stream_event (scm_car (s));
258           if (!ev)
259             continue;
260           
261           if (ev->in_event_class ("tie-event"))
262             tie_event = ev;
263         }
264           
265       if (left_ev && (tie_event || tie_stream_event))
266         {
267           Head_event_tuple event_tup;
268
269           SCM start_definition
270             = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
271
272           event_tup.head_ = head;
273           event_tup.tie_definition_ = start_definition;
274           event_tup.tie_event_ = tie_event;
275           event_tup.tie_stream_event_ = tie_stream_event;
276
277           Moment end = now_mom ();
278           if (end.grace_part_)
279             {
280               end.grace_part_ += get_event_length (left_ev).main_part_;
281             }
282           else
283             {
284               end += get_event_length (left_ev);
285             }
286           event_tup.end_moment_ = end;
287             
288           new_heads_to_tie.push_back (event_tup);
289         }
290     }
291
292   if (!wait && new_heads_to_tie.size ())
293     {
294       vector<Head_event_tuple>::iterator it=heads_to_tie_.begin ();
295       for (; it < heads_to_tie_.end (); it++)
296         report_unterminated_tie (*it);
297       heads_to_tie_.clear ();
298     }
299
300   // hmmm, how to do with copy () ?
301   for (vsize i = 0; i < new_heads_to_tie.size (); i++)
302     heads_to_tie_.push_back (new_heads_to_tie[i]);
303   
304   event_ = 0;
305   now_heads_.clear ();
306 }
307
308 void
309 Tie_engraver::typeset_tie (Grob *her)
310 {
311   if (! (Tie::head (her, LEFT) && Tie::head (her, RIGHT)))
312     warning (_ ("lonely tie"));
313
314   Direction d = LEFT;
315   Drul_array<Grob *> new_head_drul;
316   new_head_drul[LEFT] = Tie::head (her, LEFT);
317   new_head_drul[RIGHT] = Tie::head (her, RIGHT);
318   do
319     {
320       if (!Tie::head (her, d))
321         new_head_drul[d] = Tie::head (her, (Direction) - d);
322     }
323   while (flip (&d) != LEFT);
324
325   Spanner *sp = dynamic_cast<Spanner*> (her);
326   sp->set_bound (LEFT, new_head_drul[LEFT]);
327   sp->set_bound (RIGHT, new_head_drul[RIGHT]);
328 }
329
330 ADD_ACKNOWLEDGER (Tie_engraver, note_head);
331 ADD_TRANSLATOR (Tie_engraver,
332                 /* doc */
333                 "Generate ties between note heads of equal pitch.",
334
335                 /* create */
336                 "Tie "
337                 "TieColumn ",
338
339                 /* read */
340                 "tieWaitForNote ",
341
342                 /* write */
343                 "tieMelismaBusy "
344                 );