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