]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-lyric-combine-music-iterator.cc
* lily/new-lyric-combine-music-iterator.cc (find_voice): make sure
[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_voice ();
36
37   bool made_association_;
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   made_association_ = false;
53   lyric_iter_ =0;
54   music_context_ =0;
55   lyrics_context_ = 0;
56
57   /*
58     Ugh. out of place here.
59    */
60   if (!busy_ev)
61     {
62       busy_ev
63         = make_music_by_name (ly_symbol2scm ("BusyPlayingEvent"));
64       start_ev
65         = make_music_by_name (ly_symbol2scm ("StartPlayingEvent"));
66       melisma_playing_ev
67         = make_music_by_name (ly_symbol2scm ("MelismaPlayingEvent"));
68     }
69 }
70
71 bool
72 New_lyric_combine_music_iterator::start_new_syllable ()
73 {
74   bool b = music_context_->try_music (busy_ev);
75   
76   if (!b)
77     return false;
78
79   if (!lyrics_context_)
80     return false;
81   
82   if (!to_boolean (lyrics_context_->get_property ("ignoreMelismata")))
83     {
84       bool m = music_context_->try_music (melisma_playing_ev);
85       if (m)
86         return false;
87     }
88   
89   return true;
90 }
91
92 Moment
93 New_lyric_combine_music_iterator::pending_moment () const
94 {
95   Moment m;
96
97   m.set_infinite (1);
98     
99   return m;
100 }
101
102 bool
103 New_lyric_combine_music_iterator::run_always () const
104 {
105   return true;
106 }
107
108 bool
109 New_lyric_combine_music_iterator::ok () const
110 {
111   return lyric_iter_ && lyric_iter_->ok ();
112 }
113
114 void
115 New_lyric_combine_music_iterator::derived_mark()const
116 {
117   if (lyric_iter_)
118     scm_gc_mark (lyric_iter_->self_scm ());
119   if (lyrics_context_)
120     scm_gc_mark (lyrics_context_->self_scm ());
121   if (music_context_)
122     scm_gc_mark (music_context_->self_scm ());
123 }
124
125 void
126 New_lyric_combine_music_iterator::derived_substitute (Translator_group*f, Translator_group*t)
127 {
128   if (lyric_iter_)
129     lyric_iter_->substitute_outlet (f,t);
130   if (lyrics_context_ && lyrics_context_==f)
131     lyrics_context_ = t;
132   if (music_context_ && music_context_ == f)
133     music_context_ = t; 
134 }
135
136 /*
137   ID == "" means accept any ID.
138  */
139 Translator_group *
140 find_context_below (Translator_group * where,
141                     String type, String id)
142 {
143   if (where->is_alias (ly_symbol2scm (type.to_str0 ())))
144     {
145       if (id == "" || where->id_string_ == id)
146         return where;
147     }
148   
149   Translator_group * found = 0;
150   for (SCM s = where->trans_group_list_;
151        !found && gh_pair_p (s); s = gh_cdr (s))
152     {
153       Translator_group * tr = dynamic_cast<Translator_group*> (unsmob_translator (gh_car (s)));
154
155       found = find_context_below (tr, type, id);
156     }
157
158   return found; 
159 }
160
161
162
163 void
164 New_lyric_combine_music_iterator::construct_children ()
165 {
166   Music *m = unsmob_music (get_music ()->get_mus_property ("element"));
167   lyric_iter_ = unsmob_iterator (get_iterator (m));
168
169   find_voice ();
170   
171   if (lyric_iter_)
172     lyrics_context_ = find_context_below (lyric_iter_->get_outlet (),
173                                           "LyricsVoice", "");
174
175   /*
176     We do not create a LyricsVoice context, because the user might
177     create one with a different name, and then we will not find that
178     one.
179    */
180 }
181
182 void
183 New_lyric_combine_music_iterator::find_voice ()
184 {
185   if (!music_context_)
186     {
187       SCM voice_name = get_music ()->get_mus_property ("associated-context");
188   
189       if (gh_string_p (voice_name))
190         {
191           Translator_group *t = get_outlet ();
192           while (t && t->daddy_trans_)
193             t = t->daddy_trans_;
194
195           String name = ly_scm2string (voice_name);
196           Translator_group *voice = find_context_below (t, "Voice", name);
197           if (!voice)
198             get_music ()->origin ()->warning (_f ("Cannot find Voice: %s\n",
199                                                   name.to_str0 ())); 
200           else
201             music_context_ = voice;
202             
203         }
204     }
205
206   if (lyrics_context_ && music_context_)
207     {
208       if (!made_association_)
209         {
210           made_association_ = true; 
211           lyrics_context_->set_property ("associatedVoiceContext",
212                                          music_context_->self_scm ());
213         }
214     }
215 }
216
217 void
218 New_lyric_combine_music_iterator::process (Moment )
219 {
220   find_voice ();
221   if (!music_context_)
222     return ;
223   
224   if (!music_context_->daddy_trans_)
225     {
226       music_context_ = 0;
227       if (lyrics_context_)
228         lyrics_context_->unset_property (ly_symbol2scm ("associatedVoiceContext"));
229     }
230   
231   if (music_context_
232       && start_new_syllable () && lyric_iter_->ok ())
233     {
234       Moment m= lyric_iter_->pending_moment ();
235       lyric_iter_->process (m);
236     }
237 }
238
239 void
240 New_lyric_combine_music_iterator::do_quit ()
241 {
242   if (lyric_iter_)
243     lyric_iter_->quit();
244 }
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);