]> git.donarmstrong.com Git - lilypond.git/blob - lily/part-combine-music-iterator.cc
276e2f4271bfcc9e48fbfc7a23c63d45286548cc
[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_ = 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 (Melodic_req *r = dynamic_cast<Melodic_req *> (m))
157             *pitches = gh_cons (r->get_mus_property ("pitch"), *pitches);
158           if (Rhythmic_req *r = dynamic_cast<Rhythmic_req *> (m))
159             {
160               SCM d = r->get_mus_property ("duration");
161               if (d == SCM_EOL)
162                 r->origin ()->warning ("Rhythmic_req has no duration\n");
163                 else
164                   // *durations = gh_cons (r->get_mus_property ("duration"), *durations);
165                   *durations = gh_cons (d, *durations);
166             }
167         }
168     }
169 }
170   
171 int
172 Part_combine_music_iterator::get_state (Moment)
173 {
174   int state = UNKNOWN;
175   Part_combine_music const *p = dynamic_cast<Part_combine_music const* > (get_music ());
176
177   String w = ly_scm2string (p->get_mus_property ("what"));
178     
179   
180   Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
181
182   SCM s = first_translator->get_property ("changeMoment");
183   if (!gh_pair_p (s))
184     return state;
185
186   Moment change_mom = *unsmob_moment (ly_car (s));
187   Moment diff_mom = *unsmob_moment (ly_cdr (s));
188   
189   Moment now = pending_moment ();
190
191   if (!now.main_part_.mod_rat (change_mom.main_part_))
192     {
193       SCM interval = SCM_BOOL_F;
194       if (first_until_ < now)
195         first_until_ = now;
196       if (second_until_ < now)
197         second_until_ = now;
198
199       Moment first_mom = first_until_;
200       Moment second_mom = second_until_;
201       Moment diff_until = diff_mom + now;
202
203
204       bool first = true;
205       Music_iterator *first_iter = first_iter_->clone ();
206       Music_iterator *second_iter = second_iter_->clone ();
207       
208       Moment last_pending (-1);
209       Moment pending = now;
210       while (now < diff_until
211               && (first_iter->ok () || second_iter->ok ())
212
213              // urg, this is a hack, haven't caught this case yet
214              && (pending != last_pending))
215         {
216           if (!second_iter->ok ())
217             pending = first_iter->pending_moment ();
218           else if (!first_iter->ok ())
219             pending = second_iter->pending_moment ();
220           else
221             pending = first_iter->pending_moment () <? second_iter->pending_moment ();
222           last_pending = pending;
223
224           SCM first_pitches = SCM_EOL;
225           SCM first_durations = SCM_EOL;
226           get_music_info (pending, first_iter,
227                           &first_pitches, &first_durations);
228       
229           SCM second_pitches = SCM_EOL;
230           SCM second_durations = SCM_EOL;
231           get_music_info (pending, second_iter,
232                           &second_pitches, &second_durations);
233
234           if (first_pitches != SCM_EOL && second_pitches != SCM_EOL)
235             {
236               scm_sort_list_x (first_pitches,
237                                scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
238               scm_sort_list_x (second_pitches,
239                                scm_primitive_eval (ly_symbol2scm ("Pitch::less_p")));
240
241               interval = gh_int2scm (unsmob_pitch (ly_car (first_pitches))->steps ()
242                                      - unsmob_pitch (ly_car (scm_last_pair (second_pitches)))->steps ());
243             }
244
245           if (first_durations != SCM_EOL)
246             {
247               scm_sort_list_x (first_durations,
248                                scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
249               first_mom += unsmob_duration (ly_car (first_durations))->length_mom ();
250             }
251           
252           if (second_durations != SCM_EOL)
253             {
254               scm_sort_list_x (second_durations,
255                                scm_primitive_eval (ly_symbol2scm ("Duration::less_p")));
256               second_mom += unsmob_duration (ly_car (second_durations))->length_mom ();
257             }
258           
259           if (first_pitches != SCM_EOL && second_pitches == SCM_EOL
260                   && ! (second_until_ > now))
261             {
262               state |= UNRELATED;
263               state &= ~UNISILENCE;
264               if (! (state & ~ (UNRELATED | SOLO1 | UNISILENCE)))
265                 state |= SOLO1;
266             }
267           else
268             state &= ~SOLO1;
269
270           if (first_pitches == SCM_EOL && second_pitches != SCM_EOL
271               && ! (first_until_ > now))
272             {
273               state |= UNRELATED;
274               state &= ~UNISILENCE;
275               if (! (state & ~ (UNRELATED | SOLO2 | UNISILENCE)))
276                 state |= SOLO2;
277             }
278           else
279             state &= ~SOLO2;
280
281           if (gh_equal_p (first_durations, second_durations))
282             {
283               state &= ~UNISILENCE;
284               if (! (state & ~ (UNIRHYTHM | UNISON)))
285                 state |= UNIRHYTHM;
286             }
287           else
288             state &= ~ (UNIRHYTHM | UNISILENCE);
289           
290           if (first_pitches != SCM_EOL
291               && gh_equal_p (first_pitches, second_pitches))
292             {
293               state &= ~UNISILENCE;
294               if (! (state & ~ (UNIRHYTHM | UNISON)))
295                 state |= UNISON;
296             }
297           else
298             state &= ~ (UNISON);
299             
300           if (first_pitches == SCM_EOL && second_pitches == SCM_EOL)
301             {
302               if (! (state & ~ (UNIRHYTHM | UNISILENCE)))
303                 state |= UNISILENCE;
304             }
305           else if (!state)
306             state |= UNRELATED;
307           else
308             state &= ~ (UNISILENCE);
309
310           if (gh_number_p (interval))
311             {
312               SCM s = first_translator->get_property ("splitInterval");
313               int i = gh_scm2int (interval);
314               if (gh_pair_p (s)
315                   && gh_number_p (ly_car (s))
316                   && gh_number_p (ly_cdr (s))
317                   && i >= gh_scm2int (ly_car (s))
318                   && i <= gh_scm2int (ly_cdr (s)))
319                 {
320                   if (! (state & ~ (SPLIT_INTERVAL | UNIRHYTHM | UNISON)))
321                     state |= SPLIT_INTERVAL;
322                 }
323               else
324                 state &= ~ (SPLIT_INTERVAL);
325             }
326
327           if (first && first_pitches != SCM_EOL)
328             first_until_ = first_mom;
329           if (first && second_pitches != SCM_EOL)
330             second_until_ = second_mom;
331           first = false;
332
333           if (first_iter->ok ())
334             first_iter->skip (pending);
335           if (second_iter->ok ())
336             second_iter->skip (pending);
337           now = pending;
338         }
339       scm_gc_unprotect_object (first_iter->self_scm ());
340       scm_gc_unprotect_object (second_iter->self_scm ());
341     }
342
343   return state;
344 }
345
346 static Span_req* abort_req = NULL;
347
348 void
349 Part_combine_music_iterator::process (Moment m)
350 {
351
352   /*
353     TODO:
354     - Use three named contexts (be it Thread or Voice): one, two, solo.
355       Let user pre-set (pushproperty) stem direction, remove
356       dynamic-engraver, and such.
357
358       **** Tried this, but won't work:
359
360 s      Consider thread switching: threads "one", "two" and "both".
361       User can't pre-set the (most important) stem direction at
362       thread level!
363    */
364  
365   if (suffix_.empty_b ())
366     suffix_ = first_iter_->report_to ()
367       ->daddy_trans_->id_string_.cut_string (3, INT_MAX);
368
369   int state = get_state (m);
370   if (state)
371     state_ = state;
372   else
373     state = state_;
374   
375   Part_combine_music const *p =
376     dynamic_cast<Part_combine_music const* > (get_music ());
377
378
379   bool previously_combined_b = first_iter_->report_to ()->daddy_trans_
380     == second_iter_->report_to ()->daddy_trans_;
381
382   bool combine_b = previously_combined_b;
383
384   if (! (state & UNIRHYTHM)
385       || (state & SPLIT_INTERVAL)
386       || (state & (SOLO1 | SOLO2)))
387     combine_b = false;
388   else if (state & (UNIRHYTHM | UNISILENCE))
389     combine_b = true;
390
391   /*
392     When combining, abort all running spanners
393    */
394
395   if (!abort_req)
396     {
397       abort_req = new Span_req;
398       abort_req->set_mus_property ("span-type", scm_makfrom0str ("abort"));
399     }
400   
401   if (combine_b && combine_b != previously_combined_b)
402     {
403       if (second_iter_ && second_iter_->ok ())
404         second_iter_->try_music (abort_req);
405      }
406   String w = ly_scm2string (p->get_mus_property ("what"));
407   if (combine_b != previously_combined_b)
408     change_to (second_iter_, w, (combine_b ? "one" : "two")
409                + suffix_);
410   
411   Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
412   Translator_group *second_translator = second_iter_->report_to ()->find_create_translator (w, "two" + suffix_);
413   
414
415   /* Hmm */
416   first_translator->set_property ("combineParts", SCM_BOOL_T);
417   second_translator ->set_property ("combineParts", SCM_BOOL_T);
418  
419  
420   /* hmm */
421   SCM b = (state & UNIRHYTHM) ? SCM_BOOL_T : SCM_BOOL_F;
422   first_translator->set_property ("unirhythm", b);
423   second_translator->set_property ("unirhythm", b);
424
425   b = (state & SPLIT_INTERVAL) ? SCM_BOOL_T : SCM_BOOL_F;
426   first_translator->set_property ("split-interval", b);
427   second_translator->set_property ("split-interval",  b);
428
429   b = (state & UNISILENCE) ? SCM_BOOL_T : SCM_BOOL_F;
430   first_translator->set_property ("unisilence", b);
431   second_translator->set_property ("unisilence", b);
432
433   // difference in definition...
434   //b = ((state & UNISON) ? SCM_BOOL_T : SCM_BOOL_F;
435   b = ((state & UNISON) && (state & UNIRHYTHM)) ? SCM_BOOL_T : SCM_BOOL_F;
436   first_translator->set_property ("unison", b);
437   second_translator->set_property ("unison", b);
438
439   SCM b1 = (state & SOLO1) ? SCM_BOOL_T : SCM_BOOL_F;
440   SCM b2 = (state & SOLO2) ? SCM_BOOL_T : SCM_BOOL_F;
441   first_translator->set_property ("solo", b1);
442   second_translator->set_property ("solo", b2);
443
444   /* Can't these be computed? */
445   first_translator->set_property ("othersolo", b2);
446   second_translator->set_property ("othersolo", b1);
447
448   if (first_iter_->ok ())
449     first_iter_->process (m);
450   
451   if (second_iter_->ok ())
452     second_iter_->process (m);
453 }
454
455 Music_iterator*
456 Part_combine_music_iterator::try_music_in_children (Music *m) const
457 {
458   Music_iterator * i =  first_iter_->try_music (m);
459   if (i)
460     return i;
461   else
462     return second_iter_->try_music (m);
463 }
464
465
466 SCM
467 Part_combine_music_iterator::get_pending_events (Moment m)const
468 {
469   SCM s = SCM_EOL;
470   if (first_iter_)
471     s = gh_append2 (s,first_iter_->get_pending_events (m));
472   if (second_iter_)
473     s = gh_append2 (second_iter_->get_pending_events (m),s);
474   return s;
475 }
476
477 IMPLEMENT_CTOR_CALLBACK (Part_combine_music_iterator);