]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-combine-music-iterator.cc
* lily/translator.cc, lily/context.cc:, lily/translator-group.cc:
[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 "context.hh"
10 #include "dispatcher.hh"
11 #include "global-context.hh"
12 #include "grob.hh"
13 #include "input.hh"
14 #include "international.hh"
15 #include "listener.hh"
16 #include "music-iterator.hh"
17 #include "music.hh"
18
19 class Lyric_combine_music_iterator : public Music_iterator
20 {
21 public:
22   Lyric_combine_music_iterator ();
23   Lyric_combine_music_iterator (Lyric_combine_music_iterator const &src);
24   DECLARE_SCHEME_CALLBACK (constructor, ());
25 protected:
26   virtual void construct_children ();
27   virtual Moment pending_moment () const;
28   virtual void do_quit ();
29   virtual void process (Moment);
30   virtual bool run_always ()const;
31   virtual bool ok () const;
32   virtual void derived_mark () const;
33   virtual void derived_substitute (Context *, Context *);
34   void set_music_context (Context *to);
35 private:
36   bool start_new_syllable ();
37   Context *find_voice ();
38   DECLARE_LISTENER (set_busy);
39   DECLARE_LISTENER (check_new_context);
40
41   bool pending_grace_lyric_;
42   bool music_found_;
43   Context *lyrics_context_;
44   Context *music_context_;
45   SCM lyricsto_voice_name_;
46
47   bool busy_;
48   Music_iterator *lyric_iter_;
49 };
50
51 Lyric_combine_music_iterator::Lyric_combine_music_iterator ()
52 {
53   music_found_ = false;
54   pending_grace_lyric_ = false;
55   lyric_iter_ = 0;
56   music_context_ = 0;
57   lyrics_context_ = 0;
58   busy_ = false;
59 }
60
61 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, set_busy)
62 void
63 Lyric_combine_music_iterator::set_busy (SCM se)
64 {
65   Stream_event *e = unsmob_stream_event (se);
66
67   if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
68     busy_ = true;
69 }
70
71 void
72 Lyric_combine_music_iterator::set_music_context (Context *to)
73 {
74   if (music_context_)
75     {
76       music_context_->event_source()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
77       lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
78     }
79   music_context_ = to;
80   if (to)
81     {
82       to->event_source()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
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   lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
163
164   Context *voice = find_voice ();
165   if (voice)
166     set_music_context (voice);
167   else
168     {
169       /*
170         Wait for a Create_context event. If this isn't done, lyrics can be 
171         delayed when voices are created implicitly.
172       */
173       Global_context *g = lyrics_context_->get_global_context ();
174       g->events_below ()->add_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
175     }
176
177   /*
178     We do not create a Lyrics context, because the user might
179     create one with a different name, and then we will not find that
180     one.
181   */
182 }
183
184 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, check_new_context)
185 void
186 Lyric_combine_music_iterator::check_new_context (SCM sev)
187 {
188   // TODO: Check first if type=Voice and if id matches
189   (void)sev;
190
191   Context *voice = find_voice ();
192   if (voice)
193     {
194       set_music_context (voice);
195
196       Global_context *g = voice->get_global_context ();
197       g->events_below ()->remove_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
198     }
199 }
200
201 /*
202   Look for a suitable voice to align lyrics to.
203
204   Returns 0 if nothing should change; i.e., if we already listen to the
205   right voice, or if we don't yet listen to a voice but no appropriate
206   voice could be found.
207 */
208 Context *
209 Lyric_combine_music_iterator::find_voice ()
210 {
211   SCM voice_name = lyricsto_voice_name_;
212   SCM running = lyrics_context_
213     ? lyrics_context_->get_property ("associatedVoice")
214     : SCM_EOL;
215
216   if (scm_is_string (running))
217     voice_name = running;
218
219   if (scm_is_string (voice_name)
220       && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
221     {
222       Context *t = get_outlet ();
223       while (t && t->get_parent_context ())
224         t = t->get_parent_context ();
225
226       string name = ly_scm2string (voice_name);
227       return find_context_below (t, ly_symbol2scm ("Voice"), name);
228     }
229
230   return 0;
231 }
232
233 void
234 Lyric_combine_music_iterator::process (Moment)
235 {
236   /* see if associatedVoice has been changed */
237   Context *new_voice = find_voice ();
238   if (new_voice)
239     set_music_context (new_voice);
240
241   if (!music_context_)
242     return;
243
244   if (!music_context_->get_parent_context ())
245     {
246       /*
247         The melody has died.
248         We die too.
249       */
250       if (lyrics_context_)
251         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
252       lyric_iter_ = 0;
253       set_music_context (0);
254     }
255
256   if (music_context_
257       && (start_new_syllable () || pending_grace_lyric_)
258       && lyric_iter_->ok ())
259     {
260       if (music_context_->now_mom ().grace_part_)
261         {
262           pending_grace_lyric_ = true;
263           return;
264         }
265       else
266         pending_grace_lyric_ = false;
267       
268       Moment m = lyric_iter_->pending_moment ();
269       lyric_iter_->process (m);
270
271       music_found_ = true;
272     }
273 }
274
275 void
276 Lyric_combine_music_iterator::do_quit ()
277 {
278   if (!music_found_)
279     {
280       SCM voice_name = get_music ()->get_property ("associated-context");
281
282       string name;
283       if (scm_is_string (voice_name))
284         name = ly_scm2string (voice_name);
285
286       get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
287                                             name.c_str ()) + "\n");
288     }
289
290   if (lyric_iter_)
291     lyric_iter_->quit ();
292 }
293
294 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);