2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2011 Han-Wen Nienhuys
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "dispatcher.hh"
22 #include "lily-guile.hh"
24 #include "music-iterator.hh"
25 #include "music-sequence.hh"
30 CONTEXT_ONE, CONTEXT_TWO,
31 CONTEXT_SHARED, CONTEXT_SOLO,
32 CONTEXT_NULL, NUM_OUTLETS
35 static const char *outlet_names_[NUM_OUTLETS]
36 = {"one", "two", "shared", "solo", "null"};
38 class Part_combine_iterator : public Music_iterator
41 Part_combine_iterator ();
43 DECLARE_SCHEME_CALLBACK (constructor, ());
45 virtual void derived_substitute (Context *f, Context *t);
46 virtual void derived_mark () const;
48 virtual void construct_children ();
49 virtual Moment pending_moment () const;
50 virtual void do_quit ();
51 virtual void process (Moment);
53 virtual bool ok () const;
56 /* used by try_process */
57 DECLARE_LISTENER (set_busy);
61 bool try_process (Music_iterator *i, Moment m);
63 Music_iterator *first_iter_;
64 Music_iterator *second_iter_;
71 SCM horizontalShiftOne_;
72 SCM horizontalShiftTwo_;
74 Stream_event *unisono_event_;
75 Stream_event *solo_one_event_;
76 Stream_event *solo_two_event_;
77 Stream_event *mmrest_event_;
89 Status playing_state_;
92 Should be SOLO1 or SOLO2
97 TODO: this is getting off hand...
99 Context_handle handles_[NUM_OUTLETS];
101 void substitute_both (Outlet_type to1,
104 /* parameter is really Outlet_type */
105 void kill_mmrest (int in);
106 void chords_together ();
109 void apart (bool silent);
110 void unisono (bool silent);
114 Part_combine_iterator::do_quit ()
117 first_iter_->quit ();
119 second_iter_->quit ();
121 // Add listeners to all contexts except Devnull.
122 for (int i = 0; i < NUM_OUTLETS; i++)
124 Context *c = handles_[i].get_context ();
125 if (c->is_alias (ly_symbol2scm ("Voice")))
126 c->event_source ()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
127 handles_[i].set_context (0);
131 Part_combine_iterator::Part_combine_iterator ()
140 split_list_ = SCM_EOL;
141 direction_ = SCM_BOOL_F;
142 directionOne_ = scm_from_int (1);
143 directionTwo_ = scm_from_int (-1);
144 horizontalShiftOne_ = scm_from_int (0);
145 horizontalShiftTwo_ = scm_from_int (1);
147 playing_state_ = APART;
148 last_playing_ = APART;
151 notice_busy_ = false;
155 Part_combine_iterator::derived_mark () const
158 scm_gc_mark (first_iter_->self_scm ());
160 scm_gc_mark (second_iter_->self_scm ());
162 Stream_event *ptrs[] =
170 for (int i = 0; ptrs[i]; i++)
172 scm_gc_mark (ptrs[i]->self_scm ());
176 Part_combine_iterator::derived_substitute (Context *f,
180 first_iter_->substitute_outlet (f, t);
184 Part_combine_iterator::pending_moment () const
188 if (first_iter_->ok ())
189 p = min (p, first_iter_->pending_moment ());
191 if (second_iter_->ok ())
192 p = min (p, second_iter_->pending_moment ());
197 Part_combine_iterator::ok () const
199 return first_iter_->ok () || second_iter_->ok ();
203 Part_combine_iterator::substitute_both (Outlet_type to1,
206 Outlet_type tos[] = {to1, to2};
208 Music_iterator *mis[] = {first_iter_, second_iter_};
210 for (int i = 0; i < 2; i++)
212 for (int j = 0; j < NUM_OUTLETS; j++)
214 mis[i]->substitute_outlet (handles_[j].get_context (), handles_[tos[i]].get_context ());
217 for (int j = 0; j < NUM_OUTLETS; j++)
219 if (j != to1 && j != to2)
225 Part_combine_iterator::kill_mmrest (int in)
230 mmrest_event_ = new Stream_event (ly_symbol2scm ("multi-measure-rest-event"));
231 mmrest_event_->set_property ("duration", SCM_EOL);
232 mmrest_event_->unprotect ();
235 handles_[in].get_context ()->event_source ()->broadcast (mmrest_event_);
239 Part_combine_iterator::unisono (bool silent)
241 Status newstate = (silent) ? UNISILENCE : UNISONO;
243 if (newstate == state_)
248 If we're coming from SOLO2 state, we might have kill mmrests
249 in the 1st voice, so in that case, we use the second voice
250 as a basis for events.
252 Outlet_type c1 = (last_playing_ == SOLO2) ? CONTEXT_NULL : CONTEXT_SHARED;
253 Outlet_type c2 = (last_playing_ == SOLO2) ? CONTEXT_SHARED : CONTEXT_NULL;
254 substitute_both (c1, c2);
255 kill_mmrest ((last_playing_ == SOLO2) ? CONTEXT_ONE : CONTEXT_TWO);
256 kill_mmrest (CONTEXT_SHARED);
258 if (playing_state_ != UNISONO
259 && newstate == UNISONO)
263 unisono_event_ = new Stream_event (ly_symbol2scm ("unisono-event"));
264 unisono_event_->unprotect ();
267 Context *out = (last_playing_ == SOLO2 ? second_iter_ : first_iter_)
269 out->event_source ()->broadcast (unisono_event_);
270 playing_state_ = UNISONO;
277 Part_combine_iterator::solo1 ()
284 substitute_both (CONTEXT_SOLO, CONTEXT_NULL);
286 kill_mmrest (CONTEXT_TWO);
287 kill_mmrest (CONTEXT_SHARED);
289 if (playing_state_ != SOLO1)
291 if (!solo_one_event_)
293 solo_one_event_ = new Stream_event (ly_symbol2scm ("solo-one-event"));
294 solo_one_event_->unprotect ();
297 first_iter_->get_outlet ()->event_source ()->broadcast (solo_one_event_);
299 playing_state_ = SOLO1;
304 Part_combine_iterator::solo2 ()
312 substitute_both (CONTEXT_NULL, CONTEXT_SOLO);
314 if (playing_state_ != SOLO2)
316 if (!solo_two_event_)
318 solo_two_event_ = new Stream_event (ly_symbol2scm ("solo-two-event"));
319 solo_two_event_->unprotect ();
322 second_iter_->get_outlet ()->event_source ()->broadcast (solo_two_event_);
323 playing_state_ = SOLO2;
329 Part_combine_iterator::chords_together ()
331 if (state_ == TOGETHER)
335 playing_state_ = TOGETHER;
338 substitute_both (CONTEXT_SHARED, CONTEXT_SHARED);
343 Part_combine_iterator::apart (bool silent)
346 playing_state_ = APART;
353 substitute_both (CONTEXT_ONE, CONTEXT_TWO);
358 Part_combine_iterator::construct_children ()
360 start_moment_ = get_outlet ()->now_mom ();
361 split_list_ = get_music ()->get_property ("split-list");
362 direction_ = get_music ()->get_property ("direction");
363 if (is_direction (direction_))
365 directionOne_ = direction_;
366 directionTwo_ = direction_;
367 if (scm_is_true (scm_negative_p (direction_)))
369 horizontalShiftOne_ = scm_from_int (1);
370 horizontalShiftTwo_ = scm_from_int (0);
374 Context *c = get_outlet ();
376 for (int i = 0; i < NUM_OUTLETS; i++)
378 SCM type = (i == CONTEXT_NULL) ? ly_symbol2scm ("Devnull") : ly_symbol2scm ("Voice");
379 /* find context below c: otherwise we may create new staff for each voice */
380 c = c->find_create_context (type, outlet_names_[i], SCM_EOL);
381 handles_[i].set_context (c);
382 if (c->is_alias (ly_symbol2scm ("Voice")))
383 c->event_source ()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
386 SCM lst = get_music ()->get_property ("elements");
387 Context *one = handles_[CONTEXT_ONE].get_context ();
389 first_iter_ = unsmob_iterator (get_iterator (unsmob_music (scm_car (lst))));
390 Context *two = handles_[CONTEXT_TWO].get_context ();
392 second_iter_ = unsmob_iterator (get_iterator (unsmob_music (scm_cadr (lst))));
393 Context *shared = handles_[CONTEXT_SHARED].get_context ();
394 set_context (shared);
396 /* Mimic all settings of voiceOne/voiceTwo for the two separate voices...*/
397 /* FIXME: Is there any way to use the definition of \voiceOne/\voiceTwo
403 "DynamicLineSpanner",
413 for (char const **p = syms; *p; p++)
415 SCM sym = ly_symbol2scm (*p);
416 execute_pushpop_property (one, sym,
417 ly_symbol2scm ("direction"), directionOne_);
419 execute_pushpop_property (two, sym,
420 ly_symbol2scm ("direction"), directionTwo_);
422 if (scm_is_number (direction_))
423 execute_pushpop_property (shared, sym,
424 ly_symbol2scm ("direction"), direction_);
426 /* Handle horizontal shifts for crossing notes */
427 execute_pushpop_property (one, ly_symbol2scm ("NoteColumn"),
428 ly_symbol2scm ("horizontal-shift"), horizontalShiftOne_);
429 execute_pushpop_property (two, ly_symbol2scm ("NoteColumn"),
430 ly_symbol2scm ("horizontal-shift"), horizontalShiftTwo_);
431 /* Also handle MultiMeasureRest positions for voice 1/2 */
432 execute_pushpop_property (one, ly_symbol2scm ("MultiMeasureRest"),
433 ly_symbol2scm ("staff-position"), scm_from_int (4));
434 execute_pushpop_property (two, ly_symbol2scm ("MultiMeasureRest"),
435 ly_symbol2scm ("staff-position"), scm_from_int (-4));
439 IMPLEMENT_LISTENER (Part_combine_iterator, set_busy);
441 Part_combine_iterator::set_busy (SCM se)
446 Stream_event *e = unsmob_stream_event (se);
448 if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
453 Processes a moment in an iterator, and returns whether any new music
457 Part_combine_iterator::try_process (Music_iterator *i, Moment m)
464 notice_busy_ = false;
469 Part_combine_iterator::process (Moment m)
471 Moment now = get_outlet ()->now_mom ();
474 /* This is needed if construct_children was called before iteration
476 if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
479 for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
481 splitm = unsmob_moment (scm_caar (split_list_));
482 if (splitm && *splitm + start_moment_ > now)
485 SCM tag = scm_cdar (split_list_);
487 if (tag == ly_symbol2scm ("chords"))
489 else if (tag == ly_symbol2scm ("apart")
490 || tag == ly_symbol2scm ("apart-silence")
491 || tag == ly_symbol2scm ("apart-spanner"))
492 apart (tag == ly_symbol2scm ("apart-silence"));
493 else if (tag == ly_symbol2scm ("unisono"))
495 else if (tag == ly_symbol2scm ("unisilence"))
497 else if (tag == ly_symbol2scm ("solo1"))
499 else if (tag == ly_symbol2scm ("solo2"))
501 else if (scm_is_symbol (tag))
503 string s = "Unknown split directive: "
504 + (scm_is_symbol (tag) ? ly_symbol2string (tag) : string ("not a symbol"));
505 programming_error (s);
509 if (first_iter_->ok ())
511 if (try_process (first_iter_, m))
512 last_playing_ = SOLO1;
515 if (second_iter_->ok ())
517 if (try_process (second_iter_, m))
518 last_playing_ = SOLO2;
522 IMPLEMENT_CTOR_CALLBACK (Part_combine_iterator);