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