]> git.donarmstrong.com Git - lilypond.git/blob - lily/part-combine-music-iterator.cc
31959d48d53e800492808057e49ea4308fe86848
[lilypond.git] / lily / part-combine-music-iterator.cc
1 /*   
2   part-combine-music-iterator.cc -- implement  Part_combine_music_iterator
3
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7  */
8
9 #include "part-combine-music.hh"
10 #include "part-combine-music-iterator.hh"
11 #include "translator-group.hh"
12 #include "request.hh"
13 #include "music-sequence.hh"
14 #include "lily-guile.hh"
15 #include "warn.hh"
16
17 Part_combine_music_iterator::Part_combine_music_iterator ()
18 {
19   first_iter_ = 0;
20   second_iter_ = 0;
21   first_until_ = 0;
22   second_until_ = 0;
23
24   state_ = 0;
25 }
26
27 void
28 Part_combine_music_iterator::derived_mark () const
29 {
30   if (first_iter_)
31     scm_gc_mark (first_iter_->self_scm());
32   if (second_iter_)
33     scm_gc_mark(second_iter_->self_scm());
34 }
35
36 void
37 Part_combine_music_iterator::do_quit ()
38 {
39   if (first_iter_)
40     first_iter_->quit();
41   if (second_iter_)
42     second_iter_->quit();
43 }
44
45 Part_combine_music_iterator::Part_combine_music_iterator (Part_combine_music_iterator const &src)
46   : Music_iterator (src)
47 {
48   first_iter_ = 0;
49   second_iter_ = 0;
50
51   if(src.first_iter_)
52     first_iter_ = src.first_iter_->clone ();
53   if (src.second_iter_)
54     second_iter_ = src.second_iter_->clone ();
55
56   first_until_ = src.first_until_;
57   second_until_ = src.second_until_;
58   state_ = src.state_;
59   suffix_ = src.suffix_;
60
61   if (first_iter_)
62     scm_gc_unprotect_object (first_iter_->self_scm());
63   if (second_iter_)
64     scm_gc_unprotect_object (second_iter_->self_scm());
65 }
66
67 Moment
68 Part_combine_music_iterator::pending_moment () const
69 {
70   Moment p;
71   p.set_infinite (1);
72   if (first_iter_->ok ())
73     p = p <? first_iter_->pending_moment ();
74
75   if (second_iter_->ok ())
76     p = p <? second_iter_->pending_moment ();
77   return p;
78 }
79
80 bool
81 Part_combine_music_iterator::ok () const
82 {
83   return first_iter_->ok () || second_iter_->ok ();
84 }
85
86 void
87 Part_combine_music_iterator::construct_children ()
88 {
89   Part_combine_music const * m = dynamic_cast<Part_combine_music const*> (get_music ());
90   
91   first_iter_ = unsmob_iterator (get_iterator (m->get_first ()));
92   second_iter_ = unsmob_iterator (get_iterator (m->get_second ()));
93 }
94
95 void
96 Part_combine_music_iterator::change_to (Music_iterator *it, String to_type,
97                                         String to_id)
98 {
99   Translator_group * current = it->report_to ();
100   Translator_group * last = 0;
101
102   /*
103     Cut & Paste from from Auto_change_iterator from Change_iterator (ugh).
104
105     TODO: abstract this function 
106    */
107   
108   /* find the type  of translator that we're changing.
109      
110      If \translator Staff = bass, then look for Staff = *
111    */
112   while (current && current->type_string_ != to_type)
113     {
114       last = current;
115       current = current->daddy_trans_;
116     }
117
118   if (current && current->id_string_ == to_id)
119     {
120       String msg;
121       msg += _ ("Can't switch translators, I'm there already");
122     }
123   
124   if (current) 
125     if (last)
126       {
127         Translator_group * dest = 
128           it->report_to ()->find_create_translator (to_type, to_id);
129         current->remove_translator (last);
130         dest->add_used_group_translator (last);
131       }
132     else
133       {
134         /*
135           We could change the current translator's id, but that would make 
136           errors hard to catch
137           
138            last->translator_id_string_  = get_change ()->change_to_id_string_;
139         */
140         error (_f ("I'm one myself: `%s'", to_type.to_str0 ()));
141       }
142   else
143     error (_f ("none of these in my family: `%s'", to_id.to_str0 ()));
144 }
145
146
147 // SCM*, moet / kan dat niet met set_x ofzo?
148 static void
149 get_music_info (Moment m, Music_iterator* iter, SCM *pitches, SCM *durations)
150 {
151   if (iter->ok ())
152     {
153       for (SCM i = iter->get_pending_events (m); gh_pair_p (i); i = ly_cdr (i))
154         {
155           Music *m = unsmob_music (ly_car (i));
156           if (m->is_mus_type ("melodic-event"))
157             *pitches = gh_cons (m->get_mus_property ("pitch"), *pitches);
158           if (m->is_mus_type ("rhythmic-event"))
159             {
160               SCM d = m->get_mus_property ("duration");
161               if (d == SCM_EOL)
162                 m->origin ()->warning ("Music has no duration\n");
163               else
164                 *durations = gh_cons (d, *durations);
165             }
166         }
167     }
168 }
169   
170 int
171 Part_combine_music_iterator::get_state (Moment)
172 {
173   int state = UNKNOWN;
174   Part_combine_music const *p = dynamic_cast<Part_combine_music const* > (get_music ());
175
176   String w = ly_scm2string (p->get_mus_property ("what"));
177     
178   
179   Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
180
181   SCM s = first_translator->get_property ("changeMoment");
182   if (!gh_pair_p (s))
183     return state;
184
185   Moment change_mom = *unsmob_moment (ly_car (s));
186   Moment diff_mom = *unsmob_moment (ly_cdr (s));
187   
188   Moment now = pending_moment ();
189
190   if (!now.main_part_.mod_rat (change_mom.main_part_))
191     {
192       SCM interval = SCM_BOOL_F;
193       if (first_until_ < now)
194         first_until_ = now;
195       if (second_until_ < now)
196         second_until_ = now;
197
198       Moment first_mom = first_until_;
199       Moment second_mom = second_until_;
200       Moment diff_until = diff_mom + now;
201
202
203       bool first = true;
204       Music_iterator *first_iter = first_iter_->clone ();
205       Music_iterator *second_iter = second_iter_->clone ();
206       
207       Moment last_pending (-1);
208       Moment pending = now;
209       while (now < diff_until
210               && (first_iter->ok () || second_iter->ok ())
211
212              // urg, this is a hack, haven't caught this case yet
213              && (pending != last_pending))
214         {
215           if (!second_iter->ok ())
216             pending = first_iter->pending_moment ();
217           else if (!first_iter->ok ())
218             pending = second_iter->pending_moment ();
219           else
220             pending = first_iter->pending_moment () <? second_iter->pending_moment ();
221           last_pending = pending;
222
223           SCM first_pitches = SCM_EOL;
224           SCM first_durations = SCM_EOL;
225           get_music_info (pending, first_iter,
226                           &first_pitches, &first_durations);
227       
228           SCM second_pitches = SCM_EOL;
229           SCM second_durations = SCM_EOL;
230           get_music_info (pending, second_iter,
231                           &second_pitches, &second_durations);
232
233           if (first_pitches != SCM_EOL && second_pitches != SCM_EOL)
234             {
235               scm_sort_list_x (first_pitches,
236                                scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
237               scm_sort_list_x (second_pitches,
238                                scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
239
240               interval = gh_int2scm (unsmob_pitch (ly_car (first_pitches))->steps ()
241                                      - unsmob_pitch (ly_car (scm_last_pair (second_pitches)))->steps ());
242             }
243
244           if (first_durations != SCM_EOL)
245             {
246               scm_sort_list_x (first_durations,
247                                scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
248               first_mom += unsmob_duration (ly_car (first_durations))->length_mom ();
249             }
250           
251           if (second_durations != SCM_EOL)
252             {
253               scm_sort_list_x (second_durations,
254                                scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
255               second_mom += unsmob_duration (ly_car (second_durations))->length_mom ();
256             }
257           
258           if (first_pitches != SCM_EOL && second_pitches == SCM_EOL
259                   && ! (second_until_ > now))
260             {
261               state |= UNRELATED;
262               state &= ~UNISILENCE;
263               if (! (state & ~ (UNRELATED | SOLO1 | UNISILENCE)))
264                 state |= SOLO1;
265             }
266           else
267             state &= ~SOLO1;
268
269           if (first_pitches == SCM_EOL && second_pitches != SCM_EOL
270               && ! (first_until_ > now))
271             {
272               state |= UNRELATED;
273               state &= ~UNISILENCE;
274               if (! (state & ~ (UNRELATED | SOLO2 | UNISILENCE)))
275                 state |= SOLO2;
276             }
277           else
278             state &= ~SOLO2;
279
280           if (gh_equal_p (first_durations, second_durations))
281             {
282               state &= ~UNISILENCE;
283               if (! (state & ~ (UNIRHYTHM | UNISON)))
284                 state |= UNIRHYTHM;
285             }
286           else
287             state &= ~ (UNIRHYTHM | UNISILENCE);
288           
289           if (first_pitches != SCM_EOL
290               && gh_equal_p (first_pitches, second_pitches))
291             {
292               state &= ~UNISILENCE;
293               if (! (state & ~ (UNIRHYTHM | UNISON)))
294                 state |= UNISON;
295             }
296           else
297             state &= ~ (UNISON);
298             
299           if (first_pitches == SCM_EOL && second_pitches == SCM_EOL)
300             {
301               if (! (state & ~ (UNIRHYTHM | UNISILENCE)))
302                 state |= UNISILENCE;
303             }
304           else if (!state)
305             state |= UNRELATED;
306           else
307             state &= ~ (UNISILENCE);
308
309           if (gh_number_p (interval))
310             {
311               SCM s = first_translator->get_property ("splitInterval");
312               int i = gh_scm2int (interval);
313               if (gh_pair_p (s)
314                   && gh_number_p (ly_car (s))
315                   && gh_number_p (ly_cdr (s))
316                   && i >= gh_scm2int (ly_car (s))
317                   && i <= gh_scm2int (ly_cdr (s)))
318                 {
319                   if (! (state & ~ (SPLIT_INTERVAL | UNIRHYTHM | UNISON)))
320                     state |= SPLIT_INTERVAL;
321                 }
322               else
323                 state &= ~ (SPLIT_INTERVAL);
324             }
325
326           if (first && first_pitches != SCM_EOL)
327             first_until_ = first_mom;
328           if (first && second_pitches != SCM_EOL)
329             second_until_ = second_mom;
330           first = false;
331
332           if (first_iter->ok ())
333             first_iter->skip (pending);
334           if (second_iter->ok ())
335             second_iter->skip (pending);
336           now = pending;
337         }
338       scm_gc_unprotect_object (first_iter->self_scm ());
339       scm_gc_unprotect_object (second_iter->self_scm ());
340     }
341
342   return state;
343 }
344
345 static Music* abort_req = NULL;
346
347 void
348 Part_combine_music_iterator::process (Moment m)
349 {
350
351   /*
352     TODO:
353     - Use three named contexts (be it Thread or Voice): one, two, solo.
354       Let user pre-set (pushproperty) stem direction, remove
355       dynamic-engraver, and such.
356
357       **** Tried this, but won't work:
358
359 s      Consider thread switching: threads "one", "two" and "both".
360       User can't pre-set the (most important) stem direction at
361       thread level!
362    */
363  
364   if (suffix_.empty_b ())
365     suffix_ = first_iter_->report_to ()
366       ->daddy_trans_->id_string_.cut_string (3, INT_MAX);
367
368   int state = get_state (m);
369   if (state)
370     state_ = state;
371   else
372     state = state_;
373   
374   Part_combine_music const *p =
375     dynamic_cast<Part_combine_music const* > (get_music ());
376
377
378   bool previously_combined_b = first_iter_->report_to ()->daddy_trans_
379     == second_iter_->report_to ()->daddy_trans_;
380
381   bool combine_b = previously_combined_b;
382
383   if (! (state & UNIRHYTHM)
384       || (state & SPLIT_INTERVAL)
385       || (state & (SOLO1 | SOLO2)))
386     combine_b = false;
387   else if (state & (UNIRHYTHM | UNISILENCE))
388     combine_b = true;
389
390   /*
391     When combining, abort all running spanners
392    */
393
394   if (!abort_req)
395     {
396       abort_req = make_music_by_name (ly_symbol2scm ("AbortEvent"));
397     }
398   
399   if (combine_b && combine_b != previously_combined_b)
400     {
401       if (second_iter_ && second_iter_->ok ())
402         second_iter_->try_music (abort_req);
403      }
404   String w = ly_scm2string (p->get_mus_property ("what"));
405   if (combine_b != previously_combined_b)
406     change_to (second_iter_, w, (combine_b ? "one" : "two")
407                + suffix_);
408   
409   Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
410   Translator_group *second_translator = second_iter_->report_to ()->find_create_translator (w, "two" + suffix_);
411   
412
413   /* Hmm */
414   first_translator->set_property ("combineParts", SCM_BOOL_T);
415   second_translator ->set_property ("combineParts", SCM_BOOL_T);
416  
417  
418   /* hmm */
419   SCM b = (state & UNIRHYTHM) ? SCM_BOOL_T : SCM_BOOL_F;
420   first_translator->set_property ("unirhythm", b);
421   second_translator->set_property ("unirhythm", b);
422
423   b = (state & SPLIT_INTERVAL) ? SCM_BOOL_T : SCM_BOOL_F;
424   first_translator->set_property ("split-interval", b);
425   second_translator->set_property ("split-interval",  b);
426
427   b = (state & UNISILENCE) ? SCM_BOOL_T : SCM_BOOL_F;
428   first_translator->set_property ("unisilence", b);
429   second_translator->set_property ("unisilence", b);
430
431   // difference in definition...
432   //b = ((state & UNISON) ? SCM_BOOL_T : SCM_BOOL_F;
433   b = ((state & UNISON) && (state & UNIRHYTHM)) ? SCM_BOOL_T : SCM_BOOL_F;
434   first_translator->set_property ("unison", b);
435   second_translator->set_property ("unison", b);
436
437   SCM b1 = (state & SOLO1) ? SCM_BOOL_T : SCM_BOOL_F;
438   SCM b2 = (state & SOLO2) ? SCM_BOOL_T : SCM_BOOL_F;
439   first_translator->set_property ("solo", b1);
440   second_translator->set_property ("solo", b2);
441
442   /* Can't these be computed? */
443   first_translator->set_property ("othersolo", b2);
444   second_translator->set_property ("othersolo", b1);
445
446   if (first_iter_->ok ())
447     first_iter_->process (m);
448   
449   if (second_iter_->ok ())
450     second_iter_->process (m);
451 }
452
453 Music_iterator*
454 Part_combine_music_iterator::try_music_in_children (Music *m) const
455 {
456   Music_iterator * i =  first_iter_->try_music (m);
457   if (i)
458     return i;
459   else
460     return second_iter_->try_music (m);
461 }
462
463
464 SCM
465 Part_combine_music_iterator::get_pending_events (Moment m)const
466 {
467   SCM s = SCM_EOL;
468   if (first_iter_)
469     s = gh_append2 (s,first_iter_->get_pending_events (m));
470   if (second_iter_)
471     s = gh_append2 (second_iter_->get_pending_events (m),s);
472   return s;
473 }
474
475 IMPLEMENT_CTOR_CALLBACK (Part_combine_music_iterator);