2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2015 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 ();
63 void check_new_context (SCM);
67 Context *lyrics_context_;
68 Context *music_context_;
69 SCM lyricsto_voice_name_;
70 SCM lyricsto_voice_type_;
73 Moment pending_grace_moment_;
75 Music_iterator *lyric_iter_;
78 Lyric_combine_music_iterator::Lyric_combine_music_iterator ()
81 lyrics_found_ = false;
82 pending_grace_moment_.set_infinite (1);
86 busy_moment_.set_infinite (-1);
87 lyricsto_voice_name_ = SCM_UNDEFINED;
88 lyricsto_voice_type_ = SCM_UNDEFINED;
92 It's dubious whether we can ever make this fully work. Due to
93 associatedVoice switching, this routine may be triggered for
94 the wrong music_context_
97 Lyric_combine_music_iterator::set_busy (SCM se)
99 Stream_event *e = unsmob<Stream_event> (se);
101 if ((e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
104 busy_moment_ = max (music_context_->now_mom (),
110 Lyric_combine_music_iterator::set_music_context (Context *to)
114 music_context_->events_below ()->
115 remove_listener (GET_LISTENER (Lyric_combine_music_iterator, set_busy), ly_symbol2scm ("rhythmic-event"));
121 to->events_below ()->add_listener (GET_LISTENER (Lyric_combine_music_iterator, set_busy),
122 ly_symbol2scm ("rhythmic-event"));
127 Lyric_combine_music_iterator::start_new_syllable () const
129 if (busy_moment_ < music_context_->now_mom ())
132 if (!lyrics_context_)
135 if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
137 bool m = melisma_busy (music_context_);
146 Lyric_combine_music_iterator::pending_moment () const
156 Lyric_combine_music_iterator::run_always () const
162 Lyric_combine_music_iterator::ok () const
164 return lyric_iter_ && lyric_iter_->ok ();
168 Lyric_combine_music_iterator::derived_mark ()const
171 scm_gc_mark (lyric_iter_->self_scm ());
173 scm_gc_mark (lyrics_context_->self_scm ());
175 scm_gc_mark (music_context_->self_scm ());
176 scm_gc_mark (lyricsto_voice_name_);
177 scm_gc_mark (lyricsto_voice_type_);
181 Lyric_combine_music_iterator::derived_substitute (Context *f, Context *t)
184 lyric_iter_->substitute_outlet (f, t);
185 if (lyrics_context_ && lyrics_context_ == f)
187 if (music_context_ && music_context_ == f)
188 set_music_context (t);
192 Lyric_combine_music_iterator::construct_children ()
194 Music *m = unsmob<Music> (get_music ()->get_property ("element"));
195 lyric_iter_ = unsmob<Music_iterator> (get_iterator (m));
198 lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
199 ly_symbol2scm ("Lyrics"), "");
201 if (!lyrics_context_)
203 m->origin ()->warning (_ ("argument of \\lyricsto should contain Lyrics context"));
206 lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
207 lyricsto_voice_type_ = get_music ()->get_property ("associated-context-type");
208 if (!scm_is_symbol (lyricsto_voice_type_))
209 lyricsto_voice_type_ = ly_symbol2scm ("Voice");
211 Context *voice = find_voice ();
213 set_music_context (voice);
216 Wait for a Create_context event. If this isn't done, lyrics can be
217 delayed when voices are created implicitly.
219 Global_context *g = get_outlet ()->get_global_context ();
220 g->events_below ()->add_listener (GET_LISTENER (Lyric_combine_music_iterator, check_new_context), ly_symbol2scm ("CreateContext"));
223 We do not create a Lyrics context, because the user might
224 create one with a different name, and then we will not find that
230 Lyric_combine_music_iterator::check_new_context (SCM /*sev*/)
235 // Search for a possible candidate voice to attach the lyrics to. If none
236 // is found, we'll try next time again.
237 Context *voice = find_voice ();
240 set_music_context (voice);
245 Look for a suitable voice to align lyrics to.
247 Returns 0 if nothing should change; i.e., if we already listen to the
248 right voice, or if we don't yet listen to a voice but no appropriate
249 voice could be found.
252 Lyric_combine_music_iterator::find_voice ()
254 SCM voice_name = lyricsto_voice_name_;
255 SCM running = lyrics_context_
256 ? lyrics_context_->get_property ("associatedVoice")
258 SCM voice_type = lyricsto_voice_type_;
259 if (scm_is_string (running)) {
260 voice_name = running;
261 voice_type = lyrics_context_->get_property ("associatedVoiceType");
264 if (scm_is_string (voice_name)
265 && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ())
266 && scm_is_symbol (voice_type))
268 Context *t = get_outlet ();
269 while (t && t->get_parent_context ())
270 t = t->get_parent_context ();
272 string name = ly_scm2string (voice_name);
273 return find_context_below (t, voice_type, name);
280 Lyric_combine_music_iterator::process (Moment /* when */)
282 /* see if associatedVoice has been changed */
283 Context *new_voice = find_voice ();
285 set_music_context (new_voice);
287 lyrics_found_ = true;
291 if (!music_context_->get_parent_context ())
298 lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
300 set_music_context (0);
304 && (start_new_syllable ()
305 || (busy_moment_ >= pending_grace_moment_))
306 && lyric_iter_->ok ())
308 Moment now = music_context_->now_mom ();
309 if (now.grace_part_ && !to_boolean (lyrics_context_->get_property ("includeGraceNotes")))
311 pending_grace_moment_ = now;
312 pending_grace_moment_.grace_part_ = Rational (0);
317 pending_grace_moment_.set_infinite (1);
320 Moment m = lyric_iter_->pending_moment ();
321 lyrics_context_->set_property (ly_symbol2scm ("associatedVoiceContext"),
322 music_context_->self_scm ());
323 lyric_iter_->process (m);
328 new_voice = find_voice ();
330 set_music_context (new_voice);
334 Lyric_combine_music_iterator::do_quit ()
338 SCM voice_name = get_music ()->get_property ("associated-context");
339 SCM voice_type = get_music ()->get_property ("associated-context-type");
341 if (scm_is_string (voice_name))
342 name = ly_scm2string (voice_name);
343 type = robust_symbol2string (voice_type, "Voice");
344 /* Don't print a warning for empty lyrics (in which case we don't try
345 to find the proper voice, so it will not be found) */
347 get_music ()->origin ()->warning (_f ("cannot find %s `%s'",
348 type.c_str (), name.c_str ()) + "\n");
352 lyric_iter_->quit ();
355 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);