]> git.donarmstrong.com Git - lilypond.git/blob - lily/part-combine-music-iterator.cc
patch::: 1.3.78.jcn2
[lilypond.git] / lily / part-combine-music-iterator.cc
1 /*   
2   part-combine-music-iterator.cc -- implement  Part_combine_music_iterator
3
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
7  */
8
9 #include "part-combine-music.hh"
10 #include "part-combine-music-iterator.hh"
11 #include "translator-group.hh"
12 #include "musical-request.hh"
13 #include "warn.hh"
14
15 Part_combine_music_iterator::Part_combine_music_iterator ()
16 {
17   combined_b_ = false;
18
19   first_iter_p_ = 0;
20   second_iter_p_ = 0;
21 }
22
23 Part_combine_music_iterator::~Part_combine_music_iterator ()
24 {
25   delete second_iter_p_;
26   delete first_iter_p_;
27 }
28
29 Moment
30 Part_combine_music_iterator::next_moment () const
31 {
32   Moment first_next = first_iter_p_->next_moment ();
33   Moment second_next = second_iter_p_->next_moment ();
34   return first_next <? second_next;
35 }
36
37 bool
38 Part_combine_music_iterator::ok () const
39 {
40   //hmm
41   return first_iter_p_->ok ();
42 }
43
44 void
45 Part_combine_music_iterator::do_print () const
46 {
47   first_iter_p_->print ();
48   second_iter_p_->print ();
49 }
50
51 void
52 Part_combine_music_iterator::construct_children ()
53 {
54   Part_combine_music const * m = dynamic_cast<Part_combine_music const*> (music_l_);
55   
56   first_iter_p_ = get_iterator_p (m->first_l ());
57   second_iter_p_ = get_iterator_p (m->second_l ());
58 }
59
60 void
61 Part_combine_music_iterator::change_to (Music_iterator *it, String to_type,
62                                         String to_id)
63 {
64   Translator_group * current = it->report_to_l ();
65   Translator_group * last = 0;
66
67   /*
68     Cut & Paste from from Auto_change_iterator from Change_iterator (ugh).
69
70     TODO: abstract this function 
71    */
72   
73   /* find the type  of translator that we're changing.
74      
75      If \translator Staff = bass, then look for Staff = *
76    */
77   while  (current && current->type_str_ != to_type)
78     {
79       last = current;
80       current = current->daddy_trans_l_;
81     }
82
83   if (current && current->id_str_ == to_id)
84     {
85       String msg;
86       msg += _ ("Can't switch translators, I'm there already");
87     }
88   
89   if (current) 
90     if (last)
91       {
92         Translator_group * dest = 
93           it->report_to_l ()->find_create_translator_l (to_type, to_id);
94         current->remove_translator_p (last);
95         dest->add_translator (last);
96       }
97     else
98       {
99         /*
100           We could change the current translator's id, but that would make 
101           errors hard to catch
102           
103            last->translator_id_str_  = change_l ()->change_to_id_str_;
104         */
105         error (_f ("I'm one myself: `%s'", to_type.ch_C ()));
106       }
107   else
108     error (_f ("none of these in my family: `%s'", to_id.ch_C ()));
109 }
110
111 Pitch_interrogate_req* first_spanish_inquisition; // nobody expects it
112 Pitch_interrogate_req* second_spanish_inquisition; // won't strike twice
113
114 void
115 Part_combine_music_iterator::do_process_and_next (Moment m)
116 {
117   Moment first_next = first_iter_p_->next_moment ();
118   Moment second_next = second_iter_p_->next_moment ();
119
120   Part_combine_music const * p = dynamic_cast<Part_combine_music const* > (music_l_);
121
122   /*
123     different rhythm for combined voices: separate 
124     same rhythm for separated voices: combine
125
126     Arg.  Voices should be separated for small intervals, eg < 3.
127     This should be \property settable, and, we need the outcome
128     of the spanish_inquisition's...
129
130     Can't we first do a process_and_next go into a fake/tmp tree,
131     use + junk the result, and then do the real process_and_next...?
132     
133   */
134
135
136   if (first_next <= m)
137     first_iter_p_->process_and_next (m);
138
139   if (second_next <= m)
140     second_iter_p_->process_and_next (m);
141
142   Music_iterator::do_process_and_next (m);
143
144   /*
145     TODO:
146
147     * setting of stem directions by a2-engraver don't work !!
148
149     * check setting/resetting of properties
150
151     * use some unexpectedly handy abrvs.
152     
153     (customise) * separate for small ( <3 ?) intervals too
154       
155     Later (because currently, we only handle thread switching, really):
156
157     Maybe different modes exist?
158
159     * Wind instruments (Flute I/II)
160     * Hymnals:  
161
162
163       Rules for Hymnals/SATB (John Henckel <henckel@iname.com>):
164
165       1. if S and A differ by less than a third, the stems should be up/down.
166       2. else if S and A have different values, the stems should be up/down.
167       3. else if A sings "la" or higher, both S and A stems should be down.
168       4. else both stems should be up
169
170     * This may get really tricky: combining voices/staffs: string instruments
171
172    */
173   
174   if (!first_spanish_inquisition)
175     first_spanish_inquisition = new Pitch_interrogate_req;
176   Music_iterator* fit = first_iter_p_->try_music (first_spanish_inquisition);
177
178   if (!second_spanish_inquisition)
179     second_spanish_inquisition = new Pitch_interrogate_req;
180   Music_iterator* sit = second_iter_p_->try_music (second_spanish_inquisition);
181
182   SCM interval = SCM_BOOL_F;
183   if (first_spanish_inquisition->pitch_arr_.size ()
184       && second_spanish_inquisition->pitch_arr_.size ())
185     {
186       first_spanish_inquisition->pitch_arr_.sort (Musical_pitch::compare);
187       second_spanish_inquisition->pitch_arr_.sort (Musical_pitch::compare);
188       interval = gh_int2scm (first_spanish_inquisition->pitch_arr_.top ().semitone_pitch ());
189     }
190
191
192   Translator_group * fir = first_iter_p_->report_to_l ();
193   Translator_group * sir = second_iter_p_->report_to_l ();
194
195   bool solo = (first_spanish_inquisition->pitch_arr_.empty ()
196                != second_spanish_inquisition->pitch_arr_.empty ());
197
198   bool unirhythm_b = (first_next == second_next) && !solo;
199
200   bool too_close_b = false;
201   if (gh_number_p (interval) && gh_scm2int (interval) < 3)
202     too_close_b = true;
203
204   String to_id =  combined_b_ ? "one" : "two";
205   if ((!unirhythm_b && combined_b_)
206       || (unirhythm_b && !combined_b_)
207       || (too_close_b && combined_b_)
208       || (solo && combined_b_))
209     {
210       combined_b_ = !combined_b_;
211       to_id =  combined_b_ ? "one" : "two";
212       change_to (second_iter_p_, p->what_str_, to_id);
213     }
214
215   if (!combined_b_)
216     sir = second_iter_p_->report_to_l ();
217
218   Translator_group * fd = fir->find_create_translator_l (p->what_str_, "one");
219   Translator_group * sd = sir->find_create_translator_l (p->what_str_, "two");
220
221   fd->set_property ("unirhythm", unirhythm_b ? SCM_BOOL_T : SCM_BOOL_F);
222   sd->set_property ("unirhythm", unirhythm_b ? SCM_BOOL_T : SCM_BOOL_F);
223   fir->set_property ("unirhythm", unirhythm_b ? SCM_BOOL_T : SCM_BOOL_F);
224   sir->set_property ("unirhythm", unirhythm_b ? SCM_BOOL_T : SCM_BOOL_F);
225
226   if (unirhythm_b
227       && !compare (&first_spanish_inquisition->pitch_arr_,
228                    &second_spanish_inquisition->pitch_arr_))
229     {
230       fd->set_property ("unison", SCM_BOOL_T);
231       sd->set_property ("unison", SCM_BOOL_T);
232       fir->set_property ("unison", SCM_BOOL_T);
233       sir->set_property ("unison", SCM_BOOL_T);
234     }
235   else
236     {
237       fd->set_property ("unison", SCM_BOOL_F);
238       sd->set_property ("unison", SCM_BOOL_F);
239       fir->set_property ("unison", SCM_BOOL_F);
240       sir->set_property ("unison", SCM_BOOL_F);
241     }
242
243   fd->set_property ("interval", interval);
244   sd->set_property ("interval", interval);
245   fir->set_property ("interval", interval);
246   sir->set_property ("interval", interval);
247       
248   if (first_spanish_inquisition->pitch_arr_.size ()
249       && !second_spanish_inquisition->pitch_arr_.size ())
250     {
251       fd->set_property ("solo", SCM_BOOL_T);
252       fir->set_property ("solo", SCM_BOOL_T);
253     }
254   else
255     {
256       fd->set_property ("solo", SCM_BOOL_F);
257       fir->set_property ("solo", SCM_BOOL_F);
258     }
259
260
261   if (!first_spanish_inquisition->pitch_arr_.size ()
262       && second_spanish_inquisition->pitch_arr_.size ())
263     {
264       sd->set_property ("solo", SCM_BOOL_T);
265       sir->set_property ("solo", SCM_BOOL_T);
266     }
267   else
268     {
269       sd->set_property ("solo", SCM_BOOL_F);
270       sir->set_property ("solo", SCM_BOOL_F);
271     }
272
273
274   first_spanish_inquisition->pitch_arr_.clear ();
275   second_spanish_inquisition->pitch_arr_.clear ();
276 }
277
278 Music_iterator*
279 Part_combine_music_iterator::try_music_in_children (Music *m) const
280 {
281   Music_iterator * i =  first_iter_p_->try_music (m);
282   if (i)
283     return i;
284   else
285     return second_iter_p_->try_music (m);
286 }
287