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