2 quote-iterator.cc -- implement Quote_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "music-sequence.hh"
13 #include "lily-guile.hh"
14 #include "music-iterator.hh"
20 class Quote_iterator : public Music_iterator
30 SCM transposed_musics_;
32 DECLARE_SCHEME_CALLBACK (constructor, ());
35 virtual void derived_mark ();
36 virtual void construct_children ();
37 virtual Moment pending_moment () const;
38 virtual void process (Moment);
39 virtual bool ok () const;
43 Quote_iterator::derived_mark ()
45 scm_gc_mark (transposed_musics_ );
48 Quote_iterator::Quote_iterator ()
50 event_vector_ = SCM_EOL;
56 moment_less (SCM a, SCM b)
58 return *unsmob_moment (a) < *unsmob_moment (b);
63 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less)(SCM a,SCM b))
68 hi = SCM_VECTOR_LENGTH (vec);
73 int cmp = (lo + hi) / 2;
75 SCM when = ly_caar (SCM_VECTOR_REF (vec, cmp));
76 bool result = (*is_less) (key, when);
89 Quote_iterator::construct_children ()
91 SCM dur = get_music ()->get_property ("duration");
92 if (!unsmob_duration (dur))
95 set_translator (get_outlet ()->get_default_interpreter ());
97 Moment now = get_outlet ()->now_mom ();
98 Moment stop = now + unsmob_duration (dur)->get_length ();
101 event_vector_ = get_music ()->get_property ("quoted-events");
103 if (ly_c_vector_p (event_vector_))
105 event_idx_ = binsearch_scm_vector (event_vector_, now.smobbed_copy (), &moment_less);
106 end_idx_ = binsearch_scm_vector (event_vector_, stop.smobbed_copy (), &moment_less);
112 Quote_iterator::ok () const
114 return (event_idx_ <= end_idx_);
119 Quote_iterator::pending_moment () const
121 SCM entry = SCM_VECTOR_REF (event_vector_, event_idx_);
122 return *unsmob_moment (ly_caar (entry)) - start_moment_;
127 Quote_iterator::process (Moment m)
132 while (event_idx_ < end_idx_)
134 entry = SCM_VECTOR_REF (event_vector_, event_idx_);
136 Moment em = *unsmob_moment (ly_caar (entry));
147 if (ly_c_pair_p (entry))
149 Pitch * quote_pitch = unsmob_pitch (ly_cdar (entry));
150 Pitch * me_pitch = unsmob_pitch (get_outlet ()->get_property ("instrumentTransposition"));
152 for (SCM s = ly_cdr (entry); ly_c_pair_p (s); s = ly_cdr (s))
154 SCM ev_acc = ly_car (s);
157 Music * mus = unsmob_music (ly_car (ev_acc));
160 if (quote_pitch || me_pitch)
168 Pitch diff = interval (mp, qp);
170 SCM copy = ly_deep_mus_copy (mus->self_scm ());
171 mus = unsmob_music (copy);
172 transposed_musics_ = scm_cons (copy, transposed_musics_);
174 mus->transpose (diff);
178 bool b = get_outlet ()->try_music (mus);
181 mus->origin ()->warning (_f ("In quotation: junking event %s", mus->name ()));
184 programming_error ("need music in quote.");
190 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);