]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-combine-music-iterator.cc
Added new system for how translators listen to events.
[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   SCM mus = e->get_property ("music");
67   Music *m = unsmob_music (mus);
68   assert (m);
69
70   if (m->is_mus_type ("note-event") || m->is_mus_type ("cluster-note-event"))
71     busy_ = true;
72 }
73
74 void
75 Lyric_combine_music_iterator::set_music_context (Context *to)
76 {
77   if (music_context_)
78     {
79       music_context_->event_source()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("OldMusicEvent"));
80       lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
81     }
82   music_context_ = to;
83   if (to)
84     {
85       to->event_source()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("OldMusicEvent"));
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   lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
166
167   Context *voice = find_voice ();
168   if (voice)
169     set_music_context (voice);
170   else
171     {
172       /*
173         Wait for a Create_context event. If this isn't done, lyrics can be 
174         delayed when voices are created implicitly.
175       */
176       Global_context *g = lyrics_context_->get_global_context ();
177       g->events_below ()->add_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
178     }
179
180   /*
181     We do not create a Lyrics context, because the user might
182     create one with a different name, and then we will not find that
183     one.
184   */
185 }
186
187 IMPLEMENT_LISTENER (Lyric_combine_music_iterator, check_new_context)
188 void
189 Lyric_combine_music_iterator::check_new_context (SCM sev)
190 {
191   // TODO: Check first if type=Voice and if id matches
192   (void)sev;
193
194   Context *voice = find_voice ();
195   if (voice)
196     {
197       set_music_context (voice);
198
199       Global_context *g = voice->get_global_context ();
200       g->events_below ()->remove_listener (GET_LISTENER (check_new_context), ly_symbol2scm ("CreateContext"));
201     }
202 }
203
204 /*
205   Look for a suitable voice to align lyrics to.
206
207   Returns 0 if nothing should change; i.e., if we already listen to the
208   right voice, or if we don't yet listen to a voice but no appropriate
209   voice could be found.
210 */
211 Context *
212 Lyric_combine_music_iterator::find_voice ()
213 {
214   SCM voice_name = lyricsto_voice_name_;
215   SCM running = lyrics_context_
216     ? lyrics_context_->get_property ("associatedVoice")
217     : SCM_EOL;
218
219   if (scm_is_string (running))
220     voice_name = running;
221
222   if (scm_is_string (voice_name)
223       && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
224     {
225       Context *t = get_outlet ();
226       while (t && t->get_parent_context ())
227         t = t->get_parent_context ();
228
229       string name = ly_scm2string (voice_name);
230       return find_context_below (t, ly_symbol2scm ("Voice"), name);
231     }
232
233   return 0;
234 }
235
236 void
237 Lyric_combine_music_iterator::process (Moment)
238 {
239   /* see if associatedVoice has been changed */
240   Context *new_voice = find_voice ();
241   if (new_voice)
242     set_music_context (new_voice);
243
244   if (!music_context_)
245     return;
246
247   if (!music_context_->get_parent_context ())
248     {
249       /*
250         The melody has died.
251         We die too.
252       */
253       if (lyrics_context_)
254         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
255       lyric_iter_ = 0;
256       set_music_context (0);
257     }
258
259   if (music_context_
260       && (start_new_syllable () || pending_grace_lyric_)
261       && lyric_iter_->ok ())
262     {
263       if (music_context_->now_mom ().grace_part_)
264         {
265           pending_grace_lyric_ = true;
266           return;
267         }
268       else
269         pending_grace_lyric_ = false;
270       
271       Moment m = lyric_iter_->pending_moment ();
272       lyric_iter_->process (m);
273
274       music_found_ = true;
275     }
276 }
277
278 void
279 Lyric_combine_music_iterator::do_quit ()
280 {
281   if (!music_found_)
282     {
283       SCM voice_name = get_music ()->get_property ("associated-context");
284
285       string name;
286       if (scm_is_string (voice_name))
287         name = ly_scm2string (voice_name);
288
289       get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
290                                             name.c_str ()) + "\n");
291     }
292
293   if (lyric_iter_)
294     lyric_iter_->quit ();
295 }
296
297 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);