2 new-part-combine-music-iterator.cc -- implement Part_combine_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2006 Han-Wen Nienhuys
10 #include "dispatcher.hh"
11 #include "lily-guile.hh"
12 #include "listener.hh"
14 #include "music-iterator.hh"
15 #include "music-sequence.hh"
18 typedef enum Outlet_type
20 CONTEXT_ONE, CONTEXT_TWO, CONTEXT_SHARED, CONTEXT_SOLO, CONTEXT_NULL, NUM_OUTLETS
23 static const char *outlet_names_[NUM_OUTLETS] =
24 {"one", "two", "shared", "solo", "null"};
26 class Part_combine_iterator : public Music_iterator
29 Part_combine_iterator ();
31 DECLARE_SCHEME_CALLBACK (constructor, ());
33 virtual void derived_substitute (Context *f, Context *t);
34 virtual void derived_mark () const;
35 Part_combine_iterator (Part_combine_iterator const &);
37 virtual void construct_children ();
38 virtual Moment pending_moment () const;
39 virtual void do_quit ();
40 virtual void process (Moment);
42 virtual bool ok () const;
45 /* used by try_process */
46 DECLARE_LISTENER (set_busy);
50 bool try_process (Music_iterator *i, Moment m);
52 Music_iterator *first_iter_;
53 Music_iterator *second_iter_;
68 Status playing_state_;
71 Should be SOLO1 or SOLO2
76 TODO: this is getting of hand...
78 Context_handle handles_[NUM_OUTLETS];
80 void substitute_both (Outlet_type to1,
83 /* parameter is really Outlet_type */
84 void kill_mmrest (int in);
85 void chords_together ();
88 void apart (bool silent);
89 void unisono (bool silent);
93 Part_combine_iterator::do_quit ()
98 second_iter_->quit ();
100 // Add listeners to all contexts except Devnull.
101 for (int i = 0; i < NUM_OUTLETS; i++)
103 Context *c = handles_[i].get_outlet ();
104 if (c->is_alias (ly_symbol2scm ("Voice")))
105 c->event_source ()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
106 handles_[i].set_context (0);
110 Part_combine_iterator::Part_combine_iterator ()
114 split_list_ = SCM_EOL;
116 playing_state_ = APART;
120 Part_combine_iterator::derived_mark () const
123 scm_gc_mark (first_iter_->self_scm ());
125 scm_gc_mark (second_iter_->self_scm ());
129 Part_combine_iterator::derived_substitute (Context *f,
133 first_iter_->substitute_outlet (f, t);
137 Part_combine_iterator::pending_moment () const
141 if (first_iter_->ok ())
142 p = min (p, first_iter_->pending_moment ());
144 if (second_iter_->ok ())
145 p = min (p, second_iter_->pending_moment ());
150 Part_combine_iterator::ok () const
152 return first_iter_->ok () || second_iter_->ok ();
156 Part_combine_iterator::chords_together ()
158 if (state_ == TOGETHER)
162 playing_state_ = TOGETHER;
165 substitute_both (CONTEXT_SHARED, CONTEXT_SHARED);
170 Part_combine_iterator::kill_mmrest (int in)
172 static Stream_event *mmrest;
175 mmrest = new Stream_event (ly_symbol2scm ("multi-measure-rest-event"));
176 mmrest->set_property ("duration", SCM_EOL);
179 handles_[in].get_outlet ()->event_source ()->broadcast (mmrest);
183 Part_combine_iterator::solo1 ()
190 substitute_both (CONTEXT_SOLO, CONTEXT_NULL);
192 kill_mmrest (CONTEXT_TWO);
193 kill_mmrest (CONTEXT_SHARED);
195 if (playing_state_ != SOLO1)
197 static Stream_event *event;
199 event = new Stream_event (ly_symbol2scm ("solo1-event"));
201 first_iter_->get_outlet ()->event_source ()->broadcast (event);
203 playing_state_ = SOLO1;
208 Part_combine_iterator::substitute_both (Outlet_type to1,
211 Outlet_type tos[] = {to1, to2};
213 Music_iterator *mis[] = {first_iter_, second_iter_};
215 for (int i = 0; i < 2; i++)
217 for (int j = 0; j < NUM_OUTLETS; j++)
219 mis[i]->substitute_outlet (handles_[j].get_outlet (), handles_[tos[i]].get_outlet ());
222 for (int j = 0; j < NUM_OUTLETS; j++)
224 if (j != to1 && j != to2)
230 Part_combine_iterator::unisono (bool silent)
232 Status newstate = (silent) ? UNISILENCE : UNISONO;
234 if (newstate == state_)
239 If we're coming from SOLO2 state, we might have kill mmrests
240 in the 1st voice, so in that case, we use the second voice
241 as a basis for events.
243 Outlet_type c1 = (last_playing_ == SOLO2) ? CONTEXT_NULL : CONTEXT_SHARED;
244 Outlet_type c2 = (last_playing_ == SOLO2) ? CONTEXT_SHARED : CONTEXT_NULL;
245 substitute_both (c1, c2);
246 kill_mmrest ((last_playing_ == SOLO2)
247 ? CONTEXT_ONE : CONTEXT_TWO);
248 kill_mmrest (CONTEXT_SHARED);
250 if (playing_state_ != UNISONO
251 && newstate == UNISONO)
253 static Stream_event *event;
255 event = new Stream_event (ly_symbol2scm ("unisono-event"));
258 Context *out = (last_playing_ == SOLO2 ? second_iter_ : first_iter_)
260 out->event_source ()->broadcast (event);
261 playing_state_ = UNISONO;
268 Part_combine_iterator::solo2 ()
276 substitute_both (CONTEXT_NULL, CONTEXT_SOLO);
278 if (playing_state_ != SOLO2)
280 static Stream_event *event;
282 event = new Stream_event (ly_symbol2scm ("solo2-event"));
284 second_iter_->get_outlet ()->event_source ()->broadcast (event);
285 playing_state_ = SOLO2;
291 Part_combine_iterator::apart (bool silent)
294 playing_state_ = APART;
301 substitute_both (CONTEXT_ONE, CONTEXT_TWO);
306 Part_combine_iterator::construct_children ()
308 start_moment_ = get_outlet ()->now_mom ();
309 split_list_ = get_music ()->get_property ("split-list");
311 Context *c = get_outlet ();
313 for (int i = 0; i < NUM_OUTLETS; i++)
315 SCM type = (i == CONTEXT_NULL) ? ly_symbol2scm ("Devnull") : ly_symbol2scm ("Voice");
316 /* find context below c: otherwise we may create new staff for each voice */
317 c = c->find_create_context (type, outlet_names_[i], SCM_EOL);
318 handles_[i].set_context (c);
319 if (c->is_alias (ly_symbol2scm ("Voice")))
320 c->event_source ()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
323 SCM lst = get_music ()->get_property ("elements");
324 Context *one = handles_[CONTEXT_ONE].get_outlet ();
326 first_iter_ = unsmob_iterator (get_iterator (unsmob_music (scm_car (lst))));
327 Context *two = handles_[CONTEXT_TWO].get_outlet ();
329 second_iter_ = unsmob_iterator (get_iterator (unsmob_music (scm_cadr (lst))));
334 "DynamicLineSpanner",
344 for (char const **p = syms; *p; p++)
346 SCM sym = ly_symbol2scm (*p);
347 execute_pushpop_property (one, sym,
348 ly_symbol2scm ("direction"), scm_from_int (1));
350 execute_pushpop_property (two, sym,
351 ly_symbol2scm ("direction"), scm_from_int (-1));
355 IMPLEMENT_LISTENER (Part_combine_iterator, set_busy);
357 Part_combine_iterator::set_busy (SCM se)
362 Stream_event *e = unsmob_stream_event (se);
364 if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
369 * Processes a moment in an iterator, and returns whether any new music was reported.
372 Part_combine_iterator::try_process (Music_iterator *i, Moment m)
379 notice_busy_ = false;
384 Part_combine_iterator::process (Moment m)
386 Moment now = get_outlet ()->now_mom ();
389 /* This is needed if construct_children was called before iteration
391 if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
394 for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
396 splitm = unsmob_moment (scm_caar (split_list_));
397 if (splitm && *splitm + start_moment_ > now)
400 SCM tag = scm_cdar (split_list_);
402 if (tag == ly_symbol2scm ("chords"))
404 else if (tag == ly_symbol2scm ("apart")
405 || tag == ly_symbol2scm ("apart-silence")
406 || tag == ly_symbol2scm ("apart-spanner"))
407 apart (tag == ly_symbol2scm ("apart-silence"));
408 else if (tag == ly_symbol2scm ("unisono"))
410 else if (tag == ly_symbol2scm ("unisilence"))
412 else if (tag == ly_symbol2scm ("solo1"))
414 else if (tag == ly_symbol2scm ("solo2"))
416 else if (scm_is_symbol (tag))
418 string s = "Unknown split directive: "
419 + (scm_is_symbol (tag) ? ly_symbol2string (tag) : string ("not a symbol"));
420 programming_error (s);
424 if (first_iter_->ok ())
426 if (try_process (first_iter_, m))
427 last_playing_ = SOLO1;
430 if (second_iter_->ok ())
432 if (try_process (second_iter_, m))
433 last_playing_ = SOLO2;
437 IMPLEMENT_CTOR_CALLBACK (Part_combine_iterator);