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 scm_is_pair (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 transposed_musics_ = SCM_EOL;
67 event_vector_ = SCM_EOL;
73 moment_less (SCM a, SCM b)
75 return *unsmob_moment (a) < *unsmob_moment (b);
80 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less)(SCM a,SCM b))
83 int hi = SCM_VECTOR_LENGTH (vec);
88 int cmp = (lo + hi) / 2;
90 SCM when = ly_caar (SCM_VECTOR_REF (vec, cmp));
91 bool result = (*is_less) (key, when);
104 Quote_iterator::construct_children ()
106 SCM dur = get_music ()->get_property ("duration");
107 if (!unsmob_duration (dur))
110 set_translator (get_outlet ()->get_default_interpreter ());
112 Moment now = get_outlet ()->now_mom ();
113 Moment stop = now + unsmob_duration (dur)->get_length ();
116 event_vector_ = get_music ()->get_property ("quoted-events");
118 if (ly_c_vector_p (event_vector_))
120 event_idx_ = binsearch_scm_vector (event_vector_, now.smobbed_copy (), &moment_less);
121 end_idx_ = binsearch_scm_vector (event_vector_, stop.smobbed_copy (), &moment_less);
125 get_music ()->origin()->warning (_("No events found for \\quote"));
131 Quote_iterator::ok () const
133 return ly_c_vector_p (event_vector_) && (event_idx_ <= end_idx_);
137 Quote_iterator::pending_moment () const
139 SCM entry = SCM_VECTOR_REF (event_vector_, event_idx_);
140 return *unsmob_moment (ly_caar (entry)) - start_moment_;
144 Quote_iterator::process (Moment m)
149 while (event_idx_ < end_idx_)
151 entry = SCM_VECTOR_REF (event_vector_, event_idx_);
153 Moment em = *unsmob_moment (ly_caar (entry));
164 if (scm_is_pair (entry))
166 Pitch * quote_pitch = unsmob_pitch (ly_cdar (entry));
169 The pitch that sounds like central C
171 Pitch * me_pitch = unsmob_pitch (get_outlet ()->get_property ("instrumentTransposition"));
173 for (SCM s = ly_cdr (entry); scm_is_pair (s); s = ly_cdr (s))
175 SCM ev_acc = ly_car (s);
177 Music * mus = unsmob_music (ly_car (ev_acc));
179 programming_error ("need music in quote.");
180 else if (accept_music_type (mus))
182 if (quote_pitch || me_pitch)
190 Pitch diff = pitch_interval (qp, mp);
192 SCM copy = ly_deep_mus_copy (mus->self_scm ());
193 mus = unsmob_music (copy);
194 transposed_musics_ = scm_cons (copy, transposed_musics_);
197 mus->transpose (diff);
201 bool b = get_outlet ()->try_music (mus);
204 mus->origin ()->warning (_f ("In quotation: junking event %s", mus->name ()));
211 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);