2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "dispatcher.hh"
21 #include "global-context.hh"
24 #include "international.hh"
25 #include "listener.hh"
26 #include "music-iterator.hh"
30 This iterator is hairy. It tracks both lyric and melody contexts,
31 and has a complicated communication route, reading/writing
34 In the future, this should rather be done with
36 \interpretAsMelodyFor { MUSIC } { LYRICS LYRICS LYRICS }
38 This can run an interpret step on MUSIC, generating a stream. Then
39 the stream can be perused at leisure to apply durations to all of
43 class Lyric_combine_music_iterator : public Music_iterator
46 Lyric_combine_music_iterator ();
47 Lyric_combine_music_iterator (Lyric_combine_music_iterator const &src);
48 DECLARE_SCHEME_CALLBACK (constructor, ());
50 virtual void construct_children ();
51 virtual Moment pending_moment () const;
52 virtual void do_quit ();
53 virtual void process (Moment);
54 virtual bool run_always ()const;
55 virtual bool ok () const;
56 virtual void derived_mark () const;
57 virtual void derived_substitute (Context *, Context *);
58 void set_music_context (Context *to);
60 bool start_new_syllable () const;
61 Context *find_voice ();
62 DECLARE_LISTENER (set_busy);
63 DECLARE_LISTENER (check_new_context);
67 Context *lyrics_context_;
68 Context *music_context_;
69 SCM lyricsto_voice_name_;
72 Moment pending_grace_moment_;
74 Music_iterator *lyric_iter_;
77 Lyric_combine_music_iterator::Lyric_combine_music_iterator ()
80 lyrics_found_ = false;
81 pending_grace_moment_.set_infinite (1);
85 busy_moment_.set_infinite (-1);
90 It's dubious whether we can ever make this fully work. Due to
91 associatedVoice switching, this routine may be triggered for
92 the wrong music_context_
94 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, set_busy)
96 Lyric_combine_music_iterator::set_busy (SCM se)
98 Stream_event *e = unsmob_stream_event (se);
100 if ((e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
103 busy_moment_ = max (music_context_->now_mom (),
109 Lyric_combine_music_iterator::set_music_context (Context *to)
113 music_context_->event_source ()->
114 remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
120 to->event_source ()->add_listener (GET_LISTENER (set_busy),
121 ly_symbol2scm ("music-event"));
126 Lyric_combine_music_iterator::start_new_syllable () const
128 if (busy_moment_ < music_context_->now_mom ())
131 if (!lyrics_context_)
134 if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
136 bool m = melisma_busy (music_context_);
145 Lyric_combine_music_iterator::pending_moment () const
155 Lyric_combine_music_iterator::run_always () const
161 Lyric_combine_music_iterator::ok () const
163 return lyric_iter_ && lyric_iter_->ok ();
167 Lyric_combine_music_iterator::derived_mark ()const
170 scm_gc_mark (lyric_iter_->self_scm ());
172 scm_gc_mark (lyrics_context_->self_scm ());
174 scm_gc_mark (music_context_->self_scm ());
178 Lyric_combine_music_iterator::derived_substitute (Context *f, Context *t)
181 lyric_iter_->substitute_outlet (f, t);
182 if (lyrics_context_ && lyrics_context_ == f)
184 if (music_context_ && music_context_ == f)
185 set_music_context (t);
189 Lyric_combine_music_iterator::construct_children ()
191 Music *m = unsmob_music (get_music ()->get_property ("element"));
192 lyric_iter_ = unsmob_iterator (get_iterator (m));
195 lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
196 ly_symbol2scm ("Lyrics"), "");
198 if (!lyrics_context_)
200 m->origin ()->warning ("argument of \\lyricsto should contain Lyrics context");
203 lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
205 Context *voice = find_voice ();
207 set_music_context (voice);
210 Wait for a Create_context event. If this isn't done, lyrics can be
211 delayed when voices are created implicitly.
213 Global_context *g = get_outlet ()->get_global_context ();
214 g->events_below ()->add_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
217 We do not create a Lyrics context, because the user might
218 create one with a different name, and then we will not find that
223 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, check_new_context)
225 Lyric_combine_music_iterator::check_new_context (SCM sev)
230 // TODO: Check first if type=Voice and if id matches
231 Stream_event * ev = unsmob_stream_event (sev);
232 if (ev->get_property ("type") != ly_symbol2scm ("Voice"))
235 Context *voice = find_voice ();
239 set_music_context (voice);
244 Look for a suitable voice to align lyrics to.
246 Returns 0 if nothing should change; i.e., if we already listen to the
247 right voice, or if we don't yet listen to a voice but no appropriate
248 voice could be found.
251 Lyric_combine_music_iterator::find_voice ()
253 SCM voice_name = lyricsto_voice_name_;
254 SCM running = lyrics_context_
255 ? lyrics_context_->get_property ("associatedVoice")
258 if (scm_is_string (running))
259 voice_name = running;
261 if (scm_is_string (voice_name)
262 && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
264 Context *t = get_outlet ();
265 while (t && t->get_parent_context ())
266 t = t->get_parent_context ();
268 string name = ly_scm2string (voice_name);
269 return find_context_below (t, ly_symbol2scm ("Voice"), name);
276 Lyric_combine_music_iterator::process (Moment /* when */)
278 /* see if associatedVoice has been changed */
279 Context *new_voice = find_voice ();
281 set_music_context (new_voice);
283 lyrics_found_ = true;
287 if (!music_context_->get_parent_context ())
294 lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
296 set_music_context (0);
300 && (start_new_syllable () ||
301 (busy_moment_ >= pending_grace_moment_))
302 && lyric_iter_->ok ())
304 Moment now = music_context_->now_mom ();
305 if (now.grace_part_ && !to_boolean (lyrics_context_->get_property ("includeGraceNotes")))
307 pending_grace_moment_ = now;
308 pending_grace_moment_.grace_part_ = Rational (0);
313 pending_grace_moment_.set_infinite (1);
316 Moment m = lyric_iter_->pending_moment ();
317 lyrics_context_->set_property (ly_symbol2scm ("associatedVoiceContext"),
318 music_context_->self_scm ());
319 lyric_iter_->process (m);
324 new_voice = find_voice ();
326 set_music_context (new_voice);
330 Lyric_combine_music_iterator::do_quit ()
334 SCM voice_name = get_music ()->get_property ("associated-context");
337 if (scm_is_string (voice_name))
338 name = ly_scm2string (voice_name);
339 /* Don't print a warning for empty lyrics (in which case we don't try
340 to find the proper voice, so it will not be found) */
342 get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
343 name.c_str ()) + "\n");
347 lyric_iter_->quit ();
350 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);