]> git.donarmstrong.com Git - lilypond.git/blob - lily/part-combine-music-iterator.cc
* VERSION (MY_PATCH_LEVEL): make 1.7.0
[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 "musical-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_ = src.first_iter_ ? src.first_iter_->clone () : 0;
49   second_iter_ = src.second_iter_ ? src.second_iter_->clone () : 0;
50
51   first_until_ = src.first_until_;
52   second_until_ = src.second_until_;
53   state_ = src.state_;
54   suffix_ = src.suffix_;
55
56   if (first_iter_)
57     scm_gc_unprotect_object (first_iter_->self_scm());
58   if (second_iter_)
59     scm_gc_unprotect_object (second_iter_->self_scm());
60 }
61
62 Moment
63 Part_combine_music_iterator::pending_moment () const
64 {
65   Moment p;
66   p.set_infinite (1);
67   if (first_iter_->ok ())
68     p = p <? first_iter_->pending_moment ();
69
70   if (second_iter_->ok ())
71     p = p <? second_iter_->pending_moment ();
72   return p;
73 }
74
75 bool
76 Part_combine_music_iterator::ok () const
77 {
78   return first_iter_->ok () || second_iter_->ok ();
79 }
80
81 void
82 Part_combine_music_iterator::construct_children ()
83 {
84   Part_combine_music const * m = dynamic_cast<Part_combine_music const*> (get_music ());
85   
86   first_iter_ = unsmob_iterator (get_iterator (m->get_first ()));
87   second_iter_ = unsmob_iterator (get_iterator (m->get_second ()));
88 }
89
90 void
91 Part_combine_music_iterator::change_to (Music_iterator *it, String to_type,
92                                         String to_id)
93 {
94   Translator_group * current = it->report_to ();
95   Translator_group * last = 0;
96
97   /*
98     Cut & Paste from from Auto_change_iterator from Change_iterator (ugh).
99
100     TODO: abstract this function 
101    */
102   
103   /* find the type  of translator that we're changing.
104      
105      If \translator Staff = bass, then look for Staff = *
106    */
107   while (current && current->type_string_ != to_type)
108     {
109       last = current;
110       current = current->daddy_trans_;
111     }
112
113   if (current && current->id_string_ == to_id)
114     {
115       String msg;
116       msg += _ ("Can't switch translators, I'm there already");
117     }
118   
119   if (current) 
120     if (last)
121       {
122         Translator_group * dest = 
123           it->report_to ()->find_create_translator (to_type, to_id);
124         current->remove_translator (last);
125         dest->add_used_group_translator (last);
126       }
127     else
128       {
129         /*
130           We could change the current translator's id, but that would make 
131           errors hard to catch
132           
133            last->translator_id_string_  = get_change ()->change_to_id_string_;
134         */
135         error (_f ("I'm one myself: `%s'", to_type.to_str0 ()));
136       }
137   else
138     error (_f ("none of these in my family: `%s'", to_id.to_str0 ()));
139 }
140
141
142 // SCM*, moet / kan dat niet met set_x ofzo?
143 static void
144 get_music_info (Moment m, Music_iterator* iter, SCM *pitches, SCM *durations)
145 {
146   if (iter->ok ())
147     {
148       for (SCM i = iter->get_pending_events (m); gh_pair_p (i); i = ly_cdr (i))
149         {
150           Music *m = unsmob_music (ly_car (i));
151           if (Melodic_req *r = dynamic_cast<Melodic_req *> (m))
152             *pitches = gh_cons (r->get_mus_property ("pitch"), *pitches);
153           if (Rhythmic_req *r = dynamic_cast<Rhythmic_req *> (m))
154             {
155               SCM d = r->get_mus_property ("duration");
156               if (d == SCM_EOL)
157                 r->origin ()->warning ("Rhythmic_req has no duration\n");
158                 else
159                   // *durations = gh_cons (r->get_mus_property ("duration"), *durations);
160                   *durations = gh_cons (d, *durations);
161             }
162         }
163     }
164 }
165   
166 int
167 Part_combine_music_iterator::get_state (Moment)
168 {
169   int state = UNKNOWN;
170   Part_combine_music const *p = dynamic_cast<Part_combine_music const* > (get_music ());
171
172   String w = ly_scm2string (p->get_mus_property ("what"));
173     
174   
175   Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
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,
232                                scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
233               scm_sort_list_x (second_pitches,
234                                scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
235
236               interval = gh_int2scm (unsmob_pitch (ly_car (first_pitches))->steps ()
237                                      - unsmob_pitch (ly_car (scm_last_pair (second_pitches)))->steps ());
238             }
239
240           if (first_durations != SCM_EOL)
241             {
242               scm_sort_list_x (first_durations,
243                                scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
244               first_mom += unsmob_duration (ly_car (first_durations))->length_mom ();
245             }
246           
247           if (second_durations != SCM_EOL)
248             {
249               scm_sort_list_x (second_durations,
250                                scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
251               second_mom += unsmob_duration (ly_car (second_durations))->length_mom ();
252             }
253           
254           if (first_pitches != SCM_EOL && second_pitches == SCM_EOL
255                   && ! (second_until_ > now))
256             {
257               state |= UNRELATED;
258               state &= ~UNISILENCE;
259               if (! (state & ~ (UNRELATED | SOLO1 | UNISILENCE)))
260                 state |= SOLO1;
261             }
262           else
263             state &= ~SOLO1;
264
265           if (first_pitches == SCM_EOL && second_pitches != SCM_EOL
266               && ! (first_until_ > now))
267             {
268               state |= UNRELATED;
269               state &= ~UNISILENCE;
270               if (! (state & ~ (UNRELATED | SOLO2 | UNISILENCE)))
271                 state |= SOLO2;
272             }
273           else
274             state &= ~SOLO2;
275
276           if (gh_equal_p (first_durations, second_durations))
277             {
278               state &= ~UNISILENCE;
279               if (! (state & ~ (UNIRHYTHM | UNISON)))
280                 state |= UNIRHYTHM;
281             }
282           else
283             state &= ~ (UNIRHYTHM | UNISILENCE);
284           
285           if (first_pitches != SCM_EOL
286               && gh_equal_p (first_pitches, second_pitches))
287             {
288               state &= ~UNISILENCE;
289               if (! (state & ~ (UNIRHYTHM | UNISON)))
290                 state |= UNISON;
291             }
292           else
293             state &= ~ (UNISON);
294             
295           if (first_pitches == SCM_EOL && second_pitches == SCM_EOL)
296             {
297               if (! (state & ~ (UNIRHYTHM | UNISILENCE)))
298                 state |= UNISILENCE;
299             }
300           else if (!state)
301             state |= UNRELATED;
302           else
303             state &= ~ (UNISILENCE);
304
305           if (gh_number_p (interval))
306             {
307               SCM s = first_translator->get_property ("splitInterval");
308               int i = gh_scm2int (interval);
309               if (gh_pair_p (s)
310                   && gh_number_p (ly_car (s))
311                   && gh_number_p (ly_cdr (s))
312                   && i >= gh_scm2int (ly_car (s))
313                   && i <= gh_scm2int (ly_cdr (s)))
314                 {
315                   if (! (state & ~ (SPLIT_INTERVAL | UNIRHYTHM | UNISON)))
316                     state |= SPLIT_INTERVAL;
317                 }
318               else
319                 state &= ~ (SPLIT_INTERVAL);
320             }
321
322           if (first && first_pitches != SCM_EOL)
323             first_until_ = first_mom;
324           if (first && second_pitches != SCM_EOL)
325             second_until_ = second_mom;
326           first = false;
327
328           if (first_iter->ok ())
329             first_iter->skip (pending);
330           if (second_iter->ok ())
331             second_iter->skip (pending);
332           now = pending;
333         }
334       scm_gc_unprotect_object (first_iter->self_scm ());
335       scm_gc_unprotect_object (second_iter->self_scm ());
336     }
337
338   return state;
339 }
340
341 static Span_req* abort_req = NULL;
342
343 void
344 Part_combine_music_iterator::process (Moment m)
345 {
346
347   /*
348     TODO:
349     - Use three named contexts (be it Thread or Voice): one, two, solo.
350       Let user pre-set (pushproperty) stem direction, remove
351       dynamic-engraver, and such.
352
353       **** Tried this, but won't work:
354
355 s      Consider thread switching: threads "one", "two" and "both".
356       User can't pre-set the (most important) stem direction at
357       thread level!
358    */
359  
360   if (suffix_.empty_b ())
361     suffix_ = first_iter_->report_to ()
362       ->daddy_trans_->id_string_.cut_string (3, INT_MAX);
363
364   int state = get_state (m);
365   if (state)
366     state_ = state;
367   else
368     state = state_;
369   
370   Part_combine_music const *p =
371     dynamic_cast<Part_combine_music const* > (get_music ());
372
373
374   bool previously_combined_b = first_iter_->report_to ()->daddy_trans_
375     == second_iter_->report_to ()->daddy_trans_;
376
377   bool combine_b = previously_combined_b;
378
379   if (! (state & UNIRHYTHM)
380       || (state & SPLIT_INTERVAL)
381       || (state & (SOLO1 | SOLO2)))
382     combine_b = false;
383   else if (state & (UNIRHYTHM | UNISILENCE))
384     combine_b = true;
385
386   /*
387     When combining, abort all running spanners
388    */
389
390   if (!abort_req)
391     {
392       abort_req = new Span_req;
393       abort_req->set_mus_property ("span-type", scm_makfrom0str ("abort"));
394     }
395   
396   if (combine_b && combine_b != previously_combined_b)
397     {
398       if (second_iter_ && second_iter_->ok ())
399         second_iter_->try_music (abort_req);
400      }
401   String w = ly_scm2string (p->get_mus_property ("what"));
402   if (combine_b != previously_combined_b)
403     change_to (second_iter_, w, (combine_b ? "one" : "two")
404                + suffix_);
405   
406   Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
407   Translator_group *second_translator = second_iter_->report_to ()->find_create_translator (w, "two" + suffix_);
408   
409
410   /* Hmm */
411   first_translator->set_property ("combineParts", SCM_BOOL_T);
412   second_translator ->set_property ("combineParts", SCM_BOOL_T);
413  
414  
415   /* hmm */
416   SCM b = (state & UNIRHYTHM) ? SCM_BOOL_T : SCM_BOOL_F;
417   first_translator->set_property ("unirhythm", b);
418   second_translator->set_property ("unirhythm", b);
419
420   b = (state & SPLIT_INTERVAL) ? SCM_BOOL_T : SCM_BOOL_F;
421   first_translator->set_property ("split-interval", b);
422   second_translator->set_property ("split-interval",  b);
423
424   b = (state & UNISILENCE) ? SCM_BOOL_T : SCM_BOOL_F;
425   first_translator->set_property ("unisilence", b);
426   second_translator->set_property ("unisilence", b);
427
428   // difference in definition...
429   //b = ((state & UNISON) ? SCM_BOOL_T : SCM_BOOL_F;
430   b = ((state & UNISON) && (state & UNIRHYTHM)) ? SCM_BOOL_T : SCM_BOOL_F;
431   first_translator->set_property ("unison", b);
432   second_translator->set_property ("unison", b);
433
434   SCM b1 = (state & SOLO1) ? SCM_BOOL_T : SCM_BOOL_F;
435   SCM b2 = (state & SOLO2) ? SCM_BOOL_T : SCM_BOOL_F;
436   first_translator->set_property ("solo", b1);
437   second_translator->set_property ("solo", b2);
438
439   /* Can't these be computed? */
440   first_translator->set_property ("othersolo", b2);
441   second_translator->set_property ("othersolo", b1);
442
443   if (first_iter_->ok ())
444     first_iter_->process (m);
445   
446   if (second_iter_->ok ())
447     second_iter_->process (m);
448 }
449
450 Music_iterator*
451 Part_combine_music_iterator::try_music_in_children (Music *m) const
452 {
453   Music_iterator * i =  first_iter_->try_music (m);
454   if (i)
455     return i;
456   else
457     return second_iter_->try_music (m);
458 }
459
460
461 SCM
462 Part_combine_music_iterator::get_pending_events (Moment m)const
463 {
464   SCM s = SCM_EOL;
465   if (first_iter_)
466     s = gh_append2 (s,first_iter_->get_pending_events (m));
467   if (second_iter_)
468     s = gh_append2 (second_iter_->get_pending_events (m),s);
469   return s;
470 }
471
472 IMPLEMENT_CTOR_CALLBACK (Part_combine_music_iterator);