]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-combine-music-iterator.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / lyric-combine-music-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "dispatcher.hh"
21 #include "global-context.hh"
22 #include "grob.hh"
23 #include "input.hh"
24 #include "international.hh"
25 #include "listener.hh"
26 #include "music-iterator.hh"
27 #include "music.hh"
28
29 /*
30   This iterator is hairy.  It tracks both lyric and melody contexts,
31   and has a complicated communication route, reading/writing
32   properties in both.
33
34   In the future, this should rather be done with
35
36      \interpretAsMelodyFor { MUSIC } { LYRICS LYRICS LYRICS }
37
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
40   the LYRICS.
41 */
42
43 class Lyric_combine_music_iterator : public Music_iterator
44 {
45 public:
46   Lyric_combine_music_iterator ();
47   Lyric_combine_music_iterator (Lyric_combine_music_iterator const &src);
48   DECLARE_SCHEME_CALLBACK (constructor, ());
49 protected:
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);
59 private:
60   bool start_new_syllable () const;
61   Context *find_voice ();
62   DECLARE_LISTENER (set_busy);
63   DECLARE_LISTENER (check_new_context);
64
65   bool music_found_;
66   bool lyrics_found_;
67   Context *lyrics_context_;
68   Context *music_context_;
69   SCM lyricsto_voice_name_;
70
71   Moment busy_moment_;
72   Moment pending_grace_moment_;
73
74   Music_iterator *lyric_iter_;
75 };
76
77 Lyric_combine_music_iterator::Lyric_combine_music_iterator ()
78 {
79   music_found_ = false;
80   lyrics_found_ = false;
81   pending_grace_moment_.set_infinite (1);
82   lyric_iter_ = 0;
83   music_context_ = 0;
84   lyrics_context_ = 0;
85   busy_moment_.set_infinite (-1);
86 }
87
88 /*
89   It's dubious whether we can ever make this fully work.  Due to
90   associatedVoice switching, this routine may be triggered for
91   the wrong music_context_
92  */
93 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, set_busy)
94 void
95 Lyric_combine_music_iterator::set_busy (SCM se)
96 {
97   Stream_event *e = unsmob_stream_event (se);
98
99   if ((e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
100       && music_context_)
101
102     busy_moment_ = max (music_context_->now_mom (),
103                         busy_moment_);
104
105 }
106
107 void
108 Lyric_combine_music_iterator::set_music_context (Context *to)
109 {
110   if (music_context_)
111     {
112       music_context_->event_source ()->
113       remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("rhythmic-event"));
114     }
115
116   music_context_ = to;
117   if (to)
118     {
119       to->event_source ()->add_listener (GET_LISTENER (set_busy),
120                                          ly_symbol2scm ("rhythmic-event"));
121     }
122 }
123
124 bool
125 Lyric_combine_music_iterator::start_new_syllable () const
126 {
127   if (busy_moment_ < music_context_->now_mom ())
128     return false;
129
130   if (!lyrics_context_)
131     return false;
132
133   if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
134     {
135       bool m = melisma_busy (music_context_);
136       if (m)
137         return false;
138     }
139
140   return true;
141 }
142
143 Moment
144 Lyric_combine_music_iterator::pending_moment () const
145 {
146   Moment m;
147
148   m.set_infinite (1);
149
150   return m;
151 }
152
153 bool
154 Lyric_combine_music_iterator::run_always () const
155 {
156   return true;
157 }
158
159 bool
160 Lyric_combine_music_iterator::ok () const
161 {
162   return lyric_iter_ && lyric_iter_->ok ();
163 }
164
165 void
166 Lyric_combine_music_iterator::derived_mark ()const
167 {
168   if (lyric_iter_)
169     scm_gc_mark (lyric_iter_->self_scm ());
170   if (lyrics_context_)
171     scm_gc_mark (lyrics_context_->self_scm ());
172   if (music_context_)
173     scm_gc_mark (music_context_->self_scm ());
174 }
175
176 void
177 Lyric_combine_music_iterator::derived_substitute (Context *f, Context *t)
178 {
179   if (lyric_iter_)
180     lyric_iter_->substitute_outlet (f, t);
181   if (lyrics_context_ && lyrics_context_ == f)
182     lyrics_context_ = t;
183   if (music_context_ && music_context_ == f)
184     set_music_context (t);
185 }
186
187 void
188 Lyric_combine_music_iterator::construct_children ()
189 {
190   Music *m = unsmob_music (get_music ()->get_property ("element"));
191   lyric_iter_ = unsmob_iterator (get_iterator (m));
192   if (!lyric_iter_)
193     return;
194   lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
195                                         ly_symbol2scm ("Lyrics"), "");
196
197   if (!lyrics_context_)
198     {
199       m->origin ()->warning (_ ("argument of \\lyricsto should contain Lyrics context"));
200     }
201
202   lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
203
204   Context *voice = find_voice ();
205   if (voice)
206     set_music_context (voice);
207
208   /*
209     Wait for a Create_context event. If this isn't done, lyrics can be
210     delayed when voices are created implicitly.
211   */
212   Global_context *g = get_outlet ()->get_global_context ();
213   g->events_below ()->add_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
214
215   /*
216     We do not create a Lyrics context, because the user might
217     create one with a different name, and then we will not find that
218     one.
219   */
220 }
221
222 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, check_new_context)
223 void
224 Lyric_combine_music_iterator::check_new_context (SCM /*sev*/)
225 {
226   if (!ok ())
227     return;
228
229   // Search for a possible candidate voice to attach the lyrics to. If none
230   // is found, we'll try next time again.
231   Context *voice = find_voice ();
232   if (voice)
233     {
234       set_music_context (voice);
235     }
236 }
237
238 /*
239   Look for a suitable voice to align lyrics to.
240
241   Returns 0 if nothing should change; i.e., if we already listen to the
242   right voice, or if we don't yet listen to a voice but no appropriate
243   voice could be found.
244 */
245 Context *
246 Lyric_combine_music_iterator::find_voice ()
247 {
248   SCM voice_name = lyricsto_voice_name_;
249   SCM running = lyrics_context_
250                 ? lyrics_context_->get_property ("associatedVoice")
251                 : SCM_EOL;
252
253   if (scm_is_string (running))
254     voice_name = running;
255
256   if (scm_is_string (voice_name)
257       && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
258     {
259       Context *t = get_outlet ();
260       while (t && t->get_parent_context ())
261         t = t->get_parent_context ();
262
263       string name = ly_scm2string (voice_name);
264       return find_context_below (t, ly_symbol2scm ("Voice"), name);
265     }
266
267   return 0;
268 }
269
270 void
271 Lyric_combine_music_iterator::process (Moment /* when */)
272 {
273   /* see if associatedVoice has been changed */
274   Context *new_voice = find_voice ();
275   if (new_voice)
276     set_music_context (new_voice);
277
278   lyrics_found_ = true;
279   if (!music_context_)
280     return;
281
282   if (!music_context_->get_parent_context ())
283     {
284       /*
285         The melody has died.
286         We die too.
287       */
288       if (lyrics_context_)
289         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
290       lyric_iter_ = 0;
291       set_music_context (0);
292     }
293
294   if (music_context_
295       && (start_new_syllable ()
296           || (busy_moment_ >= pending_grace_moment_))
297       && lyric_iter_->ok ())
298     {
299       Moment now = music_context_->now_mom ();
300       if (now.grace_part_ && !to_boolean (lyrics_context_->get_property ("includeGraceNotes")))
301         {
302           pending_grace_moment_ = now;
303           pending_grace_moment_.grace_part_ = Rational (0);
304           return;
305         }
306       else
307         {
308           pending_grace_moment_.set_infinite (1);
309         }
310
311       Moment m = lyric_iter_->pending_moment ();
312       lyrics_context_->set_property (ly_symbol2scm ("associatedVoiceContext"),
313                                      music_context_->self_scm ());
314       lyric_iter_->process (m);
315
316       music_found_ = true;
317     }
318
319   new_voice = find_voice ();
320   if (new_voice)
321     set_music_context (new_voice);
322 }
323
324 void
325 Lyric_combine_music_iterator::do_quit ()
326 {
327   if (!music_found_)
328     {
329       SCM voice_name = get_music ()->get_property ("associated-context");
330
331       string name;
332       if (scm_is_string (voice_name))
333         name = ly_scm2string (voice_name);
334       /* Don't print a warning for empty lyrics (in which case we don't try
335          to find the proper voice, so it will not be found) */
336       if (lyrics_found_)
337         get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
338                                               name.c_str ()) + "\n");
339     }
340
341   if (lyric_iter_)
342     lyric_iter_->quit ();
343 }
344
345 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);