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, ());
34 bool accept_music_type (Music*) const;
36 virtual void derived_mark () const;
37 virtual void construct_children ();
38 virtual Moment pending_moment () const;
39 virtual void process (Moment);
40 virtual bool ok () const;
44 Quote_iterator::accept_music_type (Music *mus) const
46 SCM accept = get_outlet()->get_property ("quotedEventTypes");
47 for (SCM s = mus->get_property ("types");
48 ly_c_pair_p (s); s = ly_cdr (s))
50 if (scm_memq (ly_car (s), accept) != SCM_BOOL_F)
59 Quote_iterator::derived_mark () const
61 scm_gc_mark (transposed_musics_ );
64 Quote_iterator::Quote_iterator ()
66 event_vector_ = SCM_EOL;
72 moment_less (SCM a, SCM b)
74 return *unsmob_moment (a) < *unsmob_moment (b);
79 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less)(SCM a,SCM b))
82 int hi = SCM_VECTOR_LENGTH (vec);
87 int cmp = (lo + hi) / 2;
89 SCM when = ly_caar (SCM_VECTOR_REF (vec, cmp));
90 bool result = (*is_less) (key, when);
103 Quote_iterator::construct_children ()
105 SCM dur = get_music ()->get_property ("duration");
106 if (!unsmob_duration (dur))
109 set_translator (get_outlet ()->get_default_interpreter ());
111 Moment now = get_outlet ()->now_mom ();
112 Moment stop = now + unsmob_duration (dur)->get_length ();
115 event_vector_ = get_music ()->get_property ("quoted-events");
117 if (ly_c_vector_p (event_vector_))
119 event_idx_ = binsearch_scm_vector (event_vector_, now.smobbed_copy (), &moment_less);
120 end_idx_ = binsearch_scm_vector (event_vector_, stop.smobbed_copy (), &moment_less);
124 get_music ()->origin()->warning (_("No events found for \\quote"));
130 Quote_iterator::ok () const
132 return ly_c_vector_p (event_vector_) && (event_idx_ <= end_idx_);
136 Quote_iterator::pending_moment () const
138 SCM entry = SCM_VECTOR_REF (event_vector_, event_idx_);
139 return *unsmob_moment (ly_caar (entry)) - start_moment_;
143 Quote_iterator::process (Moment m)
148 while (event_idx_ < end_idx_)
150 entry = SCM_VECTOR_REF (event_vector_, event_idx_);
152 Moment em = *unsmob_moment (ly_caar (entry));
163 if (ly_c_pair_p (entry))
165 Pitch * quote_pitch = unsmob_pitch (ly_cdar (entry));
166 Pitch * me_pitch = unsmob_pitch (get_outlet ()->get_property ("instrumentTransposition"));
168 for (SCM s = ly_cdr (entry); ly_c_pair_p (s); s = ly_cdr (s))
170 SCM ev_acc = ly_car (s);
172 Music * mus = unsmob_music (ly_car (ev_acc));
174 programming_error ("need music in quote.");
175 else if (accept_music_type (mus))
177 if (quote_pitch || me_pitch)
185 Pitch diff = interval (mp, qp);
187 SCM copy = ly_deep_mus_copy (mus->self_scm ());
188 mus = unsmob_music (copy);
189 transposed_musics_ = scm_cons (copy, transposed_musics_);
190 scm_gc_unprotect_object (copy);
193 mus->transpose (diff);
197 bool b = get_outlet ()->try_music (mus);
200 mus->origin ()->warning (_f ("In quotation: junking event %s", mus->name ()));
207 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);