]> git.donarmstrong.com Git - lilypond.git/blob - lily/part-combine-iterator.cc
a8471b41582be855a98f86e1ceb3e6e11da542ee
[lilypond.git] / lily / part-combine-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2011 Han-Wen Nienhuys
5
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.
10
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.
15
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/>.
18 */
19
20 #include "context.hh"
21 #include "dispatcher.hh"
22 #include "lily-guile.hh"
23 #include "music.hh"
24 #include "music-iterator.hh"
25 #include "music-sequence.hh"
26 #include "warn.hh"
27
28 enum Outlet_type
29 {
30   CONTEXT_ONE, CONTEXT_TWO,
31   CONTEXT_SHARED, CONTEXT_SOLO,
32   CONTEXT_NULL, NUM_OUTLETS
33 };
34
35 static const char *outlet_names_[NUM_OUTLETS]
36   = {"one", "two", "shared", "solo", "null"};
37
38 class Part_combine_iterator : public Music_iterator
39 {
40 public:
41   Part_combine_iterator ();
42
43   DECLARE_SCHEME_CALLBACK (constructor, ());
44 protected:
45   virtual void derived_substitute (Context *f, Context *t);
46   virtual void derived_mark () const;
47
48   virtual void construct_children ();
49   virtual Moment pending_moment () const;
50   virtual void do_quit ();
51   virtual void process (Moment);
52
53   virtual bool ok () const;
54
55 private:
56   /* used by try_process */
57   DECLARE_LISTENER (set_busy);
58   bool busy_;
59   bool notice_busy_;
60
61   bool try_process (Music_iterator *i, Moment m);
62
63   Music_iterator *first_iter_;
64   Music_iterator *second_iter_;
65   Moment start_moment_;
66
67   SCM split_list_;
68   SCM direction_;
69   SCM directionOne_;
70   SCM directionTwo_;
71   SCM horizontalShiftOne_;
72   SCM horizontalShiftTwo_;
73
74   Stream_event *unisono_event_;
75   Stream_event *solo_one_event_;
76   Stream_event *solo_two_event_;
77   Stream_event *mmrest_event_;
78
79   enum Status
80   {
81     APART,
82     TOGETHER,
83     SOLO1,
84     SOLO2,
85     UNISONO,
86     UNISILENCE,
87   };
88   Status state_;
89   Status playing_state_;
90
91   /*
92     Should be SOLO1 or SOLO2
93   */
94   Status last_playing_;
95
96   /*
97     TODO: this is getting off hand...
98   */
99   Context_handle handles_[NUM_OUTLETS];
100
101   void substitute_both (Outlet_type to1,
102                         Outlet_type to2);
103
104   /* parameter is really Outlet_type */
105   void kill_mmrest (int in);
106   void chords_together ();
107   void solo1 ();
108   void solo2 ();
109   void apart (bool silent);
110   void unisono (bool silent);
111 };
112
113 void
114 Part_combine_iterator::do_quit ()
115 {
116   if (first_iter_)
117     first_iter_->quit ();
118   if (second_iter_)
119     second_iter_->quit ();
120
121   // Add listeners to all contexts except Devnull.
122   for (int i = 0; i < NUM_OUTLETS; i++)
123     {
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);
128     }
129 }
130
131 Part_combine_iterator::Part_combine_iterator ()
132 {
133   mmrest_event_ = 0;
134   unisono_event_ = 0;
135   solo_two_event_ = 0;
136   solo_one_event_ = 0;
137
138   first_iter_ = 0;
139   second_iter_ = 0;
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);
146   state_ = APART;
147   playing_state_ = APART;
148   last_playing_ = APART;
149
150   busy_ = false;
151   notice_busy_ = false;
152 }
153
154 void
155 Part_combine_iterator::derived_mark () const
156 {
157   if (first_iter_)
158     scm_gc_mark (first_iter_->self_scm ());
159   if (second_iter_)
160     scm_gc_mark (second_iter_->self_scm ());
161
162   Stream_event *ptrs[]
163   =
164   {
165     unisono_event_,
166     mmrest_event_,
167     solo_two_event_,
168     solo_one_event_,
169     0
170   };
171   for (int i = 0; ptrs[i]; i++)
172     if (ptrs[i])
173       scm_gc_mark (ptrs[i]->self_scm ());
174 }
175
176 void
177 Part_combine_iterator::derived_substitute (Context *f,
178                                            Context *t)
179 {
180   if (first_iter_)
181     first_iter_->substitute_outlet (f, t);
182 }
183
184 Moment
185 Part_combine_iterator::pending_moment () const
186 {
187   Moment p;
188   p.set_infinite (1);
189   if (first_iter_->ok ())
190     p = min (p, first_iter_->pending_moment ());
191
192   if (second_iter_->ok ())
193     p = min (p, second_iter_->pending_moment ());
194   return p;
195 }
196
197 bool
198 Part_combine_iterator::ok () const
199 {
200   return first_iter_->ok () || second_iter_->ok ();
201 }
202
203 void
204 Part_combine_iterator::substitute_both (Outlet_type to1,
205                                         Outlet_type to2)
206 {
207   Outlet_type tos[] = {to1, to2};
208
209   Music_iterator *mis[] = {first_iter_, second_iter_};
210
211   for (int i = 0; i < 2; i++)
212     {
213       for (int j = 0; j < NUM_OUTLETS; j++)
214         if (j != tos[i])
215           mis[i]->substitute_outlet (handles_[j].get_context (), handles_[tos[i]].get_context ());
216     }
217
218   for (int j = 0; j < NUM_OUTLETS; j++)
219     {
220       if (j != to1 && j != to2)
221         kill_mmrest (j);
222     }
223 }
224
225 void
226 Part_combine_iterator::kill_mmrest (int in)
227 {
228
229   if (!mmrest_event_)
230     {
231       mmrest_event_ = new Stream_event (ly_symbol2scm ("multi-measure-rest-event"));
232       mmrest_event_->set_property ("duration", SCM_EOL);
233       mmrest_event_->unprotect ();
234     }
235
236   handles_[in].get_context ()->event_source ()->broadcast (mmrest_event_);
237 }
238
239 void
240 Part_combine_iterator::unisono (bool silent)
241 {
242   Status newstate = (silent) ? UNISILENCE : UNISONO;
243
244   if (newstate == state_)
245     return;
246   else
247     {
248       /*
249         If we're coming from SOLO2 state, we might have kill mmrests
250         in the 1st voice, so in that case, we use the second voice
251         as a basis for events.
252       */
253       Outlet_type c1 = (last_playing_ == SOLO2) ? CONTEXT_NULL : CONTEXT_SHARED;
254       Outlet_type c2 = (last_playing_ == SOLO2) ? CONTEXT_SHARED : CONTEXT_NULL;
255       substitute_both (c1, c2);
256       kill_mmrest ((last_playing_ == SOLO2) ? CONTEXT_ONE : CONTEXT_TWO);
257       kill_mmrest (CONTEXT_SHARED);
258
259       if (playing_state_ != UNISONO
260           && newstate == UNISONO)
261         {
262           if (!unisono_event_)
263             {
264               unisono_event_ = new Stream_event (ly_symbol2scm ("unisono-event"));
265               unisono_event_->unprotect ();
266             }
267
268           Context *out = (last_playing_ == SOLO2 ? second_iter_ : first_iter_)
269                          ->get_outlet ();
270           out->event_source ()->broadcast (unisono_event_);
271           playing_state_ = UNISONO;
272         }
273       state_ = newstate;
274     }
275 }
276
277 void
278 Part_combine_iterator::solo1 ()
279 {
280   if (state_ == SOLO1)
281     return;
282   else
283     {
284       state_ = SOLO1;
285       substitute_both (CONTEXT_SOLO, CONTEXT_NULL);
286
287       kill_mmrest (CONTEXT_TWO);
288       kill_mmrest (CONTEXT_SHARED);
289
290       if (playing_state_ != SOLO1)
291         {
292           if (!solo_one_event_)
293             {
294               solo_one_event_ = new Stream_event (ly_symbol2scm ("solo-one-event"));
295               solo_one_event_->unprotect ();
296             }
297
298           first_iter_->get_outlet ()->event_source ()->broadcast (solo_one_event_);
299         }
300       playing_state_ = SOLO1;
301     }
302 }
303
304 void
305 Part_combine_iterator::solo2 ()
306 {
307   if (state_ == SOLO2)
308     return;
309   else
310     {
311       state_ = SOLO2;
312
313       substitute_both (CONTEXT_NULL, CONTEXT_SOLO);
314
315       if (playing_state_ != SOLO2)
316         {
317           if (!solo_two_event_)
318             {
319               solo_two_event_ = new Stream_event (ly_symbol2scm ("solo-two-event"));
320               solo_two_event_->unprotect ();
321             }
322
323           second_iter_->get_outlet ()->event_source ()->broadcast (solo_two_event_);
324           playing_state_ = SOLO2;
325         }
326     }
327 }
328
329 void
330 Part_combine_iterator::chords_together ()
331 {
332   if (state_ == TOGETHER)
333     return;
334   else
335     {
336       playing_state_ = TOGETHER;
337       state_ = TOGETHER;
338
339       substitute_both (CONTEXT_SHARED, CONTEXT_SHARED);
340     }
341 }
342
343 void
344 Part_combine_iterator::apart (bool silent)
345 {
346   if (!silent)
347     playing_state_ = APART;
348
349   if (state_ == APART)
350     return;
351   else
352     {
353       state_ = APART;
354       substitute_both (CONTEXT_ONE, CONTEXT_TWO);
355     }
356 }
357
358 void
359 Part_combine_iterator::construct_children ()
360 {
361   start_moment_ = get_outlet ()->now_mom ();
362   split_list_ = get_music ()->get_property ("split-list");
363   direction_ = get_music ()->get_property ("direction");
364   if (is_direction (direction_))
365     {
366       directionOne_ = direction_;
367       directionTwo_ = direction_;
368       if (scm_is_true (scm_negative_p (direction_)))
369         {
370           horizontalShiftOne_ = scm_from_int (1);
371           horizontalShiftTwo_ = scm_from_int (0);
372         }
373     }
374
375   Context *c = get_outlet ();
376
377   for (int i = 0; i < NUM_OUTLETS; i++)
378     {
379       SCM type = (i == CONTEXT_NULL) ? ly_symbol2scm ("Devnull") : ly_symbol2scm ("Voice");
380       /* find context below c: otherwise we may create new staff for each voice */
381       c = c->find_create_context (type, outlet_names_[i], SCM_EOL);
382       handles_[i].set_context (c);
383       if (c->is_alias (ly_symbol2scm ("Voice")))
384         c->event_source ()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
385     }
386
387   SCM lst = get_music ()->get_property ("elements");
388   Context *one = handles_[CONTEXT_ONE].get_context ();
389   set_context (one);
390   first_iter_ = unsmob_iterator (get_iterator (unsmob_music (scm_car (lst))));
391   Context *two = handles_[CONTEXT_TWO].get_context ();
392   set_context (two);
393   second_iter_ = unsmob_iterator (get_iterator (unsmob_music (scm_cadr (lst))));
394   Context *shared = handles_[CONTEXT_SHARED].get_context ();
395   set_context (shared);
396
397   /* Mimic all settings of voiceOne/voiceTwo for the two separate voices...*/
398   /* FIXME: Is there any way to use the definition of \voiceOne/\voiceTwo
399             directly??? */
400   char const *syms[]
401   =
402   {
403     "Stem",
404     "DynamicLineSpanner",
405     "Tie",
406     "Dots",
407     "Rest",
408     "Slur",
409     "TextScript",
410     "Script",
411     0
412   };
413
414   for (char const **p = syms; *p; p++)
415     {
416       SCM sym = ly_symbol2scm (*p);
417       execute_pushpop_property (one, sym,
418                                 ly_symbol2scm ("direction"), directionOne_);
419
420       execute_pushpop_property (two, sym,
421                                 ly_symbol2scm ("direction"), directionTwo_);
422
423       if (scm_is_number (direction_))
424         execute_pushpop_property (shared, sym,
425                                   ly_symbol2scm ("direction"), direction_);
426     }
427   /* Handle horizontal shifts for crossing notes */
428   execute_pushpop_property (one, ly_symbol2scm ("NoteColumn"),
429                             ly_symbol2scm ("horizontal-shift"), horizontalShiftOne_);
430   execute_pushpop_property (two, ly_symbol2scm ("NoteColumn"),
431                             ly_symbol2scm ("horizontal-shift"), horizontalShiftTwo_);
432   /* Also handle MultiMeasureRest positions for voice 1/2 */
433   execute_pushpop_property (one, ly_symbol2scm ("MultiMeasureRest"),
434                             ly_symbol2scm ("staff-position"), scm_from_int (4));
435   execute_pushpop_property (two, ly_symbol2scm ("MultiMeasureRest"),
436                             ly_symbol2scm ("staff-position"), scm_from_int (-4));
437
438 }
439
440 IMPLEMENT_LISTENER (Part_combine_iterator, set_busy);
441 void
442 Part_combine_iterator::set_busy (SCM se)
443 {
444   if (!notice_busy_)
445     return;
446
447   Stream_event *e = unsmob_stream_event (se);
448
449   if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
450     busy_ = true;
451 }
452
453 /*
454   Processes a moment in an iterator, and returns whether any new music
455   was reported.
456 */
457 bool
458 Part_combine_iterator::try_process (Music_iterator *i, Moment m)
459 {
460   busy_ = false;
461   notice_busy_ = true;
462
463   i->process (m);
464
465   notice_busy_ = false;
466   return busy_;
467 }
468
469 void
470 Part_combine_iterator::process (Moment m)
471 {
472   Moment now = get_outlet ()->now_mom ();
473   Moment *splitm = 0;
474
475   /* This is needed if construct_children was called before iteration
476      started */
477   if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
478     start_moment_ = now;
479
480   for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
481     {
482       splitm = unsmob_moment (scm_caar (split_list_));
483       if (splitm && *splitm + start_moment_ > now)
484         break;
485
486       SCM tag = scm_cdar (split_list_);
487
488       if (tag == ly_symbol2scm ("chords"))
489         chords_together ();
490       else if (tag == ly_symbol2scm ("apart")
491                || tag == ly_symbol2scm ("apart-silence")
492                || tag == ly_symbol2scm ("apart-spanner"))
493         apart (tag == ly_symbol2scm ("apart-silence"));
494       else if (tag == ly_symbol2scm ("unisono"))
495         unisono (false);
496       else if (tag == ly_symbol2scm ("unisilence"))
497         unisono (true);
498       else if (tag == ly_symbol2scm ("solo1"))
499         solo1 ();
500       else if (tag == ly_symbol2scm ("solo2"))
501         solo2 ();
502       else if (scm_is_symbol (tag))
503         {
504           string s = "Unknown split directive: "
505                      + (scm_is_symbol (tag) ? ly_symbol2string (tag) : string ("not a symbol"));
506           programming_error (s);
507         }
508     }
509
510   if (first_iter_->ok ())
511     {
512       if (try_process (first_iter_, m))
513         last_playing_ = SOLO1;
514     }
515
516   if (second_iter_->ok ())
517     {
518       if (try_process (second_iter_, m))
519         last_playing_ = SOLO2;
520     }
521 }
522
523 IMPLEMENT_CTOR_CALLBACK (Part_combine_iterator);