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,
21 CONTEXT_SHARED, CONTEXT_SOLO,
22 CONTEXT_NULL, NUM_OUTLETS
25 static const char *outlet_names_[NUM_OUTLETS] =
26 {"one", "two", "shared", "solo", "null"};
28 class Part_combine_iterator : public Music_iterator
31 Part_combine_iterator ();
33 DECLARE_SCHEME_CALLBACK (constructor, ());
35 virtual void derived_substitute (Context *f, Context *t);
36 virtual void derived_mark () const;
37 Part_combine_iterator (Part_combine_iterator const &);
39 virtual void construct_children ();
40 virtual Moment pending_moment () const;
41 virtual void do_quit ();
42 virtual void process (Moment);
44 virtual bool ok () const;
47 /* used by try_process */
48 DECLARE_LISTENER (set_busy);
52 bool try_process (Music_iterator *i, Moment m);
54 Music_iterator *first_iter_;
55 Music_iterator *second_iter_;
70 Status playing_state_;
73 Should be SOLO1 or SOLO2
78 TODO: this is getting of hand...
80 Context_handle handles_[NUM_OUTLETS];
82 void substitute_both (Outlet_type to1,
85 /* parameter is really Outlet_type */
86 void kill_mmrest (int in);
87 void chords_together ();
90 void apart (bool silent);
91 void unisono (bool silent);
95 Part_combine_iterator::do_quit ()
100 second_iter_->quit ();
102 // Add listeners to all contexts except Devnull.
103 for (int i = 0; i < NUM_OUTLETS; i++)
105 Context *c = handles_[i].get_outlet ();
106 if (c->is_alias (ly_symbol2scm ("Voice")))
107 c->event_source ()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
108 handles_[i].set_context (0);
112 Part_combine_iterator::Part_combine_iterator ()
116 split_list_ = SCM_EOL;
118 playing_state_ = APART;
122 Part_combine_iterator::derived_mark () const
125 scm_gc_mark (first_iter_->self_scm ());
127 scm_gc_mark (second_iter_->self_scm ());
131 Part_combine_iterator::derived_substitute (Context *f,
135 first_iter_->substitute_outlet (f, t);
139 Part_combine_iterator::pending_moment () const
143 if (first_iter_->ok ())
144 p = min (p, first_iter_->pending_moment ());
146 if (second_iter_->ok ())
147 p = min (p, second_iter_->pending_moment ());
152 Part_combine_iterator::ok () const
154 return first_iter_->ok () || second_iter_->ok ();
158 Part_combine_iterator::chords_together ()
160 if (state_ == TOGETHER)
164 playing_state_ = TOGETHER;
167 substitute_both (CONTEXT_SHARED, CONTEXT_SHARED);
172 Part_combine_iterator::kill_mmrest (int in)
174 static Stream_event *mmrest;
177 mmrest = new Stream_event (ly_symbol2scm ("multi-measure-rest-event"));
178 mmrest->set_property ("duration", SCM_EOL);
181 handles_[in].get_outlet ()->event_source ()->broadcast (mmrest);
185 Part_combine_iterator::solo1 ()
192 substitute_both (CONTEXT_SOLO, CONTEXT_NULL);
194 kill_mmrest (CONTEXT_TWO);
195 kill_mmrest (CONTEXT_SHARED);
197 if (playing_state_ != SOLO1)
199 static Stream_event *event;
201 event = new Stream_event (ly_symbol2scm ("solo-one-event"));
203 first_iter_->get_outlet ()->event_source ()->broadcast (event);
205 playing_state_ = SOLO1;
210 Part_combine_iterator::substitute_both (Outlet_type to1,
213 Outlet_type tos[] = {to1, to2};
215 Music_iterator *mis[] = {first_iter_, second_iter_};
217 for (int i = 0; i < 2; i++)
219 for (int j = 0; j < NUM_OUTLETS; j++)
221 mis[i]->substitute_outlet (handles_[j].get_outlet (), handles_[tos[i]].get_outlet ());
224 for (int j = 0; j < NUM_OUTLETS; j++)
226 if (j != to1 && j != to2)
232 Part_combine_iterator::unisono (bool silent)
234 Status newstate = (silent) ? UNISILENCE : UNISONO;
236 if (newstate == state_)
241 If we're coming from SOLO2 state, we might have kill mmrests
242 in the 1st voice, so in that case, we use the second voice
243 as a basis for events.
245 Outlet_type c1 = (last_playing_ == SOLO2) ? CONTEXT_NULL : CONTEXT_SHARED;
246 Outlet_type c2 = (last_playing_ == SOLO2) ? CONTEXT_SHARED : CONTEXT_NULL;
247 substitute_both (c1, c2);
248 kill_mmrest ((last_playing_ == SOLO2)
249 ? CONTEXT_ONE : CONTEXT_TWO);
250 kill_mmrest (CONTEXT_SHARED);
252 if (playing_state_ != UNISONO
253 && newstate == UNISONO)
255 static Stream_event *event;
257 event = new Stream_event (ly_symbol2scm ("unisono-event"));
260 Context *out = (last_playing_ == SOLO2 ? second_iter_ : first_iter_)
262 out->event_source ()->broadcast (event);
263 playing_state_ = UNISONO;
270 Part_combine_iterator::solo2 ()
278 substitute_both (CONTEXT_NULL, CONTEXT_SOLO);
280 if (playing_state_ != SOLO2)
282 static Stream_event *event;
284 event = new Stream_event (ly_symbol2scm ("solo-two-event"));
286 second_iter_->get_outlet ()->event_source ()->broadcast (event);
287 playing_state_ = SOLO2;
293 Part_combine_iterator::apart (bool silent)
296 playing_state_ = APART;
303 substitute_both (CONTEXT_ONE, CONTEXT_TWO);
308 Part_combine_iterator::construct_children ()
310 start_moment_ = get_outlet ()->now_mom ();
311 split_list_ = get_music ()->get_property ("split-list");
313 Context *c = get_outlet ();
315 for (int i = 0; i < NUM_OUTLETS; i++)
317 SCM type = (i == CONTEXT_NULL) ? ly_symbol2scm ("Devnull") : ly_symbol2scm ("Voice");
318 /* find context below c: otherwise we may create new staff for each voice */
319 c = c->find_create_context (type, outlet_names_[i], SCM_EOL);
320 handles_[i].set_context (c);
321 if (c->is_alias (ly_symbol2scm ("Voice")))
322 c->event_source ()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
325 SCM lst = get_music ()->get_property ("elements");
326 Context *one = handles_[CONTEXT_ONE].get_outlet ();
328 first_iter_ = unsmob_iterator (get_iterator (unsmob_music (scm_car (lst))));
329 Context *two = handles_[CONTEXT_TWO].get_outlet ();
331 second_iter_ = unsmob_iterator (get_iterator (unsmob_music (scm_cadr (lst))));
336 "DynamicLineSpanner",
346 for (char const **p = syms; *p; p++)
348 SCM sym = ly_symbol2scm (*p);
349 execute_pushpop_property (one, sym,
350 ly_symbol2scm ("direction"), scm_from_int (1));
352 execute_pushpop_property (two, sym,
353 ly_symbol2scm ("direction"), scm_from_int (-1));
357 IMPLEMENT_LISTENER (Part_combine_iterator, set_busy);
359 Part_combine_iterator::set_busy (SCM se)
364 Stream_event *e = unsmob_stream_event (se);
366 if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
371 * Processes a moment in an iterator, and returns whether any new music was reported.
374 Part_combine_iterator::try_process (Music_iterator *i, Moment m)
381 notice_busy_ = false;
386 Part_combine_iterator::process (Moment m)
388 Moment now = get_outlet ()->now_mom ();
391 /* This is needed if construct_children was called before iteration
393 if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
396 for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
398 splitm = unsmob_moment (scm_caar (split_list_));
399 if (splitm && *splitm + start_moment_ > now)
402 SCM tag = scm_cdar (split_list_);
404 if (tag == ly_symbol2scm ("chords"))
406 else if (tag == ly_symbol2scm ("apart")
407 || tag == ly_symbol2scm ("apart-silence")
408 || tag == ly_symbol2scm ("apart-spanner"))
409 apart (tag == ly_symbol2scm ("apart-silence"));
410 else if (tag == ly_symbol2scm ("unisono"))
412 else if (tag == ly_symbol2scm ("unisilence"))
414 else if (tag == ly_symbol2scm ("solo1"))
416 else if (tag == ly_symbol2scm ("solo2"))
418 else if (scm_is_symbol (tag))
420 string s = "Unknown split directive: "
421 + (scm_is_symbol (tag) ? ly_symbol2string (tag) : string ("not a symbol"));
422 programming_error (s);
426 if (first_iter_->ok ())
428 if (try_process (first_iter_, m))
429 last_playing_ = SOLO1;
432 if (second_iter_->ok ())
434 if (try_process (second_iter_, m))
435 last_playing_ = SOLO2;
439 IMPLEMENT_CTOR_CALLBACK (Part_combine_iterator);