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);
66 SCM mus = e->get_property ("music");
67 Music *m = unsmob_music (mus);
70 if (m->is_mus_type ("note-event") || m->is_mus_type ("cluster-note-event"))
75 Lyric_combine_music_iterator::set_music_context (Context *to)
79 music_context_->event_source()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("OldMusicEvent"));
80 lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
85 to->event_source()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("OldMusicEvent"));
86 lyrics_context_->set_property ("associatedVoiceContext", to->self_scm ());
91 Lyric_combine_music_iterator::start_new_syllable ()
101 if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
103 bool m = melisma_busy (music_context_);
112 Lyric_combine_music_iterator::pending_moment () const
122 Lyric_combine_music_iterator::run_always () const
128 Lyric_combine_music_iterator::ok () const
130 return lyric_iter_ && lyric_iter_->ok ();
134 Lyric_combine_music_iterator::derived_mark ()const
137 scm_gc_mark (lyric_iter_->self_scm ());
139 scm_gc_mark (lyrics_context_->self_scm ());
141 scm_gc_mark (music_context_->self_scm ());
145 Lyric_combine_music_iterator::derived_substitute (Context *f, Context *t)
148 lyric_iter_->substitute_outlet (f, t);
149 if (lyrics_context_ && lyrics_context_ == f)
151 if (music_context_ && music_context_ == f)
152 set_music_context (t);
156 Lyric_combine_music_iterator::construct_children ()
158 Music *m = unsmob_music (get_music ()->get_property ("element"));
159 lyric_iter_ = unsmob_iterator (get_iterator (m));
162 lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
163 ly_symbol2scm ("Lyrics"), "");
165 lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
167 Context *voice = find_voice ();
169 set_music_context (voice);
173 Wait for a Create_context event. If this isn't done, lyrics can be
174 delayed when voices are created implicitly.
176 Global_context *g = lyrics_context_->get_global_context ();
177 g->events_below ()->add_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
181 We do not create a Lyrics context, because the user might
182 create one with a different name, and then we will not find that
187 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, check_new_context)
189 Lyric_combine_music_iterator::check_new_context (SCM sev)
191 // TODO: Check first if type=Voice and if id matches
194 Context *voice = find_voice ();
197 set_music_context (voice);
199 Global_context *g = voice->get_global_context ();
200 g->events_below ()->remove_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
205 Look for a suitable voice to align lyrics to.
207 Returns 0 if nothing should change; i.e., if we already listen to the
208 right voice, or if we don't yet listen to a voice but no appropriate
209 voice could be found.
212 Lyric_combine_music_iterator::find_voice ()
214 SCM voice_name = lyricsto_voice_name_;
215 SCM running = lyrics_context_
216 ? lyrics_context_->get_property ("associatedVoice")
219 if (scm_is_string (running))
220 voice_name = running;
222 if (scm_is_string (voice_name)
223 && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
225 Context *t = get_outlet ();
226 while (t && t->get_parent_context ())
227 t = t->get_parent_context ();
229 string name = ly_scm2string (voice_name);
230 return find_context_below (t, ly_symbol2scm ("Voice"), name);
237 Lyric_combine_music_iterator::process (Moment)
239 /* see if associatedVoice has been changed */
240 Context *new_voice = find_voice ();
242 set_music_context (new_voice);
247 if (!music_context_->get_parent_context ())
254 lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
256 set_music_context (0);
260 && (start_new_syllable () || pending_grace_lyric_)
261 && lyric_iter_->ok ())
263 if (music_context_->now_mom ().grace_part_)
265 pending_grace_lyric_ = true;
269 pending_grace_lyric_ = false;
271 Moment m = lyric_iter_->pending_moment ();
272 lyric_iter_->process (m);
279 Lyric_combine_music_iterator::do_quit ()
283 SCM voice_name = get_music ()->get_property ("associated-context");
286 if (scm_is_string (voice_name))
287 name = ly_scm2string (voice_name);
289 get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
290 name.c_str ()) + "\n");
294 lyric_iter_->quit ();
297 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);