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