]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-lyric-combine-music-iterator.cc
* lily/include/event.hh: remove file.
[lilypond.git] / lily / new-lyric-combine-music-iterator.cc
1 /*
2   new-lyric-combine-iterator.cc -- implement New_lyric_combine_music_iterator
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "context.hh"
10 #include "music.hh"
11 #include "grob.hh"
12 #include "music-iterator.hh"
13 #include "input.hh"
14
15 class New_lyric_combine_music_iterator : public Music_iterator
16 {
17 public:
18   New_lyric_combine_music_iterator ();
19   New_lyric_combine_music_iterator (New_lyric_combine_music_iterator const &src);
20   DECLARE_SCHEME_CALLBACK (constructor, ());
21 protected:
22   virtual void construct_children ();
23   virtual Moment pending_moment () const;
24   virtual void do_quit ();
25   virtual void process (Moment);
26   virtual Music_iterator *try_music_in_children (Music *) const;
27   virtual bool run_always ()const;
28   virtual bool ok () const;
29   virtual void derived_mark () const;
30   virtual void derived_substitute (Context *, Context *);
31 private:
32   bool start_new_syllable ();
33   void find_voice ();
34
35   bool music_found_;
36   bool made_association_;
37   Context *lyrics_context_;
38   Context *music_context_;
39   SCM lyricsto_voice_name_;
40
41   Music_iterator *lyric_iter_;
42 };
43
44 /*
45   Ugh, why static?
46 */
47 static Music *busy_ev;
48 static Music *start_ev;
49 static Music *melisma_playing_ev;
50
51 New_lyric_combine_music_iterator::New_lyric_combine_music_iterator ()
52 {
53   music_found_ = false;
54   made_association_ = false;
55   lyric_iter_ = 0;
56   music_context_ = 0;
57   lyrics_context_ = 0;
58
59   /*
60     Ugh. out of place here.
61   */
62   if (!busy_ev)
63     {
64       busy_ev
65         = make_music_by_name (ly_symbol2scm ("BusyPlayingEvent"));
66       start_ev
67         = make_music_by_name (ly_symbol2scm ("StartPlayingEvent"));
68       melisma_playing_ev
69         = make_music_by_name (ly_symbol2scm ("MelismaPlayingEvent"));
70     }
71 }
72
73 bool
74 New_lyric_combine_music_iterator::start_new_syllable ()
75 {
76   bool b = music_context_->try_music (busy_ev);
77
78   if (!b)
79     return false;
80
81   if (!lyrics_context_)
82     return false;
83
84   if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
85     {
86       bool m = music_context_->try_music (melisma_playing_ev);
87       if (m)
88         return false;
89     }
90
91   return true;
92 }
93
94 Moment
95 New_lyric_combine_music_iterator::pending_moment () const
96 {
97   Moment m;
98
99   m.set_infinite (1);
100
101   return m;
102 }
103
104 bool
105 New_lyric_combine_music_iterator::run_always () const
106 {
107   return true;
108 }
109
110 bool
111 New_lyric_combine_music_iterator::ok () const
112 {
113   return lyric_iter_ && lyric_iter_->ok ();
114 }
115
116 void
117 New_lyric_combine_music_iterator::derived_mark ()const
118 {
119   if (lyric_iter_)
120     scm_gc_mark (lyric_iter_->self_scm ());
121   if (lyrics_context_)
122     scm_gc_mark (lyrics_context_->self_scm ());
123   if (music_context_)
124     scm_gc_mark (music_context_->self_scm ());
125 }
126
127 void
128 New_lyric_combine_music_iterator::derived_substitute (Context *f, Context *t)
129 {
130   if (lyric_iter_)
131     lyric_iter_->substitute_outlet (f, t);
132   if (lyrics_context_ && lyrics_context_ == f)
133     lyrics_context_ = t;
134   if (music_context_ && music_context_ == f)
135     music_context_ = t;
136 }
137
138 void
139 New_lyric_combine_music_iterator::construct_children ()
140 {
141   Music *m = unsmob_music (get_music ()->get_property ("element"));
142   lyric_iter_ = unsmob_iterator (get_iterator (m));
143
144   lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
145
146   find_voice ();
147
148   if (lyric_iter_)
149     lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
150                                           ly_symbol2scm ("Lyrics"), "");
151
152   /*
153     We do not create a Lyrics context, because the user might
154     create one with a different name, and then we will not find that
155     one.
156   */
157 }
158
159 void
160 New_lyric_combine_music_iterator::find_voice ()
161 {
162   SCM voice_name = lyricsto_voice_name_;
163   SCM running = lyrics_context_ ? lyrics_context_->get_property ("associatedVoice") : SCM_EOL;
164
165   if (scm_is_string (running))
166     voice_name = running;
167
168   if (scm_is_string (voice_name)
169       && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))
170     {
171       /*
172         (spaghettini).
173
174         Need to set associatedVoiceContext again
175       */
176       if (music_context_)
177         made_association_ = false;
178
179       Context *t = get_outlet ();
180       while (t && t->get_parent_context ())
181         t = t->get_parent_context ();
182
183       String name = ly_scm2string (voice_name);
184       Context *voice = find_context_below (t, ly_symbol2scm ("Voice"), name);
185
186       if (voice)
187         music_context_ = voice;
188     }
189
190   if (lyrics_context_ && music_context_)
191     {
192       if (!made_association_)
193         {
194           made_association_ = true;
195           lyrics_context_->set_property ("associatedVoiceContext",
196                                          music_context_->self_scm ());
197         }
198     }
199 }
200
201 void
202 New_lyric_combine_music_iterator::process (Moment)
203 {
204   find_voice ();
205   if (!music_context_)
206     return;
207
208   if (!music_context_->get_parent_context ())
209     {
210       /*
211         The melody has died.
212         We die too.
213       */
214       if (lyrics_context_)
215         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
216       lyric_iter_ = 0;
217       music_context_ = 0;
218     }
219
220   if (music_context_
221       && start_new_syllable () && lyric_iter_->ok ())
222     {
223       Moment m = lyric_iter_->pending_moment ();
224       lyric_iter_->process (m);
225
226       music_found_ = true;
227     }
228 }
229
230 void
231 New_lyric_combine_music_iterator::do_quit ()
232 {
233   if (!music_found_)
234     {
235       SCM voice_name = get_music ()->get_property ("associated-context");
236
237       String name;
238       if (scm_is_string (voice_name))
239         name = ly_scm2string (voice_name);
240
241       get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
242                                             name.to_str0 ()) + "\n");
243     }
244
245   if (lyric_iter_)
246     lyric_iter_->quit ();
247 }
248
249 Music_iterator *
250 New_lyric_combine_music_iterator::try_music_in_children (Music *m) const
251 {
252   return lyric_iter_->try_music (m);
253 }
254
255 IMPLEMENT_CTOR_CALLBACK (New_lyric_combine_music_iterator);