]> git.donarmstrong.com Git - lilypond.git/blob - lily/part-combine-iterator.cc
Merge remote branch 'origin/master' into release/unstable
[lilypond.git] / lily / part-combine-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2015 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     SOLO,
84     UNISONO,
85     UNISILENCE,
86   };
87   Status state_;
88
89   // For states in which it matters, this is the relevant part,
90   // e.g. 1 for Solo I, 2 for Solo II.
91   int chosen_part_;
92
93   // States for generating partcombine text.
94   enum PlayingState
95   {
96     PLAYING_OTHER,
97     PLAYING_UNISONO,
98     PLAYING_SOLO1,
99     PLAYING_SOLO2,
100   } playing_state_;
101
102   int last_playing_;
103
104   /*
105     TODO: this is getting off hand...
106   */
107   Context_handle handles_[NUM_OUTLETS];
108
109   void substitute_both (Outlet_type to1,
110                         Outlet_type to2);
111
112   /* parameter is really Outlet_type */
113   void kill_mmrest (int in);
114   void chords_together ();
115   void solo1 ();
116   void solo2 ();
117   void apart (bool silent);
118   void unisono (bool silent, int newpart);
119 };
120
121 void
122 Part_combine_iterator::do_quit ()
123 {
124   if (first_iter_)
125     first_iter_->quit ();
126   if (second_iter_)
127     second_iter_->quit ();
128
129   // Add listeners to all contexts except Devnull.
130   for (int i = 0; i < NUM_OUTLETS; i++)
131     {
132       Context *c = handles_[i].get_context ();
133       if (c->is_alias (ly_symbol2scm ("Voice")))
134         c->event_source ()->remove_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
135       handles_[i].set_context (0);
136     }
137 }
138
139 Part_combine_iterator::Part_combine_iterator ()
140 {
141   mmrest_event_ = 0;
142   unisono_event_ = 0;
143   solo_two_event_ = 0;
144   solo_one_event_ = 0;
145
146   first_iter_ = 0;
147   second_iter_ = 0;
148   split_list_ = SCM_EOL;
149   direction_ = SCM_BOOL_F;
150   directionOne_ = scm_from_int (1);
151   directionTwo_ = scm_from_int (-1);
152   horizontalShiftOne_ = scm_from_int (0);
153   horizontalShiftTwo_ = scm_from_int (1);
154   state_ = APART;
155   chosen_part_ = 1;
156   playing_state_ = PLAYING_OTHER;
157   last_playing_ = 0;
158
159   busy_ = false;
160   notice_busy_ = false;
161 }
162
163 void
164 Part_combine_iterator::derived_mark () const
165 {
166   if (first_iter_)
167     scm_gc_mark (first_iter_->self_scm ());
168   if (second_iter_)
169     scm_gc_mark (second_iter_->self_scm ());
170   if (unisono_event_)
171     scm_gc_mark (unisono_event_->self_scm ());
172   if (mmrest_event_)
173     scm_gc_mark (mmrest_event_->self_scm ());
174   if (solo_one_event_)
175     scm_gc_mark (solo_one_event_->self_scm ());
176   if (solo_two_event_)
177     scm_gc_mark (solo_two_event_->self_scm ());
178 }
179
180 void
181 Part_combine_iterator::derived_substitute (Context *f,
182                                            Context *t)
183 {
184   if (first_iter_)
185     first_iter_->substitute_outlet (f, t);
186 }
187
188 Moment
189 Part_combine_iterator::pending_moment () const
190 {
191   Moment p;
192   p.set_infinite (1);
193   if (first_iter_->ok ())
194     p = min (p, first_iter_->pending_moment ());
195
196   if (second_iter_->ok ())
197     p = min (p, second_iter_->pending_moment ());
198   return p;
199 }
200
201 bool
202 Part_combine_iterator::ok () const
203 {
204   return first_iter_->ok () || second_iter_->ok ();
205 }
206
207 void
208 Part_combine_iterator::substitute_both (Outlet_type to1,
209                                         Outlet_type to2)
210 {
211   Outlet_type tos[] = {to1, to2};
212
213   Music_iterator *mis[] = {first_iter_, second_iter_};
214
215   for (int i = 0; i < 2; i++)
216     {
217       for (int j = 0; j < NUM_OUTLETS; j++)
218         if (j != tos[i])
219           mis[i]->substitute_outlet (handles_[j].get_context (), handles_[tos[i]].get_context ());
220     }
221
222   for (int j = 0; j < NUM_OUTLETS; j++)
223     {
224       if (j != to1 && j != to2)
225         kill_mmrest (j);
226     }
227 }
228
229 void
230 Part_combine_iterator::kill_mmrest (int in)
231 {
232
233   if (!mmrest_event_)
234     {
235       mmrest_event_ = new Stream_event
236         (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"),
237                      ly_symbol2scm ("multi-measure-rest-event")));
238       mmrest_event_->set_property ("duration", SCM_EOL);
239       mmrest_event_->unprotect ();
240     }
241
242   handles_[in].get_context ()->event_source ()->broadcast (mmrest_event_);
243 }
244
245 void
246 Part_combine_iterator::unisono (bool silent, int newpart)
247 {
248   Status newstate = (silent) ? UNISILENCE : UNISONO;
249
250   if ((newstate == state_) and (newpart == chosen_part_))
251     return;
252   else
253     {
254       Outlet_type c1 = (newpart == 2) ? CONTEXT_NULL : CONTEXT_SHARED;
255       Outlet_type c2 = (newpart == 2) ? CONTEXT_SHARED : CONTEXT_NULL;
256       substitute_both (c1, c2);
257       kill_mmrest ((newpart == 2) ? CONTEXT_ONE : CONTEXT_TWO);
258       kill_mmrest (CONTEXT_SHARED);
259
260       if (playing_state_ != PLAYING_UNISONO
261           && newstate == UNISONO)
262         {
263           if (!unisono_event_)
264             {
265               unisono_event_ = new Stream_event
266                 (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"),
267                              ly_symbol2scm ("unisono-event")));
268               unisono_event_->unprotect ();
269             }
270
271           Context *out = (newpart == 2 ? second_iter_ : first_iter_)
272                          ->get_outlet ();
273           out->event_source ()->broadcast (unisono_event_);
274           playing_state_ = PLAYING_UNISONO;
275         }
276       state_ = newstate;
277       chosen_part_ = newpart;
278     }
279 }
280
281 void
282 Part_combine_iterator::solo1 ()
283 {
284   if ((state_ == SOLO) && (chosen_part_ == 1))
285     return;
286   else
287     {
288       state_ = SOLO;
289       chosen_part_ = 1;
290       substitute_both (CONTEXT_SOLO, CONTEXT_NULL);
291
292       kill_mmrest (CONTEXT_TWO);
293       kill_mmrest (CONTEXT_SHARED);
294
295       if (playing_state_ != PLAYING_SOLO1)
296         {
297           if (!solo_one_event_)
298             {
299               solo_one_event_ = new Stream_event
300                 (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"),
301                              ly_symbol2scm ("solo-one-event")));
302               solo_one_event_->unprotect ();
303             }
304
305           first_iter_->get_outlet ()->event_source ()->broadcast (solo_one_event_);
306         }
307       playing_state_ = PLAYING_SOLO1;
308     }
309 }
310
311 void
312 Part_combine_iterator::solo2 ()
313 {
314   if ((state_ == SOLO) and (chosen_part_ == 2))
315     return;
316   else
317     {
318       state_ = SOLO;
319       chosen_part_ = 2;
320       substitute_both (CONTEXT_NULL, CONTEXT_SOLO);
321
322       if (playing_state_ != PLAYING_SOLO2)
323         {
324           if (!solo_two_event_)
325             {
326               solo_two_event_ = new Stream_event
327                 (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"),
328                              ly_symbol2scm ("solo-two-event")));
329               solo_two_event_->unprotect ();
330             }
331
332           second_iter_->get_outlet ()->event_source ()->broadcast (solo_two_event_);
333           playing_state_ = PLAYING_SOLO2;
334         }
335     }
336 }
337
338 void
339 Part_combine_iterator::chords_together ()
340 {
341   if (state_ == TOGETHER)
342     return;
343   else
344     {
345       playing_state_ = PLAYING_OTHER;
346       state_ = TOGETHER;
347
348       substitute_both (CONTEXT_SHARED, CONTEXT_SHARED);
349     }
350 }
351
352 void
353 Part_combine_iterator::apart (bool silent)
354 {
355   if (!silent)
356     playing_state_ = PLAYING_OTHER;
357
358   if (state_ == APART)
359     return;
360   else
361     {
362       state_ = APART;
363       substitute_both (CONTEXT_ONE, CONTEXT_TWO);
364     }
365 }
366
367 void
368 Part_combine_iterator::construct_children ()
369 {
370   start_moment_ = get_outlet ()->now_mom ();
371   split_list_ = get_music ()->get_property ("split-list");
372   direction_ = get_music ()->get_property ("direction");
373   if (is_direction (direction_))
374     {
375       directionOne_ = direction_;
376       directionTwo_ = direction_;
377       if (scm_is_true (scm_negative_p (direction_)))
378         {
379           horizontalShiftOne_ = scm_from_int (1);
380           horizontalShiftTwo_ = scm_from_int (0);
381         }
382     }
383
384   Context *c = get_outlet ();
385
386   for (int i = 0; i < NUM_OUTLETS; i++)
387     {
388       SCM type = (i == CONTEXT_NULL) ? ly_symbol2scm ("Devnull") : ly_symbol2scm ("Voice");
389       /* find context below c: otherwise we may create new staff for each voice */
390       c = c->find_create_context (type, outlet_names_[i], SCM_EOL);
391       handles_[i].set_context (c);
392       if (c->is_alias (ly_symbol2scm ("Voice")))
393         c->event_source ()->add_listener (GET_LISTENER (set_busy), ly_symbol2scm ("music-event"));
394     }
395
396   SCM lst = get_music ()->get_property ("elements");
397   Context *one = handles_[CONTEXT_ONE].get_context ();
398   set_context (one);
399   first_iter_ = Music_iterator::unsmob (get_iterator (Music::unsmob (scm_car (lst))));
400   Context *two = handles_[CONTEXT_TWO].get_context ();
401   set_context (two);
402   second_iter_ = Music_iterator::unsmob (get_iterator (Music::unsmob (scm_cadr (lst))));
403   Context *shared = handles_[CONTEXT_SHARED].get_context ();
404   set_context (shared);
405
406   /* Mimic all settings of voiceOne/voiceTwo for the two separate voices...*/
407   /* FIXME: Is there any way to use the definition of \voiceOne/\voiceTwo
408             directly??? */
409   char const *syms[]
410   =
411   {
412     "Stem",
413     "DynamicLineSpanner",
414     "Tie",
415     "Dots",
416     "MultiMeasureRest",
417     "Rest",
418     "Slur",
419     "TextScript",
420     "Script",
421     0
422   };
423
424   for (char const **p = syms; *p; p++)
425     {
426       SCM sym = ly_symbol2scm (*p);
427       execute_pushpop_property (one, sym,
428                                 ly_symbol2scm ("direction"), directionOne_);
429
430       execute_pushpop_property (two, sym,
431                                 ly_symbol2scm ("direction"), directionTwo_);
432
433       if (scm_is_number (direction_))
434         execute_pushpop_property (shared, sym,
435                                   ly_symbol2scm ("direction"), direction_);
436     }
437   /* Handle horizontal shifts for crossing notes */
438   execute_pushpop_property (one, ly_symbol2scm ("NoteColumn"),
439                             ly_symbol2scm ("horizontal-shift"), horizontalShiftOne_);
440   execute_pushpop_property (two, ly_symbol2scm ("NoteColumn"),
441                             ly_symbol2scm ("horizontal-shift"), horizontalShiftTwo_);
442
443 }
444
445 IMPLEMENT_LISTENER (Part_combine_iterator, set_busy);
446 void
447 Part_combine_iterator::set_busy (SCM se)
448 {
449   if (!notice_busy_)
450     return;
451
452   Stream_event *e = Stream_event::unsmob (se);
453
454   if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
455     busy_ = true;
456 }
457
458 /*
459   Processes a moment in an iterator, and returns whether any new music
460   was reported.
461 */
462 bool
463 Part_combine_iterator::try_process (Music_iterator *i, Moment m)
464 {
465   busy_ = false;
466   notice_busy_ = true;
467
468   i->process (m);
469
470   notice_busy_ = false;
471   return busy_;
472 }
473
474 void
475 Part_combine_iterator::process (Moment m)
476 {
477   Moment now = get_outlet ()->now_mom ();
478   Moment *splitm = 0;
479
480   /* This is needed if construct_children was called before iteration
481      started */
482   if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
483     start_moment_ = now;
484
485   for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
486     {
487       splitm = Moment::unsmob (scm_caar (split_list_));
488       if (splitm && *splitm + start_moment_ > now)
489         break;
490
491       SCM tag = scm_cdar (split_list_);
492
493       if (tag == ly_symbol2scm ("chords"))
494         chords_together ();
495       else if (tag == ly_symbol2scm ("apart")
496                || tag == ly_symbol2scm ("apart-silence")
497                || tag == ly_symbol2scm ("apart-spanner"))
498         apart (tag == ly_symbol2scm ("apart-silence"));
499       else if (tag == ly_symbol2scm ("unisono"))
500         {
501           // Continue to use the most recently used part because we might have
502           // killed mmrests in the other part.
503           unisono (false, (last_playing_ == 2) ? 2 : 1);
504         }
505       else if (tag == ly_symbol2scm ("unisilence"))
506         {
507           // as for unisono
508           unisono (true, (last_playing_ == 2) ? 2 : 1);
509         }
510       else if (tag == ly_symbol2scm ("silence1"))
511         unisono (true, 1);
512       else if (tag == ly_symbol2scm ("silence2"))
513         unisono (true, 2);
514       else if (tag == ly_symbol2scm ("solo1"))
515         solo1 ();
516       else if (tag == ly_symbol2scm ("solo2"))
517         solo2 ();
518       else if (scm_is_symbol (tag))
519         {
520           string s = "Unknown split directive: "
521                      + (scm_is_symbol (tag) ? ly_symbol2string (tag) : string ("not a symbol"));
522           programming_error (s);
523         }
524     }
525
526   if (first_iter_->ok ())
527     {
528       if (try_process (first_iter_, m))
529         last_playing_ = 1;
530     }
531
532   if (second_iter_->ok ())
533     {
534       if (try_process (second_iter_, m))
535         last_playing_ = 2;
536     }
537 }
538
539 IMPLEMENT_CTOR_CALLBACK (Part_combine_iterator);