2 part-combine-music-iterator.cc -- implement Part_combine_music_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2002 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "part-combine-music.hh"
10 #include "part-combine-music-iterator.hh"
11 #include "translator-group.hh"
13 #include "music-sequence.hh"
14 #include "lily-guile.hh"
17 Part_combine_music_iterator::Part_combine_music_iterator ()
28 Part_combine_music_iterator::derived_mark () const
31 scm_gc_mark (first_iter_->self_scm());
33 scm_gc_mark(second_iter_->self_scm());
37 Part_combine_music_iterator::do_quit ()
45 Part_combine_music_iterator::Part_combine_music_iterator (Part_combine_music_iterator const &src)
46 : Music_iterator (src)
52 first_iter_ = src.first_iter_->clone ();
54 second_iter_ = src.second_iter_->clone ();
56 first_until_ = src.first_until_;
57 second_until_ = src.second_until_;
59 suffix_ = src.suffix_;
62 scm_gc_unprotect_object (first_iter_->self_scm());
64 scm_gc_unprotect_object (second_iter_->self_scm());
68 Part_combine_music_iterator::pending_moment () const
72 if (first_iter_->ok ())
73 p = p <? first_iter_->pending_moment ();
75 if (second_iter_->ok ())
76 p = p <? second_iter_->pending_moment ();
81 Part_combine_music_iterator::ok () const
83 return first_iter_->ok () || second_iter_->ok ();
87 Part_combine_music_iterator::construct_children ()
89 Part_combine_music const * m = dynamic_cast<Part_combine_music const*> (get_music ());
91 first_iter_ = unsmob_iterator (get_iterator (m->get_first ()));
92 second_iter_ = unsmob_iterator (get_iterator (m->get_second ()));
96 Part_combine_music_iterator::change_to (Music_iterator *it, String to_type,
99 Translator_group * current = it->report_to ();
100 Translator_group * last = 0;
103 Cut & Paste from from Auto_change_iterator from Change_iterator (ugh).
105 TODO: abstract this function
108 /* find the type of translator that we're changing.
110 If \translator Staff = bass, then look for Staff = *
112 while (current && current->type_string_ != to_type)
115 current = current->daddy_trans_;
118 if (current && current->id_string_ == to_id)
121 msg += _ ("Can't switch translators, I'm there already");
127 Translator_group * dest =
128 it->report_to ()->find_create_translator (to_type, to_id);
129 current->remove_translator (last);
130 dest->add_used_group_translator (last);
135 We could change the current translator's id, but that would make
138 last->translator_id_string_ = get_change ()->change_to_id_string_;
140 error (_f ("I'm one myself: `%s'", to_type.to_str0 ()));
143 error (_f ("none of these in my family: `%s'", to_id.to_str0 ()));
147 // SCM*, moet / kan dat niet met set_x ofzo?
149 get_music_info (Moment m, Music_iterator* iter, SCM *pitches, SCM *durations)
153 for (SCM i = iter->get_pending_events (m); gh_pair_p (i); i = ly_cdr (i))
155 Music *m = unsmob_music (ly_car (i));
156 if (m->is_mus_type ("melodic-event"))
157 *pitches = gh_cons (m->get_mus_property ("pitch"), *pitches);
158 if (m->is_mus_type ("rhythmic-event"))
160 SCM d = m->get_mus_property ("duration");
162 m->origin ()->warning ("Music has no duration\n");
164 *durations = gh_cons (d, *durations);
171 Part_combine_music_iterator::get_state (Moment)
174 Part_combine_music const *p = dynamic_cast<Part_combine_music const* > (get_music ());
176 String w = ly_scm2string (p->get_mus_property ("what"));
179 Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
181 SCM s = first_translator->get_property ("changeMoment");
185 Moment change_mom = *unsmob_moment (ly_car (s));
186 Moment diff_mom = *unsmob_moment (ly_cdr (s));
188 Moment now = pending_moment ();
190 if (!now.main_part_.mod_rat (change_mom.main_part_))
192 SCM interval = SCM_BOOL_F;
193 if (first_until_ < now)
195 if (second_until_ < now)
198 Moment first_mom = first_until_;
199 Moment second_mom = second_until_;
200 Moment diff_until = diff_mom + now;
204 Music_iterator *first_iter = first_iter_->clone ();
205 Music_iterator *second_iter = second_iter_->clone ();
207 Moment last_pending (-1);
208 Moment pending = now;
209 while (now < diff_until
210 && (first_iter->ok () || second_iter->ok ())
212 // urg, this is a hack, haven't caught this case yet
213 && (pending != last_pending))
215 if (!second_iter->ok ())
216 pending = first_iter->pending_moment ();
217 else if (!first_iter->ok ())
218 pending = second_iter->pending_moment ();
220 pending = first_iter->pending_moment () <? second_iter->pending_moment ();
221 last_pending = pending;
223 SCM first_pitches = SCM_EOL;
224 SCM first_durations = SCM_EOL;
225 get_music_info (pending, first_iter,
226 &first_pitches, &first_durations);
228 SCM second_pitches = SCM_EOL;
229 SCM second_durations = SCM_EOL;
230 get_music_info (pending, second_iter,
231 &second_pitches, &second_durations);
233 if (first_pitches != SCM_EOL && second_pitches != SCM_EOL)
235 scm_sort_list_x (first_pitches,
236 scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
237 scm_sort_list_x (second_pitches,
238 scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
240 interval = gh_int2scm (unsmob_pitch (ly_car (first_pitches))->steps ()
241 - unsmob_pitch (ly_car (scm_last_pair (second_pitches)))->steps ());
244 if (first_durations != SCM_EOL)
246 scm_sort_list_x (first_durations,
247 scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
248 first_mom += unsmob_duration (ly_car (first_durations))->length_mom ();
251 if (second_durations != SCM_EOL)
253 scm_sort_list_x (second_durations,
254 scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
255 second_mom += unsmob_duration (ly_car (second_durations))->length_mom ();
258 if (first_pitches != SCM_EOL && second_pitches == SCM_EOL
259 && ! (second_until_ > now))
262 state &= ~UNISILENCE;
263 if (! (state & ~ (UNRELATED | SOLO1 | UNISILENCE)))
269 if (first_pitches == SCM_EOL && second_pitches != SCM_EOL
270 && ! (first_until_ > now))
273 state &= ~UNISILENCE;
274 if (! (state & ~ (UNRELATED | SOLO2 | UNISILENCE)))
280 if (gh_equal_p (first_durations, second_durations))
282 state &= ~UNISILENCE;
283 if (! (state & ~ (UNIRHYTHM | UNISON)))
287 state &= ~ (UNIRHYTHM | UNISILENCE);
289 if (first_pitches != SCM_EOL
290 && gh_equal_p (first_pitches, second_pitches))
292 state &= ~UNISILENCE;
293 if (! (state & ~ (UNIRHYTHM | UNISON)))
299 if (first_pitches == SCM_EOL && second_pitches == SCM_EOL)
301 if (! (state & ~ (UNIRHYTHM | UNISILENCE)))
307 state &= ~ (UNISILENCE);
309 if (gh_number_p (interval))
311 SCM s = first_translator->get_property ("splitInterval");
312 int i = gh_scm2int (interval);
314 && gh_number_p (ly_car (s))
315 && gh_number_p (ly_cdr (s))
316 && i >= gh_scm2int (ly_car (s))
317 && i <= gh_scm2int (ly_cdr (s)))
319 if (! (state & ~ (SPLIT_INTERVAL | UNIRHYTHM | UNISON)))
320 state |= SPLIT_INTERVAL;
323 state &= ~ (SPLIT_INTERVAL);
326 if (first && first_pitches != SCM_EOL)
327 first_until_ = first_mom;
328 if (first && second_pitches != SCM_EOL)
329 second_until_ = second_mom;
332 if (first_iter->ok ())
333 first_iter->skip (pending);
334 if (second_iter->ok ())
335 second_iter->skip (pending);
338 scm_gc_unprotect_object (first_iter->self_scm ());
339 scm_gc_unprotect_object (second_iter->self_scm ());
345 static Music* abort_req = NULL;
348 Part_combine_music_iterator::process (Moment m)
353 - Use three named contexts (be it Thread or Voice): one, two, solo.
354 Let user pre-set (pushproperty) stem direction, remove
355 dynamic-engraver, and such.
357 **** Tried this, but won't work:
359 s Consider thread switching: threads "one", "two" and "both".
360 User can't pre-set the (most important) stem direction at
364 if (suffix_.empty_b ())
365 suffix_ = first_iter_->report_to ()
366 ->daddy_trans_->id_string_.cut_string (3, INT_MAX);
368 int state = get_state (m);
374 Part_combine_music const *p =
375 dynamic_cast<Part_combine_music const* > (get_music ());
378 bool previously_combined_b = first_iter_->report_to ()->daddy_trans_
379 == second_iter_->report_to ()->daddy_trans_;
381 bool combine_b = previously_combined_b;
383 if (! (state & UNIRHYTHM)
384 || (state & SPLIT_INTERVAL)
385 || (state & (SOLO1 | SOLO2)))
387 else if (state & (UNIRHYTHM | UNISILENCE))
391 When combining, abort all running spanners
396 abort_req = make_music_by_name (ly_symbol2scm ("AbortEvent"));
399 if (combine_b && combine_b != previously_combined_b)
401 if (second_iter_ && second_iter_->ok ())
402 second_iter_->try_music (abort_req);
404 String w = ly_scm2string (p->get_mus_property ("what"));
405 if (combine_b != previously_combined_b)
406 change_to (second_iter_, w, (combine_b ? "one" : "two")
409 Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
410 Translator_group *second_translator = second_iter_->report_to ()->find_create_translator (w, "two" + suffix_);
414 first_translator->set_property ("combineParts", SCM_BOOL_T);
415 second_translator ->set_property ("combineParts", SCM_BOOL_T);
419 SCM b = (state & UNIRHYTHM) ? SCM_BOOL_T : SCM_BOOL_F;
420 first_translator->set_property ("unirhythm", b);
421 second_translator->set_property ("unirhythm", b);
423 b = (state & SPLIT_INTERVAL) ? SCM_BOOL_T : SCM_BOOL_F;
424 first_translator->set_property ("split-interval", b);
425 second_translator->set_property ("split-interval", b);
427 b = (state & UNISILENCE) ? SCM_BOOL_T : SCM_BOOL_F;
428 first_translator->set_property ("unisilence", b);
429 second_translator->set_property ("unisilence", b);
431 // difference in definition...
432 //b = ((state & UNISON) ? SCM_BOOL_T : SCM_BOOL_F;
433 b = ((state & UNISON) && (state & UNIRHYTHM)) ? SCM_BOOL_T : SCM_BOOL_F;
434 first_translator->set_property ("unison", b);
435 second_translator->set_property ("unison", b);
437 SCM b1 = (state & SOLO1) ? SCM_BOOL_T : SCM_BOOL_F;
438 SCM b2 = (state & SOLO2) ? SCM_BOOL_T : SCM_BOOL_F;
439 first_translator->set_property ("solo", b1);
440 second_translator->set_property ("solo", b2);
442 /* Can't these be computed? */
443 first_translator->set_property ("othersolo", b2);
444 second_translator->set_property ("othersolo", b1);
446 if (first_iter_->ok ())
447 first_iter_->process (m);
449 if (second_iter_->ok ())
450 second_iter_->process (m);
454 Part_combine_music_iterator::try_music_in_children (Music *m) const
456 Music_iterator * i = first_iter_->try_music (m);
460 return second_iter_->try_music (m);
465 Part_combine_music_iterator::get_pending_events (Moment m)const
469 s = gh_append2 (s,first_iter_->get_pending_events (m));
471 s = gh_append2 (second_iter_->get_pending_events (m),s);
475 IMPLEMENT_CTOR_CALLBACK (Part_combine_music_iterator);