]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-lyric-combine-music-iterator.cc
f2eff8b6f327bc1c8447c02655b1b52baa279bd9
[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 (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
75     {
76       bool m = music_context_->try_music (melisma_playing_ev);
77       if (m)
78         return false;
79     }
80   
81   return true;
82 }
83
84 Moment
85 New_lyric_combine_music_iterator::pending_moment () const
86 {
87   Moment m;
88
89   m.set_infinite (1);
90     
91   return m;
92 }
93
94 bool
95 New_lyric_combine_music_iterator::run_always () const
96 {
97   return true;
98 }
99
100 bool
101 New_lyric_combine_music_iterator::ok () const
102 {
103   return lyric_iter_ && lyric_iter_->ok ();
104 }
105
106 void
107 New_lyric_combine_music_iterator::derived_mark()const
108 {
109   if (lyric_iter_)
110     scm_gc_mark (lyric_iter_->self_scm ());
111   if (lyrics_context_)
112     scm_gc_mark (lyrics_context_->self_scm ());
113   if (music_context_)
114     scm_gc_mark (music_context_->self_scm ());
115 }
116
117 void
118 New_lyric_combine_music_iterator::derived_substitute (Translator_group*f, Translator_group*t)
119 {
120   if (lyric_iter_)
121     lyric_iter_->substitute_outlet (f,t);
122   if (lyrics_context_ && lyrics_context_==f)
123     lyrics_context_ = t;
124   if (music_context_ && music_context_ == f)
125     music_context_ = t; 
126 }
127
128 /*
129   ID == "" means accept any ID.
130  */
131 Translator_group *
132 find_context_below (Translator_group * where,
133                     String type, String id)
134 {
135   if (where->context_name () == type)
136     {
137       if (id == "" || where->id_string_ == id)
138         return where;
139     }
140   
141   Translator_group * found = 0;
142   for (SCM s = where->trans_group_list_;
143        !found && gh_pair_p (s); s = gh_cdr (s))
144     {
145       Translator_group * tr = dynamic_cast<Translator_group*> (unsmob_translator (gh_car (s)));
146
147       
148       found = find_context_below (tr, type, id);
149     }
150
151   return found; 
152 }
153
154
155
156 void
157 New_lyric_combine_music_iterator::construct_children ()
158 {
159   Music *m = unsmob_music (get_music ()->get_mus_property ("element"));
160   lyric_iter_ = unsmob_iterator (get_iterator (m));
161
162   if (lyric_iter_)
163     {
164       lyrics_context_ = find_context_below (lyric_iter_->report_to (), "LyricsVoice", "");
165     }
166
167   find_thread ();
168 }
169
170 void
171 New_lyric_combine_music_iterator::find_thread ()
172 {
173   if (!music_context_)
174     {
175       SCM voice_name = get_music ()->get_mus_property ("associated-context");
176   
177       if (gh_string_p (voice_name))
178         {
179           Translator_group *t = report_to ();
180           while (t && t->daddy_trans_)
181             t = t->daddy_trans_;
182
183           String name =  ly_scm2string (voice_name);
184           Translator_group* voice = find_context_below (t, "Voice", name);
185           Translator_group *thread = 0;
186           if (voice)
187             thread = find_context_below (voice, "Thread", "");
188           else
189             get_music ()->origin ()->warning (_f("Cannot find Voice: %s\n", name.to_str0())); 
190
191           if (thread)
192             music_context_ = thread;
193             
194           if (lyrics_context_ && voice)
195             lyrics_context_->set_property ("associatedVoiceContext",  voice->self_scm ());
196         }
197     }
198 }
199
200 void
201 New_lyric_combine_music_iterator::process (Moment )
202 {
203   find_thread ();
204   if (!music_context_)
205     return ;
206   
207   if (!music_context_->daddy_trans_)
208     {
209       music_context_ = 0;
210       if (lyrics_context_)
211         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
212     }
213   
214   if (music_context_
215       && start_new_syllable () && lyric_iter_->ok ())
216     {
217       Moment m= lyric_iter_->pending_moment ();
218       lyric_iter_->process (m);
219     }
220 }
221
222 void
223 New_lyric_combine_music_iterator::do_quit ()
224 {
225   if (lyric_iter_)
226     lyric_iter_->quit();
227 }
228
229 New_lyric_combine_music_iterator::New_lyric_combine_music_iterator (New_lyric_combine_music_iterator const & src)
230     : Music_iterator (src)
231 {
232   lyric_iter_ = 0;
233
234   if (src.lyric_iter_)
235     lyric_iter_ =  src.lyric_iter_->clone ();
236
237   if (lyric_iter_)
238     scm_gc_unprotect_object (lyric_iter_->self_scm());
239
240   music_context_ = src.music_context_;
241   lyric_iter_ = src.lyric_iter_;
242     
243   
244   assert (false);               // shouldn't copy, really.
245 }
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
255 IMPLEMENT_CTOR_CALLBACK (New_lyric_combine_music_iterator);