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)
48 first_iter_ = src.first_iter_ ? src.first_iter_->clone () : 0;
49 second_iter_ = src.second_iter_ ? src.second_iter_->clone () : 0;
51 first_until_ = src.first_until_;
52 second_until_ = src.second_until_;
54 suffix_ = src.suffix_;
57 scm_gc_unprotect_object (first_iter_->self_scm());
59 scm_gc_unprotect_object (second_iter_->self_scm());
63 Part_combine_music_iterator::pending_moment () const
67 if (first_iter_->ok ())
68 p = p <? first_iter_->pending_moment ();
70 if (second_iter_->ok ())
71 p = p <? second_iter_->pending_moment ();
76 Part_combine_music_iterator::ok () const
78 return first_iter_->ok () || second_iter_->ok ();
82 Part_combine_music_iterator::construct_children ()
84 Part_combine_music const * m = dynamic_cast<Part_combine_music const*> (get_music ());
86 first_iter_ = unsmob_iterator (get_iterator (m->get_first ()));
87 second_iter_ = unsmob_iterator (get_iterator (m->get_second ()));
91 Part_combine_music_iterator::change_to (Music_iterator *it, String to_type,
94 Translator_group * current = it->report_to ();
95 Translator_group * last = 0;
98 Cut & Paste from from Auto_change_iterator from Change_iterator (ugh).
100 TODO: abstract this function
103 /* find the type of translator that we're changing.
105 If \translator Staff = bass, then look for Staff = *
107 while (current && current->type_string_ != to_type)
110 current = current->daddy_trans_;
113 if (current && current->id_string_ == to_id)
116 msg += _ ("Can't switch translators, I'm there already");
122 Translator_group * dest =
123 it->report_to ()->find_create_translator (to_type, to_id);
124 current->remove_translator (last);
125 dest->add_used_group_translator (last);
130 We could change the current translator's id, but that would make
133 last->translator_id_string_ = get_change ()->change_to_id_string_;
135 error (_f ("I'm one myself: `%s'", to_type.to_str0 ()));
138 error (_f ("none of these in my family: `%s'", to_id.to_str0 ()));
142 // SCM*, moet / kan dat niet met set_x ofzo?
144 get_music_info (Moment m, Music_iterator* iter, SCM *pitches, SCM *durations)
148 for (SCM i = iter->get_pending_events (m); gh_pair_p (i); i = ly_cdr (i))
150 Music *m = unsmob_music (ly_car (i));
151 if (Melodic_req *r = dynamic_cast<Melodic_req *> (m))
152 *pitches = gh_cons (r->get_mus_property ("pitch"), *pitches);
153 if (Rhythmic_req *r = dynamic_cast<Rhythmic_req *> (m))
155 SCM d = r->get_mus_property ("duration");
157 r->origin ()->warning ("Rhythmic_req has no duration\n");
159 // *durations = gh_cons (r->get_mus_property ("duration"), *durations);
160 *durations = gh_cons (d, *durations);
167 Part_combine_music_iterator::get_state (Moment)
170 Part_combine_music const *p = dynamic_cast<Part_combine_music const* > (get_music ());
172 String w = ly_scm2string (p->get_mus_property ("what"));
175 Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
177 SCM s = first_translator->get_property ("changeMoment");
181 Moment change_mom = *unsmob_moment (ly_car (s));
182 Moment diff_mom = *unsmob_moment (ly_cdr (s));
184 Moment now = pending_moment ();
186 if (!now.main_part_.mod_rat (change_mom.main_part_))
188 SCM interval = SCM_BOOL_F;
189 if (first_until_ < now)
191 if (second_until_ < now)
194 Moment first_mom = first_until_;
195 Moment second_mom = second_until_;
196 Moment diff_until = diff_mom + now;
200 Music_iterator *first_iter = first_iter_->clone ();
201 Music_iterator *second_iter = second_iter_->clone ();
203 Moment last_pending (-1);
204 Moment pending = now;
205 while (now < diff_until
206 && (first_iter->ok () || second_iter->ok ())
208 // urg, this is a hack, haven't caught this case yet
209 && (pending != last_pending))
211 if (!second_iter->ok ())
212 pending = first_iter->pending_moment ();
213 else if (!first_iter->ok ())
214 pending = second_iter->pending_moment ();
216 pending = first_iter->pending_moment () <? second_iter->pending_moment ();
217 last_pending = pending;
219 SCM first_pitches = SCM_EOL;
220 SCM first_durations = SCM_EOL;
221 get_music_info (pending, first_iter,
222 &first_pitches, &first_durations);
224 SCM second_pitches = SCM_EOL;
225 SCM second_durations = SCM_EOL;
226 get_music_info (pending, second_iter,
227 &second_pitches, &second_durations);
229 if (first_pitches != SCM_EOL && second_pitches != SCM_EOL)
231 scm_sort_list_x (first_pitches,
232 scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
233 scm_sort_list_x (second_pitches,
234 scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
236 interval = gh_int2scm (unsmob_pitch (ly_car (first_pitches))->steps ()
237 - unsmob_pitch (ly_car (scm_last_pair (second_pitches)))->steps ());
240 if (first_durations != SCM_EOL)
242 scm_sort_list_x (first_durations,
243 scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
244 first_mom += unsmob_duration (ly_car (first_durations))->length_mom ();
247 if (second_durations != SCM_EOL)
249 scm_sort_list_x (second_durations,
250 scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
251 second_mom += unsmob_duration (ly_car (second_durations))->length_mom ();
254 if (first_pitches != SCM_EOL && second_pitches == SCM_EOL
255 && ! (second_until_ > now))
258 state &= ~UNISILENCE;
259 if (! (state & ~ (UNRELATED | SOLO1 | UNISILENCE)))
265 if (first_pitches == SCM_EOL && second_pitches != SCM_EOL
266 && ! (first_until_ > now))
269 state &= ~UNISILENCE;
270 if (! (state & ~ (UNRELATED | SOLO2 | UNISILENCE)))
276 if (gh_equal_p (first_durations, second_durations))
278 state &= ~UNISILENCE;
279 if (! (state & ~ (UNIRHYTHM | UNISON)))
283 state &= ~ (UNIRHYTHM | UNISILENCE);
285 if (first_pitches != SCM_EOL
286 && gh_equal_p (first_pitches, second_pitches))
288 state &= ~UNISILENCE;
289 if (! (state & ~ (UNIRHYTHM | UNISON)))
295 if (first_pitches == SCM_EOL && second_pitches == SCM_EOL)
297 if (! (state & ~ (UNIRHYTHM | UNISILENCE)))
303 state &= ~ (UNISILENCE);
305 if (gh_number_p (interval))
307 SCM s = first_translator->get_property ("splitInterval");
308 int i = gh_scm2int (interval);
310 && gh_number_p (ly_car (s))
311 && gh_number_p (ly_cdr (s))
312 && i >= gh_scm2int (ly_car (s))
313 && i <= gh_scm2int (ly_cdr (s)))
315 if (! (state & ~ (SPLIT_INTERVAL | UNIRHYTHM | UNISON)))
316 state |= SPLIT_INTERVAL;
319 state &= ~ (SPLIT_INTERVAL);
322 if (first && first_pitches != SCM_EOL)
323 first_until_ = first_mom;
324 if (first && second_pitches != SCM_EOL)
325 second_until_ = second_mom;
328 if (first_iter->ok ())
329 first_iter->skip (pending);
330 if (second_iter->ok ())
331 second_iter->skip (pending);
334 scm_gc_unprotect_object (first_iter->self_scm ());
335 scm_gc_unprotect_object (second_iter->self_scm ());
341 static Span_req* abort_req = NULL;
344 Part_combine_music_iterator::process (Moment m)
349 - Use three named contexts (be it Thread or Voice): one, two, solo.
350 Let user pre-set (pushproperty) stem direction, remove
351 dynamic-engraver, and such.
353 **** Tried this, but won't work:
355 s Consider thread switching: threads "one", "two" and "both".
356 User can't pre-set the (most important) stem direction at
360 if (suffix_.empty_b ())
361 suffix_ = first_iter_->report_to ()
362 ->daddy_trans_->id_string_.cut_string (3, INT_MAX);
364 int state = get_state (m);
370 Part_combine_music const *p =
371 dynamic_cast<Part_combine_music const* > (get_music ());
374 bool previously_combined_b = first_iter_->report_to ()->daddy_trans_
375 == second_iter_->report_to ()->daddy_trans_;
377 bool combine_b = previously_combined_b;
379 if (! (state & UNIRHYTHM)
380 || (state & SPLIT_INTERVAL)
381 || (state & (SOLO1 | SOLO2)))
383 else if (state & (UNIRHYTHM | UNISILENCE))
387 When combining, abort all running spanners
392 abort_req = new Span_req;
393 abort_req->set_mus_property ("span-type", scm_makfrom0str ("abort"));
396 if (combine_b && combine_b != previously_combined_b)
398 if (second_iter_ && second_iter_->ok ())
399 second_iter_->try_music (abort_req);
401 String w = ly_scm2string (p->get_mus_property ("what"));
402 if (combine_b != previously_combined_b)
403 change_to (second_iter_, w, (combine_b ? "one" : "two")
406 Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
407 Translator_group *second_translator = second_iter_->report_to ()->find_create_translator (w, "two" + suffix_);
411 first_translator->set_property ("combineParts", SCM_BOOL_T);
412 second_translator ->set_property ("combineParts", SCM_BOOL_T);
416 SCM b = (state & UNIRHYTHM) ? SCM_BOOL_T : SCM_BOOL_F;
417 first_translator->set_property ("unirhythm", b);
418 second_translator->set_property ("unirhythm", b);
420 b = (state & SPLIT_INTERVAL) ? SCM_BOOL_T : SCM_BOOL_F;
421 first_translator->set_property ("split-interval", b);
422 second_translator->set_property ("split-interval", b);
424 b = (state & UNISILENCE) ? SCM_BOOL_T : SCM_BOOL_F;
425 first_translator->set_property ("unisilence", b);
426 second_translator->set_property ("unisilence", b);
428 // difference in definition...
429 //b = ((state & UNISON) ? SCM_BOOL_T : SCM_BOOL_F;
430 b = ((state & UNISON) && (state & UNIRHYTHM)) ? SCM_BOOL_T : SCM_BOOL_F;
431 first_translator->set_property ("unison", b);
432 second_translator->set_property ("unison", b);
434 SCM b1 = (state & SOLO1) ? SCM_BOOL_T : SCM_BOOL_F;
435 SCM b2 = (state & SOLO2) ? SCM_BOOL_T : SCM_BOOL_F;
436 first_translator->set_property ("solo", b1);
437 second_translator->set_property ("solo", b2);
439 /* Can't these be computed? */
440 first_translator->set_property ("othersolo", b2);
441 second_translator->set_property ("othersolo", b1);
443 if (first_iter_->ok ())
444 first_iter_->process (m);
446 if (second_iter_->ok ())
447 second_iter_->process (m);
451 Part_combine_music_iterator::try_music_in_children (Music *m) const
453 Music_iterator * i = first_iter_->try_music (m);
457 return second_iter_->try_music (m);
462 Part_combine_music_iterator::get_pending_events (Moment m)const
466 s = gh_append2 (s,first_iter_->get_pending_events (m));
468 s = gh_append2 (second_iter_->get_pending_events (m),s);
472 IMPLEMENT_CTOR_CALLBACK (Part_combine_music_iterator);