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