]> git.donarmstrong.com Git - lilypond.git/blob - lily/part-combine-iterator.cc
Remove CR LF from snippets using makelsr
[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   static const size_t NUM_PARTS = 2;
57   Music_iterator *iterators_[NUM_PARTS];
58   Moment start_moment_;
59
60   SCM split_list_;
61
62   Stream_event *mmrest_event_;
63
64   enum Status
65   {
66     APART,
67     TOGETHER,
68     SOLO,
69     UNISONO,
70     UNISILENCE,
71   };
72   Status state_;
73
74   // For states in which it matters, this is the relevant part,
75   // e.g. 1 for Solo I, 2 for Solo II.
76   int chosen_part_;
77
78   /*
79     TODO: this is getting off hand...
80   */
81   Context_handle handles_[NUM_OUTLETS];
82
83   void substitute_both (Outlet_type to1,
84                         Outlet_type to2);
85   bool is_active_outlet (const Context *c) const;
86   void kill_mmrest_in_inactive_outlets ();
87   /* parameter is really Outlet_type */
88   void kill_mmrest (int in);
89   void chords_together ();
90   void solo1 ();
91   void solo2 ();
92   void apart ();
93   void unisono (bool silent, int newpart);
94 };
95
96 const size_t Part_combine_iterator::NUM_PARTS;
97
98 void
99 Part_combine_iterator::do_quit ()
100 {
101   for (size_t i = 0; i < NUM_PARTS; i++)
102     if (iterators_[i])
103       iterators_[i]->quit ();
104 }
105
106 Part_combine_iterator::Part_combine_iterator ()
107 {
108   mmrest_event_ = 0;
109
110   for (size_t i = 0; i < NUM_PARTS; i++)
111     iterators_[i] = 0;
112   split_list_ = SCM_EOL;
113   state_ = APART;
114   chosen_part_ = 1;
115 }
116
117 void
118 Part_combine_iterator::derived_mark () const
119 {
120   for (size_t i = 0; i < NUM_PARTS; i++)
121     if (iterators_[i])
122       scm_gc_mark (iterators_[i]->self_scm ());
123
124   if (mmrest_event_)
125     scm_gc_mark (mmrest_event_->self_scm ());
126 }
127
128 void
129 Part_combine_iterator::derived_substitute (Context *f,
130                                            Context *t)
131 {
132   // (Explain why just iterators_[0].)
133   if (iterators_[0])
134     iterators_[0]->substitute_outlet (f, t);
135 }
136
137 Moment
138 Part_combine_iterator::pending_moment () const
139 {
140   Moment p;
141   p.set_infinite (1);
142
143   for (size_t i = 0; i < NUM_PARTS; i++)
144     if (iterators_[i]->ok ())
145       p = min (p, iterators_[i]->pending_moment ());
146
147   return p;
148 }
149
150 bool
151 Part_combine_iterator::ok () const
152 {
153   for (size_t i = 0; i < NUM_PARTS; i++)
154     if (iterators_[i]->ok ())
155       return true;
156
157   return false;
158 }
159
160 void
161 Part_combine_iterator::substitute_both (Outlet_type to1,
162                                         Outlet_type to2)
163 {
164   // TODO: There is no good reason to tie the parts together here.
165   // Factor out per-part stuff into a new class of iterator which
166   // reads a part-specific list similar to the existing combined
167   // "split-list".
168   iterators_[0]->substitute_outlet (iterators_[0]->get_outlet (),
169                                     handles_[to1].get_context ());
170   iterators_[1]->substitute_outlet (iterators_[1]->get_outlet (),
171                                     handles_[to2].get_context ());
172 }
173
174 bool Part_combine_iterator::is_active_outlet (const Context *c) const
175 {
176   for (size_t i = 0; i < NUM_PARTS; i++)
177     if (iterators_[i] && (iterators_[i]->get_outlet () == c))
178       return true;
179
180   return false;
181 }
182
183 void Part_combine_iterator::kill_mmrest_in_inactive_outlets ()
184 {
185   for (int j = 0; j < NUM_OUTLETS; j++)
186     if (!is_active_outlet (handles_[j].get_context ()))
187       kill_mmrest (j);
188 }
189
190 void
191 Part_combine_iterator::kill_mmrest (int in)
192 {
193
194   if (!mmrest_event_)
195     {
196       mmrest_event_ = new Stream_event
197         (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"),
198                      ly_symbol2scm ("multi-measure-rest-event")));
199       mmrest_event_->set_property ("duration", SCM_EOL);
200       mmrest_event_->unprotect ();
201     }
202
203   handles_[in].get_context ()->event_source ()->broadcast (mmrest_event_);
204 }
205
206 void
207 Part_combine_iterator::unisono (bool silent, int newpart)
208 {
209   Status newstate = (silent) ? UNISILENCE : UNISONO;
210
211   if ((newstate == state_) and (newpart == chosen_part_))
212     return;
213   else
214     {
215       Outlet_type c1 = (newpart == 2) ? CONTEXT_NULL : CONTEXT_SHARED;
216       Outlet_type c2 = (newpart == 2) ? CONTEXT_SHARED : CONTEXT_NULL;
217       substitute_both (c1, c2);
218
219       state_ = newstate;
220       chosen_part_ = newpart;
221     }
222 }
223
224 void
225 Part_combine_iterator::solo1 ()
226 {
227   if ((state_ == SOLO) && (chosen_part_ == 1))
228     return;
229   else
230     {
231       state_ = SOLO;
232       chosen_part_ = 1;
233       substitute_both (CONTEXT_SOLO, CONTEXT_NULL);
234     }
235 }
236
237 void
238 Part_combine_iterator::solo2 ()
239 {
240   if ((state_ == SOLO) and (chosen_part_ == 2))
241     return;
242   else
243     {
244       state_ = SOLO;
245       chosen_part_ = 2;
246       substitute_both (CONTEXT_NULL, CONTEXT_SOLO);
247     }
248 }
249
250 void
251 Part_combine_iterator::chords_together ()
252 {
253   if (state_ == TOGETHER)
254     return;
255   else
256     {
257       state_ = TOGETHER;
258
259       substitute_both (CONTEXT_SHARED, CONTEXT_SHARED);
260     }
261 }
262
263 void
264 Part_combine_iterator::apart ()
265 {
266   if (state_ == APART)
267     return;
268   else
269     {
270       state_ = APART;
271       substitute_both (CONTEXT_ONE, CONTEXT_TWO);
272     }
273 }
274
275 void
276 Part_combine_iterator::construct_children ()
277 {
278   start_moment_ = get_outlet ()->now_mom ();
279   split_list_ = get_music ()->get_property ("split-list");
280
281   Context *c = get_outlet ();
282
283   for (int i = 0; i < NUM_OUTLETS; i++)
284     {
285       SCM type = (i == CONTEXT_NULL) ? ly_symbol2scm ("Devnull") : ly_symbol2scm ("Voice");
286       /* find context below c: otherwise we may create new staff for each voice */
287       c = c->find_create_context (type, outlet_names_[i], SCM_EOL);
288       handles_[i].set_context (c);
289     }
290
291   SCM lst = get_music ()->get_property ("elements");
292
293   Context *one = handles_[CONTEXT_ONE].get_context ();
294   set_context (one);
295   iterators_[0] = unsmob<Music_iterator> (get_iterator (unsmob<Music> (scm_car (lst))));
296
297   Context *two = handles_[CONTEXT_TWO].get_context ();
298   set_context (two);
299   iterators_[1] = unsmob<Music_iterator> (get_iterator (unsmob<Music> (scm_cadr (lst))));
300
301   // (Explain the purpose of switching to the shared context.)
302   Context *shared = handles_[CONTEXT_SHARED].get_context ();
303   set_context (shared);
304 }
305
306 void
307 Part_combine_iterator::process (Moment m)
308 {
309   Moment now = get_outlet ()->now_mom ();
310   Moment *splitm = 0;
311
312   /* This is needed if construct_children was called before iteration
313      started */
314   if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
315     start_moment_ = now;
316
317   for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
318     {
319       splitm = unsmob<Moment> (scm_caar (split_list_));
320       if (splitm && *splitm + start_moment_ > now)
321         break;
322
323       SCM tag = scm_cdar (split_list_);
324
325       Context *outletsBefore[NUM_PARTS];
326       for (size_t i = 0; i < NUM_PARTS; i++)
327         outletsBefore[i] = iterators_[i]->get_outlet ();
328
329       if (scm_is_eq (tag, ly_symbol2scm ("chords")))
330         chords_together ();
331       else if (scm_is_eq (tag, ly_symbol2scm ("apart"))
332                || scm_is_eq (tag, ly_symbol2scm ("apart-silence"))
333                || scm_is_eq (tag, ly_symbol2scm ("apart-spanner")))
334         apart ();
335       else if (scm_is_eq (tag, ly_symbol2scm ("unisono")))
336         {
337           // Continue to use the most recently used part because we might have
338           // killed mmrests in the other part.
339           unisono (false, (chosen_part_ == 2) ? 2 : 1);
340         }
341       else if (scm_is_eq (tag, ly_symbol2scm ("unisilence")))
342         {
343           // as for unisono
344           unisono (true, (chosen_part_ == 2) ? 2 : 1);
345         }
346       else if (scm_is_eq (tag, ly_symbol2scm ("silence1")))
347         unisono (true, 1);
348       else if (scm_is_eq (tag, ly_symbol2scm ("silence2")))
349         unisono (true, 2);
350       else if (scm_is_eq (tag, ly_symbol2scm ("solo1")))
351         solo1 ();
352       else if (scm_is_eq (tag, ly_symbol2scm ("solo2")))
353         solo2 ();
354       else if (scm_is_symbol (tag))
355         {
356           string s = "Unknown split directive: "
357                      + (scm_is_symbol (tag) ? ly_symbol2string (tag) : string ("not a symbol"));
358           programming_error (s);
359         }
360
361       for (size_t i = 0; i < NUM_PARTS; i++)
362         if (iterators_[i]->get_outlet () != outletsBefore[i])
363           {
364             kill_mmrest_in_inactive_outlets ();
365             break;
366           }
367     }
368
369   for (size_t i = 0; i < NUM_PARTS; i++)
370     if (iterators_[i]->ok ())
371       iterators_[i]->process (m);
372 }
373
374 IMPLEMENT_CTOR_CALLBACK (Part_combine_iterator);