]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-combine-music-iterator.cc
robustness: survive \lyricsto argument without Lyrics context. Fixes #183
[lilypond.git] / lily / lyric-combine-music-iterator.cc
1 /*
2   new-lyric-combine-iterator.cc -- implement Lyric_combine_music_iterator
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "dispatcher.hh"
10 #include "global-context.hh"
11 #include "grob.hh"
12 #include "input.hh"
13 #include "international.hh"
14 #include "listener.hh"
15 #include "music-iterator.hh"
16 #include "music.hh"
17
18 class Lyric_combine_music_iterator : public Music_iterator
19 {
20 public:
21   Lyric_combine_music_iterator ();
22   Lyric_combine_music_iterator (Lyric_combine_music_iterator const &src);
23   DECLARE_SCHEME_CALLBACK (constructor, ());
24 protected:
25   virtual void construct_children ();
26   virtual Moment pending_moment () const;
27   virtual void do_quit ();
28   virtual void process (Moment);
29   virtual bool run_always ()const;
30   virtual bool ok () const;
31   virtual void derived_mark () const;
32   virtual void derived_substitute (Context *, Context *);
33   void set_music_context (Context *to);
34 private:
35   bool start_new_syllable ();
36   Context *find_voice ();
37   DECLARE_LISTENER (set_busy);
38   DECLARE_LISTENER (check_new_context);
39
40   bool pending_grace_lyric_;
41   bool music_found_;
42   Context *lyrics_context_;
43   Context *music_context_;
44   SCM lyricsto_voice_name_;
45
46   bool busy_;
47   Music_iterator *lyric_iter_;
48 };
49
50 Lyric_combine_music_iterator::Lyric_combine_music_iterator ()
51 {
52   music_found_ = false;
53   pending_grace_lyric_ = false;
54   lyric_iter_ = 0;
55   music_context_ = 0;
56   lyrics_context_ = 0;
57   busy_ = false;
58 }
59
60 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, set_busy)
61 void
62 Lyric_combine_music_iterator::set_busy (SCM se)
63 {
64   Stream_event *e = unsmob_stream_event (se);
65
66   if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
67     busy_ = true;
68 }
69
70 void
71 Lyric_combine_music_iterator::set_music_context (Context *to)
72 {
73   if (music_context_)
74     {
75       music_context_->event_source()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
76       lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
77     }
78   music_context_ = to;
79   if (to)
80     {
81       to->event_source()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
82       if (lyrics_context_)
83         lyrics_context_->set_property ("associatedVoiceContext", to->self_scm ());
84     }
85 }
86
87 bool
88 Lyric_combine_music_iterator::start_new_syllable ()
89 {
90   if (!busy_)
91     return false;
92
93   busy_ = false;
94
95   if (!lyrics_context_)
96     return false;
97
98   if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
99     {
100       bool m = melisma_busy (music_context_);
101       if (m)
102         return false;
103     }
104
105   return true;
106 }
107
108 Moment
109 Lyric_combine_music_iterator::pending_moment () const
110 {
111   Moment m;
112
113   m.set_infinite (1);
114
115   return m;
116 }
117
118 bool
119 Lyric_combine_music_iterator::run_always () const
120 {
121   return true;
122 }
123
124 bool
125 Lyric_combine_music_iterator::ok () const
126 {
127   return lyric_iter_ && lyric_iter_->ok ();
128 }
129
130 void
131 Lyric_combine_music_iterator::derived_mark ()const
132 {
133   if (lyric_iter_)
134     scm_gc_mark (lyric_iter_->self_scm ());
135   if (lyrics_context_)
136     scm_gc_mark (lyrics_context_->self_scm ());
137   if (music_context_)
138     scm_gc_mark (music_context_->self_scm ());
139 }
140
141 void
142 Lyric_combine_music_iterator::derived_substitute (Context *f, Context *t)
143 {
144   if (lyric_iter_)
145     lyric_iter_->substitute_outlet (f, t);
146   if (lyrics_context_ && lyrics_context_ == f)
147     lyrics_context_ = t;
148   if (music_context_ && music_context_ == f)
149     set_music_context (t);
150 }
151
152 void
153 Lyric_combine_music_iterator::construct_children ()
154 {
155   Music *m = unsmob_music (get_music ()->get_property ("element"));
156   lyric_iter_ = unsmob_iterator (get_iterator (m));
157   if (!lyric_iter_)
158     return;
159   lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
160                                         ly_symbol2scm ("Lyrics"), "");
161
162   if (!lyrics_context_)
163     {
164       m->origin ()->warning ("argument of \\lyricsto should contain Lyrics context");
165     }
166   
167   lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
168
169   Context *voice = find_voice ();
170   if (voice)
171     set_music_context (voice);
172   else
173     {
174       /*
175         Wait for a Create_context event. If this isn't done, lyrics can be 
176         delayed when voices are created implicitly.
177       */
178       Global_context *g = get_outlet ()->get_global_context ();
179       g->events_below ()->add_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
180     }
181
182   /*
183     We do not create a Lyrics context, because the user might
184     create one with a different name, and then we will not find that
185     one.
186   */
187 }
188
189 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, check_new_context)
190 void
191 Lyric_combine_music_iterator::check_new_context (SCM sev)
192 {
193   // TODO: Check first if type=Voice and if id matches
194   (void)sev;
195
196   Context *voice = find_voice ();
197   if (voice)
198     {
199       set_music_context (voice);
200
201       Global_context *g = voice->get_global_context ();
202       g->events_below ()->remove_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
203     }
204 }
205
206 /*
207   Look for a suitable voice to align lyrics to.
208
209   Returns 0 if nothing should change; i.e., if we already listen to the
210   right voice, or if we don't yet listen to a voice but no appropriate
211   voice could be found.
212 */
213 Context *
214 Lyric_combine_music_iterator::find_voice ()
215 {
216   SCM voice_name = lyricsto_voice_name_;
217   SCM running = lyrics_context_
218     ? lyrics_context_->get_property ("associatedVoice")
219     : SCM_EOL;
220
221   if (scm_is_string (running))
222     voice_name = running;
223
224   if (scm_is_string (voice_name)
225       && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
226     {
227       Context *t = get_outlet ();
228       while (t && t->get_parent_context ())
229         t = t->get_parent_context ();
230
231       string name = ly_scm2string (voice_name);
232       return find_context_below (t, ly_symbol2scm ("Voice"), name);
233     }
234
235   return 0;
236 }
237
238 void
239 Lyric_combine_music_iterator::process (Moment)
240 {
241   /* see if associatedVoice has been changed */
242   Context *new_voice = find_voice ();
243   if (new_voice)
244     set_music_context (new_voice);
245
246   if (!music_context_)
247     return;
248
249   if (!music_context_->get_parent_context ())
250     {
251       /*
252         The melody has died.
253         We die too.
254       */
255       if (lyrics_context_)
256         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
257       lyric_iter_ = 0;
258       set_music_context (0);
259     }
260
261   if (music_context_
262       && (start_new_syllable () || pending_grace_lyric_)
263       && lyric_iter_->ok ())
264     {
265       if (music_context_->now_mom ().grace_part_)
266         {
267           pending_grace_lyric_ = true;
268           return;
269         }
270       else
271         pending_grace_lyric_ = false;
272       
273       Moment m = lyric_iter_->pending_moment ();
274       lyric_iter_->process (m);
275
276       music_found_ = true;
277     }
278 }
279
280 void
281 Lyric_combine_music_iterator::do_quit ()
282 {
283   if (!music_found_)
284     {
285       SCM voice_name = get_music ()->get_property ("associated-context");
286
287       string name;
288       if (scm_is_string (voice_name))
289         name = ly_scm2string (voice_name);
290
291       get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
292                                             name.c_str ()) + "\n");
293     }
294
295   if (lyric_iter_)
296     lyric_iter_->quit ();
297 }
298
299 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);