]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-lyric-combine-music-iterator.cc
1b3a14a84a5716b83999c74ccd37ce521cf171b2
[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   New_lyric_combine_music_iterator ();
21   New_lyric_combine_music_iterator (New_lyric_combine_music_iterator const&src);
22   DECLARE_SCHEME_CALLBACK(constructor, ());
23 protected:
24   virtual void construct_children ();
25   virtual Moment pending_moment () const;
26   virtual void do_quit(); 
27   virtual void process (Moment);
28   virtual Music_iterator *try_music_in_children (Music *) const;
29   virtual bool run_always ()const;
30   virtual bool ok () const;
31   virtual void derived_mark () const;
32   virtual void derived_substitute (Translator_group*,Translator_group*);
33 private:
34   bool start_new_syllable () ;
35   void find_thread ();
36   
37   Translator_group * lyrics_context_;
38   Translator_group* music_context_;
39   Music_iterator * lyric_iter_;
40 };
41
42 /*
43   Ugh, why static?
44  */
45 static Music *busy_ev;
46 static Music *start_ev;
47 static Music *melisma_playing_ev;
48
49 New_lyric_combine_music_iterator::New_lyric_combine_music_iterator ()
50 {
51   lyric_iter_ =0;
52   music_context_ =0;
53   lyrics_context_ = 0;
54   if (!busy_ev)
55     {
56       busy_ev
57         = make_music_by_name (ly_symbol2scm ("BusyPlayingEvent"));
58       start_ev
59         = make_music_by_name (ly_symbol2scm ("StartPlayingEvent"));
60       melisma_playing_ev
61         = make_music_by_name (ly_symbol2scm ("MelismaPlayingEvent"));
62     }
63 }
64
65 bool
66 New_lyric_combine_music_iterator::start_new_syllable ()
67 {
68   bool b = music_context_->try_music (busy_ev);
69   
70   if (!b)
71     return false;
72
73   if (!lyrics_context_)
74     return false;
75   
76   if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
77     {
78       bool m = music_context_->try_music (melisma_playing_ev);
79       if (m)
80         return false;
81     }
82   
83   return true;
84 }
85
86 Moment
87 New_lyric_combine_music_iterator::pending_moment () const
88 {
89   Moment m;
90
91   m.set_infinite (1);
92     
93   return m;
94 }
95
96 bool
97 New_lyric_combine_music_iterator::run_always () const
98 {
99   return true;
100 }
101
102 bool
103 New_lyric_combine_music_iterator::ok () const
104 {
105   return lyric_iter_ && lyric_iter_->ok ();
106 }
107
108 void
109 New_lyric_combine_music_iterator::derived_mark()const
110 {
111   if (lyric_iter_)
112     scm_gc_mark (lyric_iter_->self_scm ());
113   if (lyrics_context_)
114     scm_gc_mark (lyrics_context_->self_scm ());
115   if (music_context_)
116     scm_gc_mark (music_context_->self_scm ());
117 }
118
119 void
120 New_lyric_combine_music_iterator::derived_substitute (Translator_group*f, Translator_group*t)
121 {
122   if (lyric_iter_)
123     lyric_iter_->substitute_outlet (f,t);
124   if (lyrics_context_ && lyrics_context_==f)
125     lyrics_context_ = t;
126   if (music_context_ && music_context_ == f)
127     music_context_ = t; 
128 }
129
130 /*
131   ID == "" means accept any ID.
132  */
133 Translator_group *
134 find_context_below (Translator_group * where,
135                     String type, String id)
136 {
137   if (where->is_alias (ly_symbol2scm (type.to_str0 ())))
138     {
139       if (id == "" || where->id_string_ == id)
140         return where;
141     }
142   
143   Translator_group * found = 0;
144   for (SCM s = where->trans_group_list_;
145        !found && gh_pair_p (s); s = gh_cdr (s))
146     {
147       Translator_group * tr = dynamic_cast<Translator_group*> (unsmob_translator (gh_car (s)));
148
149       found = find_context_below (tr, type, id);
150     }
151
152   return found; 
153 }
154
155
156
157 void
158 New_lyric_combine_music_iterator::construct_children ()
159 {
160   Music *m = unsmob_music (get_music ()->get_mus_property ("element"));
161   lyric_iter_ = unsmob_iterator (get_iterator (m));
162
163   find_thread ();
164   
165   if (lyric_iter_)
166     lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
167                                           "LyricsVoice", "");
168
169   /*
170     We do not create a LyricsVoice context, because the user might
171     create one with a different name, and then we will not find that
172     one.
173    */
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 = get_outlet ();
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
238
239 Music_iterator*
240 New_lyric_combine_music_iterator::try_music_in_children (Music *m) const
241 {
242   return lyric_iter_->try_music (m);
243 }
244
245
246 IMPLEMENT_CTOR_CALLBACK (New_lyric_combine_music_iterator);