2 tie-engraver.cc -- implement Tie_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "grob-pitch-tuple.hh"
13 #include "international.hh"
15 #include "note-head.hh"
16 #include "protected-scm.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "stream-event.hh"
20 #include "tie-column.hh"
24 #include "translator.icc"
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.
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).
35 struct Head_event_tuple
40 Stream_event *tie_stream_event_;
46 tie_definition_ = SCM_EOL;
48 tie_stream_event_ = 0;
52 class Tie_engraver : public Engraver
55 vector<Grob*> now_heads_;
56 vector<Head_event_tuple> heads_to_tie_;
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 *);
70 TRANSLATOR_DECLARATIONS (Tie_engraver);
74 Tie_engraver::derived_mark () const
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_);
81 Tie_engraver::Tie_engraver ()
87 IMPLEMENT_TRANSLATOR_LISTENER (Tie_engraver, tie);
89 Tie_engraver::listen_tie (Stream_event *ev)
95 Tie_engraver::process_music ()
97 for (vsize i = 0; i < heads_to_tie_.size (); i++)
98 if (heads_to_tie_[i].tie_event_
99 || heads_to_tie_[i].tie_stream_event_)
101 context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
107 Tie_engraver::acknowledge_note_head (Grob_info i)
111 now_heads_.push_back (h);
112 for (vsize i = heads_to_tie_.size (); i--;)
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"));
119 maybe should check positions too.
121 if (!right_ev || !left_ev)
124 if (ly_is_equal (right_ev->get_property ("pitch"),
125 left_ev->get_property ("pitch")))
127 Grob *p = new Spanner (heads_to_tie_[i].tie_definition_,
128 context ()->get_grob_key ("Tie"));
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 ();
134 announce_grob (p, cause);
135 Tie::set_head (p, LEFT, th);
136 Tie::set_head (p, RIGHT, h);
139 heads_to_tie_.erase (heads_to_tie_.begin () + i);
143 if (ties_.size () && ! tie_column_)
144 tie_column_ = make_spanner ("TieColumn", ties_[0]->self_scm ());
147 for (vsize i = ties_.size (); i--;)
148 Tie_column::add_tie (tie_column_, ties_[i]);
152 Tie_engraver::start_translation_timestep ()
154 context ()->set_property ("tieMelismaBusy",
155 ly_bool2scm (heads_to_tie_.size ()));
157 if (!to_boolean (get_property ("tieWaitForNote")))
159 Moment now = now_mom ();
160 for (vsize i = heads_to_tie_.size (); i--; )
162 if (now > heads_to_tie_[i].end_moment_)
163 heads_to_tie_.erase (heads_to_tie_.begin () + i);
169 Tie_engraver::stop_translation_timestep ()
171 bool wait = to_boolean (get_property ("tieWaitForNote"));
175 heads_to_tie_.clear ();
177 for (vsize i = 0; i < ties_.size (); i++)
178 typeset_tie (ties_[i]);
185 heads_to_tie_.clear ();
187 for (vsize i = 0; i < now_heads_.size (); i++)
189 Grob *head = now_heads_[i];
190 Stream_event *left_ev
191 = unsmob_stream_event (head->get_property ("cause"));
193 SCM left_articulations = left_ev->get_property ("articulations");
195 Music *tie_event = 0;
196 Stream_event *tie_stream_event = event_;
197 for (SCM s = left_articulations;
198 !tie_event && !tie_stream_event && scm_is_pair (s);
201 Music *m = unsmob_music (scm_car (s));
205 if (m->is_mus_type ("tie-event"))
209 if (left_ev && (tie_event || tie_stream_event))
211 Head_event_tuple event_tup;
214 = updated_grob_properties (context (), ly_symbol2scm ("Tie"));
216 event_tup.head_ = head;
217 event_tup.tie_definition_ = start_definition;
218 event_tup.tie_event_ = tie_event;
219 event_tup.tie_stream_event_ = tie_stream_event;
221 Moment end = now_mom ();
224 end.grace_part_ += get_event_length (left_ev).main_part_;
228 end += get_event_length (left_ev);
230 event_tup.end_moment_ = end;
232 heads_to_tie_.push_back (event_tup);
241 Tie_engraver::typeset_tie (Grob *her)
243 if (! (Tie::head (her, LEFT) && Tie::head (her, RIGHT)))
244 warning (_ ("lonely tie"));
247 Drul_array<Grob *> new_head_drul;
248 new_head_drul[LEFT] = Tie::head (her, LEFT);
249 new_head_drul[RIGHT] = Tie::head (her, RIGHT);
252 if (!Tie::head (her, d))
253 new_head_drul[d] = Tie::head (her, (Direction) - d);
255 while (flip (&d) != LEFT);
257 Spanner *sp = dynamic_cast<Spanner*> (her);
258 sp->set_bound (LEFT, new_head_drul[LEFT]);
259 sp->set_bound (RIGHT, new_head_drul[RIGHT]);
262 ADD_ACKNOWLEDGER (Tie_engraver, note_head);
263 ADD_TRANSLATOR (Tie_engraver,
264 /* doc */ "Generate ties between noteheads of equal pitch.",
269 /* accept */ "tie-event",
270 /* read */ "tieWaitForNote",
271 /* write */ "tieMelismaBusy");