]> git.donarmstrong.com Git - lilypond.git/blob - lily/part-combine-iterator.cc
Merge branch 'issue4357' into HEAD
[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   void set_busy (SCM);
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
69   Stream_event *mmrest_event_;
70
71   enum Status
72   {
73     APART,
74     TOGETHER,
75     SOLO,
76     UNISONO,
77     UNISILENCE,
78   };
79   Status state_;
80
81   // For states in which it matters, this is the relevant part,
82   // e.g. 1 for Solo I, 2 for Solo II.
83   int chosen_part_;
84
85   // States for generating partcombine text.
86   enum PlayingState
87   {
88     PLAYING_OTHER,
89     PLAYING_UNISONO,
90     PLAYING_SOLO1,
91     PLAYING_SOLO2,
92   } playing_state_;
93
94   int 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, int newpart);
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 (Part_combine_iterator, 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
135   first_iter_ = 0;
136   second_iter_ = 0;
137   split_list_ = SCM_EOL;
138   state_ = APART;
139   chosen_part_ = 1;
140   playing_state_ = PLAYING_OTHER;
141   last_playing_ = 0;
142
143   busy_ = false;
144   notice_busy_ = false;
145 }
146
147 void
148 Part_combine_iterator::derived_mark () const
149 {
150   if (first_iter_)
151     scm_gc_mark (first_iter_->self_scm ());
152   if (second_iter_)
153     scm_gc_mark (second_iter_->self_scm ());
154   if (mmrest_event_)
155     scm_gc_mark (mmrest_event_->self_scm ());
156 }
157
158 void
159 Part_combine_iterator::derived_substitute (Context *f,
160                                            Context *t)
161 {
162   if (first_iter_)
163     first_iter_->substitute_outlet (f, t);
164 }
165
166 Moment
167 Part_combine_iterator::pending_moment () const
168 {
169   Moment p;
170   p.set_infinite (1);
171   if (first_iter_->ok ())
172     p = min (p, first_iter_->pending_moment ());
173
174   if (second_iter_->ok ())
175     p = min (p, second_iter_->pending_moment ());
176   return p;
177 }
178
179 bool
180 Part_combine_iterator::ok () const
181 {
182   return first_iter_->ok () || second_iter_->ok ();
183 }
184
185 void
186 Part_combine_iterator::substitute_both (Outlet_type to1,
187                                         Outlet_type to2)
188 {
189   Outlet_type tos[] = {to1, to2};
190
191   Music_iterator *mis[] = {first_iter_, second_iter_};
192
193   for (int i = 0; i < 2; i++)
194     {
195       for (int j = 0; j < NUM_OUTLETS; j++)
196         if (j != tos[i])
197           mis[i]->substitute_outlet (handles_[j].get_context (), handles_[tos[i]].get_context ());
198     }
199
200   for (int j = 0; j < NUM_OUTLETS; j++)
201     {
202       if (j != to1 && j != to2)
203         kill_mmrest (j);
204     }
205 }
206
207 void
208 Part_combine_iterator::kill_mmrest (int in)
209 {
210
211   if (!mmrest_event_)
212     {
213       mmrest_event_ = new Stream_event
214         (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"),
215                      ly_symbol2scm ("multi-measure-rest-event")));
216       mmrest_event_->set_property ("duration", SCM_EOL);
217       mmrest_event_->unprotect ();
218     }
219
220   handles_[in].get_context ()->event_source ()->broadcast (mmrest_event_);
221 }
222
223 void
224 Part_combine_iterator::unisono (bool silent, int newpart)
225 {
226   Status newstate = (silent) ? UNISILENCE : UNISONO;
227
228   if ((newstate == state_) and (newpart == chosen_part_))
229     return;
230   else
231     {
232       Outlet_type c1 = (newpart == 2) ? CONTEXT_NULL : CONTEXT_SHARED;
233       Outlet_type c2 = (newpart == 2) ? CONTEXT_SHARED : CONTEXT_NULL;
234       substitute_both (c1, c2);
235       kill_mmrest ((newpart == 2) ? CONTEXT_ONE : CONTEXT_TWO);
236       kill_mmrest (CONTEXT_SHARED);
237
238       if (playing_state_ != PLAYING_UNISONO
239           && newstate == UNISONO)
240         {
241           playing_state_ = PLAYING_UNISONO;
242         }
243       state_ = newstate;
244       chosen_part_ = newpart;
245     }
246 }
247
248 void
249 Part_combine_iterator::solo1 ()
250 {
251   if ((state_ == SOLO) && (chosen_part_ == 1))
252     return;
253   else
254     {
255       state_ = SOLO;
256       chosen_part_ = 1;
257       substitute_both (CONTEXT_SOLO, CONTEXT_NULL);
258
259       kill_mmrest (CONTEXT_TWO);
260       kill_mmrest (CONTEXT_SHARED);
261
262       playing_state_ = PLAYING_SOLO1;
263     }
264 }
265
266 void
267 Part_combine_iterator::solo2 ()
268 {
269   if ((state_ == SOLO) and (chosen_part_ == 2))
270     return;
271   else
272     {
273       state_ = SOLO;
274       chosen_part_ = 2;
275       substitute_both (CONTEXT_NULL, CONTEXT_SOLO);
276
277       playing_state_ = PLAYING_SOLO2;
278     }
279 }
280
281 void
282 Part_combine_iterator::chords_together ()
283 {
284   if (state_ == TOGETHER)
285     return;
286   else
287     {
288       playing_state_ = PLAYING_OTHER;
289       state_ = TOGETHER;
290
291       substitute_both (CONTEXT_SHARED, CONTEXT_SHARED);
292     }
293 }
294
295 void
296 Part_combine_iterator::apart (bool silent)
297 {
298   if (!silent)
299     playing_state_ = PLAYING_OTHER;
300
301   if (state_ == APART)
302     return;
303   else
304     {
305       state_ = APART;
306       substitute_both (CONTEXT_ONE, CONTEXT_TWO);
307     }
308 }
309
310 void
311 Part_combine_iterator::construct_children ()
312 {
313   start_moment_ = get_outlet ()->now_mom ();
314   split_list_ = get_music ()->get_property ("split-list");
315
316   Context *c = get_outlet ();
317
318   for (int i = 0; i < NUM_OUTLETS; i++)
319     {
320       SCM type = (i == CONTEXT_NULL) ? ly_symbol2scm ("Devnull") : ly_symbol2scm ("Voice");
321       /* find context below c: otherwise we may create new staff for each voice */
322       c = c->find_create_context (type, outlet_names_[i], SCM_EOL);
323       handles_[i].set_context (c);
324       if (c->is_alias (ly_symbol2scm ("Voice")))
325         c->event_source ()->add_listener (GET_LISTENER (Part_combine_iterator, set_busy), ly_symbol2scm ("music-event"));
326     }
327
328   SCM lst = get_music ()->get_property ("elements");
329   Context *one = handles_[CONTEXT_ONE].get_context ();
330   set_context (one);
331   first_iter_ = Music_iterator::unsmob (get_iterator (Music::unsmob (scm_car (lst))));
332   Context *two = handles_[CONTEXT_TWO].get_context ();
333   set_context (two);
334   second_iter_ = Music_iterator::unsmob (get_iterator (Music::unsmob (scm_cadr (lst))));
335   Context *shared = handles_[CONTEXT_SHARED].get_context ();
336   set_context (shared);
337 }
338
339 void
340 Part_combine_iterator::set_busy (SCM se)
341 {
342   if (!notice_busy_)
343     return;
344
345   Stream_event *e = Stream_event::unsmob (se);
346
347   if (e->in_event_class ("note-event") || e->in_event_class ("cluster-note-event"))
348     busy_ = true;
349 }
350
351 /*
352   Processes a moment in an iterator, and returns whether any new music
353   was reported.
354 */
355 bool
356 Part_combine_iterator::try_process (Music_iterator *i, Moment m)
357 {
358   busy_ = false;
359   notice_busy_ = true;
360
361   i->process (m);
362
363   notice_busy_ = false;
364   return busy_;
365 }
366
367 void
368 Part_combine_iterator::process (Moment m)
369 {
370   Moment now = get_outlet ()->now_mom ();
371   Moment *splitm = 0;
372
373   /* This is needed if construct_children was called before iteration
374      started */
375   if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
376     start_moment_ = now;
377
378   for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
379     {
380       splitm = Moment::unsmob (scm_caar (split_list_));
381       if (splitm && *splitm + start_moment_ > now)
382         break;
383
384       SCM tag = scm_cdar (split_list_);
385
386       if (scm_is_eq (tag, ly_symbol2scm ("chords")))
387         chords_together ();
388       else if (scm_is_eq (tag, ly_symbol2scm ("apart"))
389                || scm_is_eq (tag, ly_symbol2scm ("apart-silence"))
390                || scm_is_eq (tag, ly_symbol2scm ("apart-spanner")))
391         apart (scm_is_eq (tag, ly_symbol2scm ("apart-silence")));
392       else if (scm_is_eq (tag, ly_symbol2scm ("unisono")))
393         {
394           // Continue to use the most recently used part because we might have
395           // killed mmrests in the other part.
396           unisono (false, (last_playing_ == 2) ? 2 : 1);
397         }
398       else if (scm_is_eq (tag, ly_symbol2scm ("unisilence")))
399         {
400           // as for unisono
401           unisono (true, (last_playing_ == 2) ? 2 : 1);
402         }
403       else if (scm_is_eq (tag, ly_symbol2scm ("silence1")))
404         unisono (true, 1);
405       else if (scm_is_eq (tag, ly_symbol2scm ("silence2")))
406         unisono (true, 2);
407       else if (scm_is_eq (tag, ly_symbol2scm ("solo1")))
408         solo1 ();
409       else if (scm_is_eq (tag, ly_symbol2scm ("solo2")))
410         solo2 ();
411       else if (scm_is_symbol (tag))
412         {
413           string s = "Unknown split directive: "
414                      + (scm_is_symbol (tag) ? ly_symbol2string (tag) : string ("not a symbol"));
415           programming_error (s);
416         }
417     }
418
419   if (first_iter_->ok ())
420     {
421       if (try_process (first_iter_, m))
422         last_playing_ = 1;
423     }
424
425   if (second_iter_->ok ())
426     {
427       if (try_process (second_iter_, m))
428         last_playing_ = 2;
429     }
430 }
431
432 IMPLEMENT_CTOR_CALLBACK (Part_combine_iterator);