]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-lyric-combine-music-iterator.cc
* configure.in: Test for and accept lmodern if EC fonts not found.
[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 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
139 void
140 New_lyric_combine_music_iterator::construct_children ()
141 {
142   Music *m = unsmob_music (get_music ()->get_property ("element"));
143   lyric_iter_ = unsmob_iterator (get_iterator (m));
144
145   lyricsto_voice_name_ = get_music ()->get_property ("associated-context");
146
147   
148   find_voice ();
149   
150   if (lyric_iter_)
151     lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
152                                           ly_symbol2scm ("Lyrics"), "");
153
154   /*
155     We do not create a Lyrics context, because the user might
156     create one with a different name, and then we will not find that
157     one.
158    */
159 }
160
161 void
162 New_lyric_combine_music_iterator::find_voice ()
163 {
164   SCM voice_name = lyricsto_voice_name_;
165   SCM running = lyrics_context_ ? lyrics_context_->get_property ("associatedVoice") : SCM_EOL;
166
167   if (scm_is_string (running))
168     voice_name = running;
169
170   if (scm_is_string (voice_name)
171       && (!music_context_ || ly_scm2string (voice_name) != music_context_->id_string ()))    
172     {
173       /*
174         (spaghettini).
175         
176         Need to set associatedVoiceContext again
177        */
178       if (music_context_)
179         made_association_ = false;
180       
181       Context *t = get_outlet ();
182       while (t && t->get_parent_context ())
183         t = t->get_parent_context ();
184
185       String name = ly_scm2string (voice_name);
186       Context *voice = find_context_below (t, ly_symbol2scm ("Voice"), name);
187
188       
189       if (voice)
190         music_context_ = voice;
191     }
192
193   if (lyrics_context_ && music_context_)
194     {
195       if (!made_association_)
196         {
197           made_association_ = true; 
198           lyrics_context_->set_property ("associatedVoiceContext",
199                                          music_context_->self_scm ());
200         }
201     }
202 }
203
204 void
205 New_lyric_combine_music_iterator::process (Moment )
206 {
207   find_voice ();
208   if (!music_context_)
209     return ;
210   
211   if (!music_context_->get_parent_context ())
212     {
213       /*
214         The melody has died.
215         We die too.
216        */
217       if (lyrics_context_)
218         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
219       lyric_iter_ = 0;
220       music_context_ = 0;
221     }
222   
223   if (music_context_
224       && start_new_syllable () && lyric_iter_->ok ())
225     {
226       Moment m= lyric_iter_->pending_moment ();
227       lyric_iter_->process (m);
228
229       music_found_ = true; 
230     }
231 }
232
233 void
234 New_lyric_combine_music_iterator::do_quit ()
235 {
236   if (!music_found_)
237     {
238       SCM voice_name = get_music ()->get_property ("associated-context");
239
240       String name;
241       if (scm_is_string (voice_name))
242         name = ly_scm2string (voice_name);
243
244       get_music ()->origin ()->warning (_f ("cannot find Voice `%s'",
245                                             name.to_str0 ()) + "\n");
246     }
247
248   if (lyric_iter_)
249     lyric_iter_->quit ();
250 }
251
252
253
254 Music_iterator*
255 New_lyric_combine_music_iterator::try_music_in_children (Music *m) const
256 {
257   return lyric_iter_->try_music (m);
258 }
259
260
261 IMPLEMENT_CTOR_CALLBACK (New_lyric_combine_music_iterator);