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>
10 #include "dispatcher.hh"
11 #include "global-context.hh"
14 #include "international.hh"
15 #include "listener.hh"
16 #include "music-iterator.hh"
19 class Lyric_combine_music_iterator : public Music_iterator
22 Lyric_combine_music_iterator ();
23 Lyric_combine_music_iterator (Lyric_combine_music_iterator const &src);
24 DECLARE_SCHEME_CALLBACK (constructor, ());
26 virtual void construct_children ();
27 virtual Moment pending_moment () const;
28 virtual void do_quit ();
29 virtual void process (Moment);
30 virtual bool run_always ()const;
31 virtual bool ok () const;
32 virtual void derived_mark () const;
33 virtual void derived_substitute (Context *, Context *);
34 void set_music_context (Context *to);
36 bool start_new_syllable ();
37 Context *find_voice ();
38 DECLARE_LISTENER (set_busy);
39 DECLARE_LISTENER (check_new_context);
41 bool pending_grace_lyric_;
43 Context *lyrics_context_;
44 Context *music_context_;
45 SCM lyricsto_voice_name_;
48 Music_iterator *lyric_iter_;
51 Lyric_combine_music_iterator::Lyric_combine_music_iterator ()
54 pending_grace_lyric_ = false;
61 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, set_busy)
63 Lyric_combine_music_iterator::set_busy (SCM se)
65 Stream_event *e = unsmob_stream_event (se);
67 if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
72 Lyric_combine_music_iterator::set_music_context (Context *to)
76 music_context_->event_source()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
77 lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
82 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 lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
164 Context *voice = find_voice ();
166 set_music_context (voice);
170 Wait for a Create_context event. If this isn't done, lyrics can be
171 delayed when voices are created implicitly.
173 Global_context *g = lyrics_context_->get_global_context ();
174 g->events_below ()->add_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
178 We do not create a Lyrics context, because the user might
179 create one with a different name, and then we will not find that
184 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, check_new_context)
186 Lyric_combine_music_iterator::check_new_context (SCM sev)
188 // TODO: Check first if type=Voice and if id matches
191 Context *voice = find_voice ();
194 set_music_context (voice);
196 Global_context *g = voice->get_global_context ();
197 g->events_below ()->remove_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
202 Look for a suitable voice to align lyrics to.
204 Returns 0 if nothing should change; i.e., if we already listen to the
205 right voice, or if we don't yet listen to a voice but no appropriate
206 voice could be found.
209 Lyric_combine_music_iterator::find_voice ()
211 SCM voice_name = lyricsto_voice_name_;
212 SCM running = lyrics_context_
213 ? lyrics_context_->get_property ("associatedVoice")
216 if (scm_is_string (running))
217 voice_name = running;
219 if (scm_is_string (voice_name)
220 && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
222 Context *t = get_outlet ();
223 while (t && t->get_parent_context ())
224 t = t->get_parent_context ();
226 string name = ly_scm2string (voice_name);
227 return find_context_below (t, ly_symbol2scm ("Voice"), name);
234 Lyric_combine_music_iterator::process (Moment)
236 /* see if associatedVoice has been changed */
237 Context *new_voice = find_voice ();
239 set_music_context (new_voice);
244 if (!music_context_->get_parent_context ())
251 lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
253 set_music_context (0);
257 && (start_new_syllable () || pending_grace_lyric_)
258 && lyric_iter_->ok ())
260 if (music_context_->now_mom ().grace_part_)
262 pending_grace_lyric_ = true;
266 pending_grace_lyric_ = false;
268 Moment m = lyric_iter_->pending_moment ();
269 lyric_iter_->process (m);
276 Lyric_combine_music_iterator::do_quit ()
280 SCM voice_name = get_music ()->get_property ("associated-context");
283 if (scm_is_string (voice_name))
284 name = ly_scm2string (voice_name);
286 get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
287 name.c_str ()) + "\n");
291 lyric_iter_->quit ();
294 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);