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"
19 This iterator is hairy. It tracks both lyric and melody contexts,
20 and has a complicated communication route, reading/writing
23 In the future, this should rather be done with
25 \interpretAsMelodyFor { MUSIC } { LYRICS LYRICS LYRICS }
27 This can run an interpret step on MUSIC, generating a stream. Then
28 the stream can be perused at leisure to apply durations to all of
32 class Lyric_combine_music_iterator : public Music_iterator
35 Lyric_combine_music_iterator ();
36 Lyric_combine_music_iterator (Lyric_combine_music_iterator const &src);
37 DECLARE_SCHEME_CALLBACK (constructor, ());
39 virtual void construct_children ();
40 virtual Moment pending_moment () const;
41 virtual void do_quit ();
42 virtual void process (Moment);
43 virtual bool run_always ()const;
44 virtual bool ok () const;
45 virtual void derived_mark () const;
46 virtual void derived_substitute (Context *, Context *);
47 void set_music_context (Context *to);
49 bool start_new_syllable () const;
50 Context *find_voice ();
51 DECLARE_LISTENER (set_busy);
52 DECLARE_LISTENER (check_new_context);
54 bool pending_grace_lyric_;
56 Context *lyrics_context_;
57 Context *music_context_;
58 SCM lyricsto_voice_name_;
61 Music_iterator *lyric_iter_;
64 Lyric_combine_music_iterator::Lyric_combine_music_iterator ()
67 pending_grace_lyric_ = false;
74 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, set_busy)
76 Lyric_combine_music_iterator::set_busy (SCM se)
78 Stream_event *e = unsmob_stream_event (se);
80 if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
85 Lyric_combine_music_iterator::set_music_context (Context *to)
89 music_context_->event_source()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
90 lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
96 to->event_source()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
98 lyrics_context_->set_property ("associatedVoiceContext", to->self_scm ());
103 Lyric_combine_music_iterator::start_new_syllable () const
108 if (!lyrics_context_)
111 if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
113 bool m = melisma_busy (music_context_);
122 Lyric_combine_music_iterator::pending_moment () const
132 Lyric_combine_music_iterator::run_always () const
138 Lyric_combine_music_iterator::ok () const
140 return lyric_iter_ && lyric_iter_->ok ();
144 Lyric_combine_music_iterator::derived_mark ()const
147 scm_gc_mark (lyric_iter_->self_scm ());
149 scm_gc_mark (lyrics_context_->self_scm ());
151 scm_gc_mark (music_context_->self_scm ());
155 Lyric_combine_music_iterator::derived_substitute (Context *f, Context *t)
158 lyric_iter_->substitute_outlet (f, t);
159 if (lyrics_context_ && lyrics_context_ == f)
161 if (music_context_ && music_context_ == f)
162 set_music_context (t);
166 Lyric_combine_music_iterator::construct_children ()
168 Music *m = unsmob_music (get_music ()->get_property ("element"));
169 lyric_iter_ = unsmob_iterator (get_iterator (m));
172 lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
173 ly_symbol2scm ("Lyrics"), "");
175 if (!lyrics_context_)
177 m->origin ()->warning ("argument of \\lyricsto should contain Lyrics context");
180 lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
182 Context *voice = find_voice ();
184 set_music_context (voice);
187 Wait for a Create_context event. If this isn't done, lyrics can be
188 delayed when voices are created implicitly.
190 Global_context *g = get_outlet ()->get_global_context ();
191 g->events_below ()->add_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
194 We do not create a Lyrics context, because the user might
195 create one with a different name, and then we will not find that
200 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, check_new_context)
202 Lyric_combine_music_iterator::check_new_context (SCM sev)
204 // TODO: Check first if type=Voice and if id matches
205 Stream_event * ev = unsmob_stream_event (sev);
206 if (ev->get_property ("type") != ly_symbol2scm ("Voice"))
209 Context *voice = find_voice ();
213 set_music_context (voice);
218 Look for a suitable voice to align lyrics to.
220 Returns 0 if nothing should change; i.e., if we already listen to the
221 right voice, or if we don't yet listen to a voice but no appropriate
222 voice could be found.
225 Lyric_combine_music_iterator::find_voice ()
227 SCM voice_name = lyricsto_voice_name_;
228 SCM running = lyrics_context_
229 ? lyrics_context_->get_property ("associatedVoice")
232 if (scm_is_string (running))
233 voice_name = running;
235 if (scm_is_string (voice_name)
236 && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
238 Context *t = get_outlet ();
239 while (t && t->get_parent_context ())
240 t = t->get_parent_context ();
242 string name = ly_scm2string (voice_name);
243 return find_context_below (t, ly_symbol2scm ("Voice"), name);
250 Lyric_combine_music_iterator::process (Moment)
252 /* see if associatedVoice has been changed */
253 Context *new_voice = find_voice ();
255 set_music_context (new_voice);
260 if (!music_context_->get_parent_context ())
267 lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
269 set_music_context (0);
274 && (start_new_syllable () || pending_grace_lyric_)
275 && lyric_iter_->ok ())
278 if (music_context_->now_mom ().grace_part_)
280 pending_grace_lyric_ = true;
284 pending_grace_lyric_ = false;
286 Moment m = lyric_iter_->pending_moment ();
287 lyric_iter_->process (m);
294 Lyric_combine_music_iterator::do_quit ()
298 SCM voice_name = get_music ()->get_property ("associated-context");
301 if (scm_is_string (voice_name))
302 name = ly_scm2string (voice_name);
304 get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
305 name.c_str ()) + "\n");
309 lyric_iter_->quit ();
312 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);