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