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