2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2015 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 */
61 bool try_process (Music_iterator *i, Moment m);
63 Music_iterator *first_iter_;
64 Music_iterator *second_iter_;
69 Stream_event *mmrest_event_;
81 // For states in which it matters, this is the relevant part,
82 // e.g. 1 for Solo I, 2 for Solo II.
85 // States for generating partcombine text.
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, int newpart);
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 (Part_combine_iterator, set_busy), ly_symbol2scm ("music-event"));
127 handles_[i].set_context (0);
131 Part_combine_iterator::Part_combine_iterator ()
137 split_list_ = SCM_EOL;
140 playing_state_ = PLAYING_OTHER;
144 notice_busy_ = false;
148 Part_combine_iterator::derived_mark () const
151 scm_gc_mark (first_iter_->self_scm ());
153 scm_gc_mark (second_iter_->self_scm ());
155 scm_gc_mark (mmrest_event_->self_scm ());
159 Part_combine_iterator::derived_substitute (Context *f,
163 first_iter_->substitute_outlet (f, t);
167 Part_combine_iterator::pending_moment () const
171 if (first_iter_->ok ())
172 p = min (p, first_iter_->pending_moment ());
174 if (second_iter_->ok ())
175 p = min (p, second_iter_->pending_moment ());
180 Part_combine_iterator::ok () const
182 return first_iter_->ok () || second_iter_->ok ();
186 Part_combine_iterator::substitute_both (Outlet_type to1,
189 first_iter_->substitute_outlet (first_iter_->get_outlet (),
190 handles_[to1].get_context ());
191 second_iter_->substitute_outlet (second_iter_->get_outlet (),
192 handles_[to2].get_context ());
194 for (int j = 0; j < NUM_OUTLETS; j++)
196 if (j != to1 && j != to2)
202 Part_combine_iterator::kill_mmrest (int in)
207 mmrest_event_ = new Stream_event
208 (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"),
209 ly_symbol2scm ("multi-measure-rest-event")));
210 mmrest_event_->set_property ("duration", SCM_EOL);
211 mmrest_event_->unprotect ();
214 handles_[in].get_context ()->event_source ()->broadcast (mmrest_event_);
218 Part_combine_iterator::unisono (bool silent, int newpart)
220 Status newstate = (silent) ? UNISILENCE : UNISONO;
222 if ((newstate == state_) and (newpart == chosen_part_))
226 Outlet_type c1 = (newpart == 2) ? CONTEXT_NULL : CONTEXT_SHARED;
227 Outlet_type c2 = (newpart == 2) ? CONTEXT_SHARED : CONTEXT_NULL;
228 substitute_both (c1, c2);
229 kill_mmrest ((newpart == 2) ? CONTEXT_ONE : CONTEXT_TWO);
230 kill_mmrest (CONTEXT_SHARED);
232 if (playing_state_ != PLAYING_UNISONO
233 && newstate == UNISONO)
235 playing_state_ = PLAYING_UNISONO;
238 chosen_part_ = newpart;
243 Part_combine_iterator::solo1 ()
245 if ((state_ == SOLO) && (chosen_part_ == 1))
251 substitute_both (CONTEXT_SOLO, CONTEXT_NULL);
253 kill_mmrest (CONTEXT_TWO);
254 kill_mmrest (CONTEXT_SHARED);
256 playing_state_ = PLAYING_SOLO1;
261 Part_combine_iterator::solo2 ()
263 if ((state_ == SOLO) and (chosen_part_ == 2))
269 substitute_both (CONTEXT_NULL, CONTEXT_SOLO);
271 playing_state_ = PLAYING_SOLO2;
276 Part_combine_iterator::chords_together ()
278 if (state_ == TOGETHER)
282 playing_state_ = PLAYING_OTHER;
285 substitute_both (CONTEXT_SHARED, CONTEXT_SHARED);
290 Part_combine_iterator::apart (bool silent)
293 playing_state_ = PLAYING_OTHER;
300 substitute_both (CONTEXT_ONE, CONTEXT_TWO);
305 Part_combine_iterator::construct_children ()
307 start_moment_ = get_outlet ()->now_mom ();
308 split_list_ = get_music ()->get_property ("split-list");
310 Context *c = get_outlet ();
312 for (int i = 0; i < NUM_OUTLETS; i++)
314 SCM type = (i == CONTEXT_NULL) ? ly_symbol2scm ("Devnull") : ly_symbol2scm ("Voice");
315 /* find context below c: otherwise we may create new staff for each voice */
316 c = c->find_create_context (type, outlet_names_[i], SCM_EOL);
317 handles_[i].set_context (c);
318 if (c->is_alias (ly_symbol2scm ("Voice")))
319 c->event_source ()->add_listener (GET_LISTENER (Part_combine_iterator, set_busy), ly_symbol2scm ("music-event"));
322 SCM lst = get_music ()->get_property ("elements");
323 Context *one = handles_[CONTEXT_ONE].get_context ();
325 first_iter_ = Music_iterator::unsmob (get_iterator (Music::unsmob (scm_car (lst))));
326 Context *two = handles_[CONTEXT_TWO].get_context ();
328 second_iter_ = Music_iterator::unsmob (get_iterator (Music::unsmob (scm_cadr (lst))));
329 Context *shared = handles_[CONTEXT_SHARED].get_context ();
330 set_context (shared);
334 Part_combine_iterator::set_busy (SCM se)
339 Stream_event *e = Stream_event::unsmob (se);
341 if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
346 Processes a moment in an iterator, and returns whether any new music
350 Part_combine_iterator::try_process (Music_iterator *i, Moment m)
357 notice_busy_ = false;
362 Part_combine_iterator::process (Moment m)
364 Moment now = get_outlet ()->now_mom ();
367 /* This is needed if construct_children was called before iteration
369 if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
372 for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
374 splitm = Moment::unsmob (scm_caar (split_list_));
375 if (splitm && *splitm + start_moment_ > now)
378 SCM tag = scm_cdar (split_list_);
380 if (scm_is_eq (tag, ly_symbol2scm ("chords")))
382 else if (scm_is_eq (tag, ly_symbol2scm ("apart"))
383 || scm_is_eq (tag, ly_symbol2scm ("apart-silence"))
384 || scm_is_eq (tag, ly_symbol2scm ("apart-spanner")))
385 apart (scm_is_eq (tag, ly_symbol2scm ("apart-silence")));
386 else if (scm_is_eq (tag, ly_symbol2scm ("unisono")))
388 // Continue to use the most recently used part because we might have
389 // killed mmrests in the other part.
390 unisono (false, (last_playing_ == 2) ? 2 : 1);
392 else if (scm_is_eq (tag, ly_symbol2scm ("unisilence")))
395 unisono (true, (last_playing_ == 2) ? 2 : 1);
397 else if (scm_is_eq (tag, ly_symbol2scm ("silence1")))
399 else if (scm_is_eq (tag, ly_symbol2scm ("silence2")))
401 else if (scm_is_eq (tag, ly_symbol2scm ("solo1")))
403 else if (scm_is_eq (tag, ly_symbol2scm ("solo2")))
405 else if (scm_is_symbol (tag))
407 string s = "Unknown split directive: "
408 + (scm_is_symbol (tag) ? ly_symbol2string (tag) : string ("not a symbol"));
409 programming_error (s);
413 if (first_iter_->ok ())
415 if (try_process (first_iter_, m))
419 if (second_iter_->ok ())
421 if (try_process (second_iter_, m))
426 IMPLEMENT_CTOR_CALLBACK (Part_combine_iterator);