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"
12 #include "musical-request.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 (Melodic_req *r = dynamic_cast<Melodic_req *> (m))
157 *pitches = gh_cons (r->get_mus_property ("pitch"), *pitches);
158 if (Rhythmic_req *r = dynamic_cast<Rhythmic_req *> (m))
160 SCM d = r->get_mus_property ("duration");
162 r->origin ()->warning ("Rhythmic_req has no duration\n");
164 // *durations = gh_cons (r->get_mus_property ("duration"), *durations);
165 *durations = gh_cons (d, *durations);
172 Part_combine_music_iterator::get_state (Moment)
175 Part_combine_music const *p = dynamic_cast<Part_combine_music const* > (get_music ());
177 String w = ly_scm2string (p->get_mus_property ("what"));
180 Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
182 SCM s = first_translator->get_property ("changeMoment");
186 Moment change_mom = *unsmob_moment (ly_car (s));
187 Moment diff_mom = *unsmob_moment (ly_cdr (s));
189 Moment now = pending_moment ();
191 if (!now.main_part_.mod_rat (change_mom.main_part_))
193 SCM interval = SCM_BOOL_F;
194 if (first_until_ < now)
196 if (second_until_ < now)
199 Moment first_mom = first_until_;
200 Moment second_mom = second_until_;
201 Moment diff_until = diff_mom + now;
205 Music_iterator *first_iter = first_iter_->clone ();
206 Music_iterator *second_iter = second_iter_->clone ();
208 Moment last_pending (-1);
209 Moment pending = now;
210 while (now < diff_until
211 && (first_iter->ok () || second_iter->ok ())
213 // urg, this is a hack, haven't caught this case yet
214 && (pending != last_pending))
216 if (!second_iter->ok ())
217 pending = first_iter->pending_moment ();
218 else if (!first_iter->ok ())
219 pending = second_iter->pending_moment ();
221 pending = first_iter->pending_moment () <? second_iter->pending_moment ();
222 last_pending = pending;
224 SCM first_pitches = SCM_EOL;
225 SCM first_durations = SCM_EOL;
226 get_music_info (pending, first_iter,
227 &first_pitches, &first_durations);
229 SCM second_pitches = SCM_EOL;
230 SCM second_durations = SCM_EOL;
231 get_music_info (pending, second_iter,
232 &second_pitches, &second_durations);
234 if (first_pitches != SCM_EOL && second_pitches != SCM_EOL)
236 scm_sort_list_x (first_pitches,
237 scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
238 scm_sort_list_x (second_pitches,
239 scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
241 interval = gh_int2scm (unsmob_pitch (ly_car (first_pitches))->steps ()
242 - unsmob_pitch (ly_car (scm_last_pair (second_pitches)))->steps ());
245 if (first_durations != SCM_EOL)
247 scm_sort_list_x (first_durations,
248 scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
249 first_mom += unsmob_duration (ly_car (first_durations))->length_mom ();
252 if (second_durations != SCM_EOL)
254 scm_sort_list_x (second_durations,
255 scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
256 second_mom += unsmob_duration (ly_car (second_durations))->length_mom ();
259 if (first_pitches != SCM_EOL && second_pitches == SCM_EOL
260 && ! (second_until_ > now))
263 state &= ~UNISILENCE;
264 if (! (state & ~ (UNRELATED | SOLO1 | UNISILENCE)))
270 if (first_pitches == SCM_EOL && second_pitches != SCM_EOL
271 && ! (first_until_ > now))
274 state &= ~UNISILENCE;
275 if (! (state & ~ (UNRELATED | SOLO2 | UNISILENCE)))
281 if (gh_equal_p (first_durations, second_durations))
283 state &= ~UNISILENCE;
284 if (! (state & ~ (UNIRHYTHM | UNISON)))
288 state &= ~ (UNIRHYTHM | UNISILENCE);
290 if (first_pitches != SCM_EOL
291 && gh_equal_p (first_pitches, second_pitches))
293 state &= ~UNISILENCE;
294 if (! (state & ~ (UNIRHYTHM | UNISON)))
300 if (first_pitches == SCM_EOL && second_pitches == SCM_EOL)
302 if (! (state & ~ (UNIRHYTHM | UNISILENCE)))
308 state &= ~ (UNISILENCE);
310 if (gh_number_p (interval))
312 SCM s = first_translator->get_property ("splitInterval");
313 int i = gh_scm2int (interval);
315 && gh_number_p (ly_car (s))
316 && gh_number_p (ly_cdr (s))
317 && i >= gh_scm2int (ly_car (s))
318 && i <= gh_scm2int (ly_cdr (s)))
320 if (! (state & ~ (SPLIT_INTERVAL | UNIRHYTHM | UNISON)))
321 state |= SPLIT_INTERVAL;
324 state &= ~ (SPLIT_INTERVAL);
327 if (first && first_pitches != SCM_EOL)
328 first_until_ = first_mom;
329 if (first && second_pitches != SCM_EOL)
330 second_until_ = second_mom;
333 if (first_iter->ok ())
334 first_iter->skip (pending);
335 if (second_iter->ok ())
336 second_iter->skip (pending);
339 scm_gc_unprotect_object (first_iter->self_scm ());
340 scm_gc_unprotect_object (second_iter->self_scm ());
346 static Span_req* abort_req = NULL;
349 Part_combine_music_iterator::process (Moment m)
354 - Use three named contexts (be it Thread or Voice): one, two, solo.
355 Let user pre-set (pushproperty) stem direction, remove
356 dynamic-engraver, and such.
358 **** Tried this, but won't work:
360 s Consider thread switching: threads "one", "two" and "both".
361 User can't pre-set the (most important) stem direction at
365 if (suffix_.empty_b ())
366 suffix_ = first_iter_->report_to ()
367 ->daddy_trans_->id_string_.cut_string (3, INT_MAX);
369 int state = get_state (m);
375 Part_combine_music const *p =
376 dynamic_cast<Part_combine_music const* > (get_music ());
379 bool previously_combined_b = first_iter_->report_to ()->daddy_trans_
380 == second_iter_->report_to ()->daddy_trans_;
382 bool combine_b = previously_combined_b;
384 if (! (state & UNIRHYTHM)
385 || (state & SPLIT_INTERVAL)
386 || (state & (SOLO1 | SOLO2)))
388 else if (state & (UNIRHYTHM | UNISILENCE))
392 When combining, abort all running spanners
397 abort_req = new Span_req;
398 abort_req->set_mus_property ("span-type", scm_makfrom0str ("abort"));
401 if (combine_b && combine_b != previously_combined_b)
403 if (second_iter_ && second_iter_->ok ())
404 second_iter_->try_music (abort_req);
406 String w = ly_scm2string (p->get_mus_property ("what"));
407 if (combine_b != previously_combined_b)
408 change_to (second_iter_, w, (combine_b ? "one" : "two")
411 Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
412 Translator_group *second_translator = second_iter_->report_to ()->find_create_translator (w, "two" + suffix_);
416 first_translator->set_property ("combineParts", SCM_BOOL_T);
417 second_translator ->set_property ("combineParts", SCM_BOOL_T);
421 SCM b = (state & UNIRHYTHM) ? SCM_BOOL_T : SCM_BOOL_F;
422 first_translator->set_property ("unirhythm", b);
423 second_translator->set_property ("unirhythm", b);
425 b = (state & SPLIT_INTERVAL) ? SCM_BOOL_T : SCM_BOOL_F;
426 first_translator->set_property ("split-interval", b);
427 second_translator->set_property ("split-interval", b);
429 b = (state & UNISILENCE) ? SCM_BOOL_T : SCM_BOOL_F;
430 first_translator->set_property ("unisilence", b);
431 second_translator->set_property ("unisilence", b);
433 // difference in definition...
434 //b = ((state & UNISON) ? SCM_BOOL_T : SCM_BOOL_F;
435 b = ((state & UNISON) && (state & UNIRHYTHM)) ? SCM_BOOL_T : SCM_BOOL_F;
436 first_translator->set_property ("unison", b);
437 second_translator->set_property ("unison", b);
439 SCM b1 = (state & SOLO1) ? SCM_BOOL_T : SCM_BOOL_F;
440 SCM b2 = (state & SOLO2) ? SCM_BOOL_T : SCM_BOOL_F;
441 first_translator->set_property ("solo", b1);
442 second_translator->set_property ("solo", b2);
444 /* Can't these be computed? */
445 first_translator->set_property ("othersolo", b2);
446 second_translator->set_property ("othersolo", b1);
448 if (first_iter_->ok ())
449 first_iter_->process (m);
451 if (second_iter_->ok ())
452 second_iter_->process (m);
456 Part_combine_music_iterator::try_music_in_children (Music *m) const
458 Music_iterator * i = first_iter_->try_music (m);
462 return second_iter_->try_music (m);
467 Part_combine_music_iterator::get_pending_events (Moment m)const
471 s = gh_append2 (s,first_iter_->get_pending_events (m));
473 s = gh_append2 (second_iter_->get_pending_events (m),s);
477 IMPLEMENT_CTOR_CALLBACK (Part_combine_music_iterator);