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