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