]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-lyric-combine-music-iterator.cc
b5d0729108c5381cb2cef56b4bda519aa1546380
[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 "translator-group.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   VIRTUAL_COPY_CONS (Music_iterator);
21   New_lyric_combine_music_iterator ();
22   New_lyric_combine_music_iterator (New_lyric_combine_music_iterator const&src);
23   DECLARE_SCHEME_CALLBACK(constructor, ());
24 protected:
25   virtual void construct_children ();
26   virtual Moment pending_moment () const;
27   virtual void do_quit(); 
28   virtual void process (Moment);
29   virtual Music_iterator *try_music_in_children (Music *) const;
30   virtual bool run_always ()const;
31   virtual bool ok () const;
32   virtual void derived_mark () const;
33   virtual void derived_substitute (Translator_group*,Translator_group*);
34 private:
35   bool start_new_syllable () ;
36   void find_thread ();
37   
38   Translator_group * lyrics_context_;
39   Translator_group* music_context_;
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   lyric_iter_ =0;
53   music_context_ =0;
54   lyrics_context_ = 0;
55   if (!busy_ev)
56     {
57       busy_ev
58         = make_music_by_name (ly_symbol2scm ("BusyPlayingEvent"));
59       start_ev
60         = make_music_by_name (ly_symbol2scm ("StartPlayingEvent"));
61       melisma_playing_ev
62         = make_music_by_name (ly_symbol2scm ("MelismaPlayingEvent"));
63     }
64 }
65
66 bool
67 New_lyric_combine_music_iterator::start_new_syllable ()
68 {
69   bool b = music_context_->try_music (busy_ev);
70   
71   if (!b)
72     return false;
73
74   if (!lyrics_context_)
75     construct_children ();
76   
77   if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
78     {
79       bool m = music_context_->try_music (melisma_playing_ev);
80       if (m)
81         return false;
82     }
83   
84   return true;
85 }
86
87 Moment
88 New_lyric_combine_music_iterator::pending_moment () const
89 {
90   Moment m;
91
92   m.set_infinite (1);
93     
94   return m;
95 }
96
97 bool
98 New_lyric_combine_music_iterator::run_always () const
99 {
100   return true;
101 }
102
103 bool
104 New_lyric_combine_music_iterator::ok () const
105 {
106   return lyric_iter_ && lyric_iter_->ok ();
107 }
108
109 void
110 New_lyric_combine_music_iterator::derived_mark()const
111 {
112   if (lyric_iter_)
113     scm_gc_mark (lyric_iter_->self_scm ());
114   if (lyrics_context_)
115     scm_gc_mark (lyrics_context_->self_scm ());
116   if (music_context_)
117     scm_gc_mark (music_context_->self_scm ());
118 }
119
120 void
121 New_lyric_combine_music_iterator::derived_substitute (Translator_group*f, Translator_group*t)
122 {
123   if (lyric_iter_)
124     lyric_iter_->substitute_outlet (f,t);
125   if (lyrics_context_ && lyrics_context_==f)
126     lyrics_context_ = t;
127   if (music_context_ && music_context_ == f)
128     music_context_ = t; 
129 }
130
131 /*
132   ID == "" means accept any ID.
133  */
134 Translator_group *
135 find_context_below (Translator_group * where,
136                     String type, String id)
137 {
138   if (where->context_name () == type)
139     {
140       if (id == "" || where->id_string_ == id)
141         return where;
142     }
143   
144   Translator_group * found = 0;
145   for (SCM s = where->trans_group_list_;
146        !found && gh_pair_p (s); s = gh_cdr (s))
147     {
148       Translator_group * tr = dynamic_cast<Translator_group*> (unsmob_translator (gh_car (s)));
149
150       
151       found = find_context_below (tr, type, id);
152     }
153
154   return found; 
155 }
156
157
158
159 void
160 New_lyric_combine_music_iterator::construct_children ()
161 {
162   Music *m = unsmob_music (get_music ()->get_mus_property ("element"));
163   lyric_iter_ = unsmob_iterator (get_iterator (m));
164
165   find_thread ();
166   
167   if (lyric_iter_)
168     lyrics_context_ = find_context_below (lyric_iter_->report_to (),
169                                           "LyricsVoice", "");
170   
171   if (music_context_ && !lyrics_context_)
172     lyrics_context_ = music_context_
173       ->find_create_translator (ly_symbol2scm ("LyricsVoice"), "", SCM_EOL);
174 }
175
176 void
177 New_lyric_combine_music_iterator::find_thread ()
178 {
179   if (!music_context_)
180     {
181       SCM voice_name = get_music ()->get_mus_property ("associated-context");
182   
183       if (gh_string_p (voice_name))
184         {
185           Translator_group *t = report_to ();
186           while (t && t->daddy_trans_)
187             t = t->daddy_trans_;
188
189           String name = ly_scm2string (voice_name);
190           Translator_group *voice = find_context_below (t, "Voice", name);
191           Translator_group *thread = 0;
192           if (voice)
193             thread = find_context_below (voice, "Thread", "");
194           else
195             get_music ()->origin ()->warning (_f ("Cannot find Voice: %s\n",
196                                                   name.to_str0 ())); 
197
198           if (thread)
199             music_context_ = thread;
200             
201           if (lyrics_context_ && voice)
202             lyrics_context_->set_property ("associatedVoiceContext",
203                                            voice->self_scm ());
204         }
205     }
206 }
207
208 void
209 New_lyric_combine_music_iterator::process (Moment )
210 {
211   find_thread ();
212   if (!music_context_)
213     return ;
214   
215   if (!music_context_->daddy_trans_)
216     {
217       music_context_ = 0;
218       if (lyrics_context_)
219         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
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 }
229
230 void
231 New_lyric_combine_music_iterator::do_quit ()
232 {
233   if (lyric_iter_)
234     lyric_iter_->quit();
235 }
236
237 New_lyric_combine_music_iterator::New_lyric_combine_music_iterator (New_lyric_combine_music_iterator const & src)
238     : Music_iterator (src)
239 {
240   lyric_iter_ = 0;
241
242   if (src.lyric_iter_)
243     lyric_iter_ =  src.lyric_iter_->clone ();
244
245   if (lyric_iter_)
246     scm_gc_unprotect_object (lyric_iter_->self_scm());
247
248   music_context_ = src.music_context_;
249   lyric_iter_ = src.lyric_iter_;
250     
251   
252   assert (false);               // shouldn't copy, really.
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);