]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-combine-music-iterator.cc
* lily/chord-tremolo-*.cc: Reworked how \repeat "tremolo"
[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       scm_display (music_context_->now_mom().smobbed_copy(), scm_current_output_port ());
99
100   if (!lyrics_context_)
101     return false;
102
103   if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
104     {
105       bool m = melisma_busy (music_context_);
106       if (m)
107         return false;
108     }
109
110   return true;
111 }
112
113 Moment
114 Lyric_combine_music_iterator::pending_moment () const
115 {
116   Moment m;
117
118   m.set_infinite (1);
119
120   return m;
121 }
122
123 bool
124 Lyric_combine_music_iterator::run_always () const
125 {
126   return true;
127 }
128
129 bool
130 Lyric_combine_music_iterator::ok () const
131 {
132   return lyric_iter_ && lyric_iter_->ok ();
133 }
134
135 void
136 Lyric_combine_music_iterator::derived_mark ()const
137 {
138   if (lyric_iter_)
139     scm_gc_mark (lyric_iter_->self_scm ());
140   if (lyrics_context_)
141     scm_gc_mark (lyrics_context_->self_scm ());
142   if (music_context_)
143     scm_gc_mark (music_context_->self_scm ());
144 }
145
146 void
147 Lyric_combine_music_iterator::derived_substitute (Context *f, Context *t)
148 {
149   if (lyric_iter_)
150     lyric_iter_->substitute_outlet (f, t);
151   if (lyrics_context_ && lyrics_context_ == f)
152     lyrics_context_ = t;
153   if (music_context_ && music_context_ == f)
154     set_music_context (t);
155 }
156
157 void
158 Lyric_combine_music_iterator::construct_children ()
159 {
160   Music *m = unsmob_music (get_music ()->get_property ("element"));
161   lyric_iter_ = unsmob_iterator (get_iterator (m));
162   if (!lyric_iter_)
163     return;
164   lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
165                                         ly_symbol2scm ("Lyrics"), "");
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 = lyrics_context_->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 Context *
210 Lyric_combine_music_iterator::find_voice ()
211 {
212   SCM voice_name = lyricsto_voice_name_;
213   SCM running = lyrics_context_
214     ? lyrics_context_->get_property ("associatedVoice")
215     : SCM_EOL;
216
217   if (scm_is_string (running))
218     voice_name = running;
219
220   if (scm_is_string (voice_name)
221       && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
222     {
223       Context *t = get_outlet ();
224       while (t && t->get_parent_context ())
225         t = t->get_parent_context ();
226
227       string name = ly_scm2string (voice_name);
228       return find_context_below (t, ly_symbol2scm ("Voice"), name);
229     }
230
231   return 0;
232 }
233
234 void
235 Lyric_combine_music_iterator::process (Moment)
236 {
237   find_voice ();
238   if (!music_context_)
239     return;
240
241   if (!music_context_->get_parent_context ())
242     {
243       /*
244         The melody has died.
245         We die too.
246       */
247       if (lyrics_context_)
248         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
249       lyric_iter_ = 0;
250       set_music_context (0);
251     }
252
253   if (music_context_
254       && (start_new_syllable () || pending_grace_lyric_)
255       && lyric_iter_->ok ())
256     {
257       if (music_context_->now_mom ().grace_part_)
258         {
259           pending_grace_lyric_ = true;
260           return;
261         }
262       else
263         pending_grace_lyric_ = false;
264       
265       Moment m = lyric_iter_->pending_moment ();
266       lyric_iter_->process (m);
267
268       music_found_ = true;
269     }
270 }
271
272 void
273 Lyric_combine_music_iterator::do_quit ()
274 {
275   if (!music_found_)
276     {
277       SCM voice_name = get_music ()->get_property ("associated-context");
278
279       string name;
280       if (scm_is_string (voice_name))
281         name = ly_scm2string (voice_name);
282
283       get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
284                                             name.c_str ()) + "\n");
285     }
286
287   if (lyric_iter_)
288     lyric_iter_->quit ();
289 }
290
291 IMPLEMENT_CTOR_CALLBACK (Lyric_combine_music_iterator);