2 new-lyric-combine-iterator.cc -- implement Lyric_combine_music_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "dispatcher.hh"
10 #include "global-context.hh"
13 #include "international.hh"
14 #include "listener.hh"
15 #include "music-iterator.hh"
18 class Lyric_combine_music_iterator : public Music_iterator
21 Lyric_combine_music_iterator ();
22 Lyric_combine_music_iterator (Lyric_combine_music_iterator const &src);
23 DECLARE_SCHEME_CALLBACK (constructor, ());
25 virtual void construct_children ();
26 virtual Moment pending_moment () const;
27 virtual void do_quit ();
28 virtual void process (Moment);
29 virtual bool run_always ()const;
30 virtual bool ok () const;
31 virtual void derived_mark () const;
32 virtual void derived_substitute (Context *, Context *);
33 void set_music_context (Context *to);
35 bool start_new_syllable ();
36 Context *find_voice ();
37 DECLARE_LISTENER (set_busy);
38 DECLARE_LISTENER (check_new_context);
40 bool pending_grace_lyric_;
42 Context *lyrics_context_;
43 Context *music_context_;
44 SCM lyricsto_voice_name_;
47 Music_iterator *lyric_iter_;
50 Lyric_combine_music_iterator::Lyric_combine_music_iterator ()
53 pending_grace_lyric_ = false;
60 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, set_busy)
62 Lyric_combine_music_iterator::set_busy (SCM se)
64 Stream_event *e = unsmob_stream_event (se);
66 if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
71 Lyric_combine_music_iterator::set_music_context (Context *to)
75 music_context_->event_source()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
76 lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
81 to->event_source()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
83 lyrics_context_->set_property ("associatedVoiceContext", to->self_scm ());
88 Lyric_combine_music_iterator::start_new_syllable ()
98 if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
100 bool m = melisma_busy (music_context_);
109 Lyric_combine_music_iterator::pending_moment () const
119 Lyric_combine_music_iterator::run_always () const
125 Lyric_combine_music_iterator::ok () const
127 return lyric_iter_ && lyric_iter_->ok ();
131 Lyric_combine_music_iterator::derived_mark ()const
134 scm_gc_mark (lyric_iter_->self_scm ());
136 scm_gc_mark (lyrics_context_->self_scm ());
138 scm_gc_mark (music_context_->self_scm ());
142 Lyric_combine_music_iterator::derived_substitute (Context *f, Context *t)
145 lyric_iter_->substitute_outlet (f, t);
146 if (lyrics_context_ && lyrics_context_ == f)
148 if (music_context_ && music_context_ == f)
149 set_music_context (t);
153 Lyric_combine_music_iterator::construct_children ()
155 Music *m = unsmob_music (get_music ()->get_property ("element"));
156 lyric_iter_ = unsmob_iterator (get_iterator (m));
159 lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
160 ly_symbol2scm ("Lyrics"), "");
162 if (!lyrics_context_)
164 m->origin ()->warning ("argument of \\lyricsto should contain Lyrics context");
167 lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
169 Context *voice = find_voice ();
171 set_music_context (voice);
175 Wait for a Create_context event. If this isn't done, lyrics can be
176 delayed when voices are created implicitly.
178 Global_context *g = get_outlet ()->get_global_context ();
179 g->events_below ()->add_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
183 We do not create a Lyrics context, because the user might
184 create one with a different name, and then we will not find that
189 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, check_new_context)
191 Lyric_combine_music_iterator::check_new_context (SCM sev)
193 // TODO: Check first if type=Voice and if id matches
196 Context *voice = find_voice ();
199 set_music_context (voice);
201 Global_context *g = voice->get_global_context ();
202 g->events_below ()->remove_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
207 Look for a suitable voice to align lyrics to.
209 Returns 0 if nothing should change; i.e., if we already listen to the
210 right voice, or if we don't yet listen to a voice but no appropriate
211 voice could be found.
214 Lyric_combine_music_iterator::find_voice ()
216 SCM voice_name = lyricsto_voice_name_;
217 SCM running = lyrics_context_
218 ? lyrics_context_->get_property ("associatedVoice")
221 if (scm_is_string (running))
222 voice_name = running;
224 if (scm_is_string (voice_name)
225 && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
227 Context *t = get_outlet ();
228 while (t && t->get_parent_context ())
229 t = t->get_parent_context ();
231 string name = ly_scm2string (voice_name);
232 return find_context_below (t, ly_symbol2scm ("Voice"), name);
239 Lyric_combine_music_iterator::process (Moment)
241 /* see if associatedVoice has been changed */
242 Context *new_voice = find_voice ();
244 set_music_context (new_voice);
249 if (!music_context_->get_parent_context ())
256 lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
258 set_music_context (0);
262 && (start_new_syllable () || pending_grace_lyric_)
263 && lyric_iter_->ok ())
265 if (music_context_->now_mom ().grace_part_)
267 pending_grace_lyric_ = true;
271 pending_grace_lyric_ = false;
273 Moment m = lyric_iter_->pending_moment ();
274 lyric_iter_->process (m);
281 Lyric_combine_music_iterator::do_quit ()
285 SCM voice_name = get_music ()->get_property ("associated-context");
288 if (scm_is_string (voice_name))
289 name = ly_scm2string (voice_name);
291 get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
292 name.c_str ()) + "\n");
296 lyric_iter_->quit ();
299 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);