]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-lyric-combine-music-iterator.cc
* input/regression/new-part-combine-solo.ly: more cases.
[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     return false;
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->is_alias (ly_symbol2scm (type.to_str0 ())))
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       found = find_context_below (tr, type, id);
151     }
152
153   return found; 
154 }
155
156
157
158 void
159 New_lyric_combine_music_iterator::construct_children ()
160 {
161   Music *m = unsmob_music (get_music ()->get_mus_property ("element"));
162   lyric_iter_ = unsmob_iterator (get_iterator (m));
163
164   find_thread ();
165   
166   if (lyric_iter_)
167     lyrics_context_ = find_context_below (lyric_iter_->report_to (),
168                                           "LyricsVoice", "");
169
170   /*
171     We do not create a LyricsVoice context, because the user might
172     create one with a different name, and then we will not find that
173     one.
174    */
175 }
176
177 void
178 New_lyric_combine_music_iterator::find_thread ()
179 {
180   if (!music_context_)
181     {
182       SCM voice_name = get_music ()->get_mus_property ("associated-context");
183   
184       if (gh_string_p (voice_name))
185         {
186           Translator_group *t = report_to ();
187           while (t && t->daddy_trans_)
188             t = t->daddy_trans_;
189
190           String name = ly_scm2string (voice_name);
191           Translator_group *voice = find_context_below (t, "Voice", name);
192           Translator_group *thread = 0;
193           if (voice)
194             thread = find_context_below (voice, "Thread", "");
195           else
196             get_music ()->origin ()->warning (_f ("Cannot find Voice: %s\n",
197                                                   name.to_str0 ())); 
198
199           if (thread)
200             music_context_ = thread;
201             
202           if (lyrics_context_ && voice)
203             lyrics_context_->set_property ("associatedVoiceContext",
204                                            voice->self_scm ());
205         }
206     }
207 }
208
209 void
210 New_lyric_combine_music_iterator::process (Moment )
211 {
212   find_thread ();
213   if (!music_context_)
214     return ;
215   
216   if (!music_context_->daddy_trans_)
217     {
218       music_context_ = 0;
219       if (lyrics_context_)
220         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
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 }
230
231 void
232 New_lyric_combine_music_iterator::do_quit ()
233 {
234   if (lyric_iter_)
235     lyric_iter_->quit();
236 }
237
238 New_lyric_combine_music_iterator::New_lyric_combine_music_iterator (New_lyric_combine_music_iterator const & src)
239     : Music_iterator (src)
240 {
241   lyric_iter_ = 0;
242
243   if (src.lyric_iter_)
244     lyric_iter_ =  src.lyric_iter_->clone ();
245
246   if (lyric_iter_)
247     scm_gc_unprotect_object (lyric_iter_->self_scm());
248
249   music_context_ = src.music_context_;
250   lyric_iter_ = src.lyric_iter_;
251     
252   
253   assert (false);               // shouldn't copy, really.
254 }
255
256
257 Music_iterator*
258 New_lyric_combine_music_iterator::try_music_in_children (Music *m) const
259 {
260   return lyric_iter_->try_music (m);
261 }
262
263
264 IMPLEMENT_CTOR_CALLBACK (New_lyric_combine_music_iterator);