]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-combine-music-iterator.cc
Fix some bugs in the dynamic engraver and PostScript backend
[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 ("MusicEvent"));
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 ("MusicEvent"));
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 Context *
208 Lyric_combine_music_iterator::find_voice ()
209 {
210   SCM voice_name = lyricsto_voice_name_;
211   SCM running = lyrics_context_
212     ? lyrics_context_->get_property ("associatedVoice")
213     : SCM_EOL;
214
215   if (scm_is_string (running))
216     voice_name = running;
217
218   if (scm_is_string (voice_name)
219       && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
220     {
221       Context *t = get_outlet ();
222       while (t && t->get_parent_context ())
223         t = t->get_parent_context ();
224
225       string name = ly_scm2string (voice_name);
226       return find_context_below (t, ly_symbol2scm ("Voice"), name);
227     }
228
229   return 0;
230 }
231
232 void
233 Lyric_combine_music_iterator::process (Moment)
234 {
235   find_voice ();
236   if (!music_context_)
237     return;
238
239   if (!music_context_->get_parent_context ())
240     {
241       /*
242         The melody has died.
243         We die too.
244       */
245       if (lyrics_context_)
246         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
247       lyric_iter_ = 0;
248       set_music_context (0);
249     }
250
251   if (music_context_
252       && (start_new_syllable () || pending_grace_lyric_)
253       && lyric_iter_->ok ())
254     {
255       if (music_context_->now_mom ().grace_part_)
256         {
257           pending_grace_lyric_ = true;
258           return;
259         }
260       else
261         pending_grace_lyric_ = false;
262       
263       Moment m = lyric_iter_->pending_moment ();
264       lyric_iter_->process (m);
265
266       music_found_ = true;
267     }
268 }
269
270 void
271 Lyric_combine_music_iterator::do_quit ()
272 {
273   if (!music_found_)
274     {
275       SCM voice_name = get_music ()->get_property ("associated-context");
276
277       string name;
278       if (scm_is_string (voice_name))
279         name = ly_scm2string (voice_name);
280
281       get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
282                                             name.c_str ()) + "\n");
283     }
284
285   if (lyric_iter_)
286     lyric_iter_->quit ();
287 }
288
289 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);