2 quote-iterator.cc -- implement Quote_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "music-sequence.hh"
12 #include "lily-guile.hh"
13 #include "music-wrapper-iterator.hh"
16 class Quote_iterator : public Music_wrapper_iterator
20 Moment vector_moment (int idx) const;
21 Interpretation_context_handle quote_outlet_;
29 SCM transposed_musics_;
31 DECLARE_SCHEME_CALLBACK (constructor, ());
32 bool quote_ok () const;
33 bool accept_music_type (Music *) const;
35 virtual void derived_mark () const;
36 virtual void construct_children ();
37 virtual Moment pending_moment () const;
38 virtual void process (Moment);
39 virtual bool ok () const;
43 Quote_iterator::accept_music_type (Music *mus) const
45 SCM accept = get_outlet ()->get_property ("quotedEventTypes");
46 for (SCM s = mus->get_property ("types");
47 scm_is_pair (s); s = scm_cdr (s))
49 if (scm_memq (scm_car (s), accept) != SCM_BOOL_F)
57 Quote_iterator::derived_mark () const
59 scm_gc_mark (transposed_musics_);
62 Quote_iterator::Quote_iterator ()
64 transposed_musics_ = SCM_EOL;
65 event_vector_ = SCM_EOL;
71 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less) (SCM a, SCM b))
74 int hi = scm_c_vector_length (vec);
79 int cmp = (lo + hi) / 2;
81 SCM when = scm_caar (scm_c_vector_ref (vec, cmp));
82 bool result = (*is_less) (key, when);
94 Quote_iterator::construct_children ()
96 Music_wrapper_iterator::construct_children ();
98 SCM name = get_music ()->get_property ("quoted-context-type");
99 SCM id = get_music ()->get_property ("quoted-context-id");
101 if (scm_is_string (id)
102 && scm_is_symbol (name))
104 Context *cue_context = get_outlet ()->find_create_context (name,
105 ly_scm2string (id), SCM_EOL);
106 quote_outlet_.set_context (cue_context);
110 quote_outlet_.set_context (get_outlet ());
113 event_vector_ = get_music ()->get_property ("quoted-events");
116 We have to delay initting event_idx_ , since we have to
117 take starting grace notes into account. Those may offset
124 Quote_iterator::ok () const
127 Music_wrapper_iterator::ok ()
132 Quote_iterator::quote_ok () const
134 return (event_idx_ >= 0
135 && scm_is_vector (event_vector_)
136 && event_idx_ <= end_idx_
139 Don't quote the grace notes leading to an unquoted note.
141 && vector_moment (event_idx_).main_part_ < stop_moment_.main_part_);
145 Quote_iterator::pending_moment () const
148 infty.set_infinite (1);
151 if (Music_wrapper_iterator::ok ())
152 m = m <? Music_wrapper_iterator::pending_moment ();
155 In case event_idx_ < 0, we're not initted yet, and the wrapped
156 music expression determines the starting moment.
159 m = m <? vector_moment (event_idx_) - start_moment_;
165 Quote_iterator::vector_moment (int idx) const
167 SCM entry = scm_c_vector_ref (event_vector_, idx);
168 return *unsmob_moment (scm_caar (entry));
172 Quote_iterator::process (Moment m)
174 if (Music_wrapper_iterator::ok ())
175 Music_wrapper_iterator::process (m);
177 if (!scm_is_vector (event_vector_))
182 event_idx_ = binsearch_scm_vector (event_vector_,
183 get_outlet ()->now_mom ().smobbed_copy (),
185 start_moment_ = get_outlet ()->now_mom () - music_start_mom ();
186 stop_moment_ = start_moment_ + get_music ()->get_length ();
188 end_idx_ = binsearch_scm_vector (event_vector_,
189 stop_moment_.smobbed_copy (),
194 while (event_idx_ <= end_idx_)
196 Moment em = vector_moment (event_idx_);
208 SCM entry = scm_c_vector_ref (event_vector_, event_idx_);
209 Pitch *quote_pitch = unsmob_pitch (scm_cdar (entry));
212 The pitch that sounds like central C
214 Pitch *me_pitch = unsmob_pitch (get_outlet ()->get_property ("instrumentTransposition"));
216 for (SCM s = scm_cdr (entry); scm_is_pair (s); s = scm_cdr (s))
218 SCM ev_acc = scm_car (s);
220 Music *mus = unsmob_music (scm_car (ev_acc));
222 programming_error ("need music in quote.");
223 else if (accept_music_type (mus))
225 if (quote_pitch || me_pitch)
233 Pitch diff = pitch_interval (qp, mp);
235 SCM copy = ly_music_deep_copy (mus->self_scm ());
236 mus = unsmob_music (copy);
238 transposed_musics_ = scm_cons (copy, transposed_musics_);
239 mus->transpose (diff);
242 bool b = quote_outlet_.get_outlet ()->try_music (mus);
244 mus->origin ()->warning (_f ("In quotation: junking event %s", mus->name ()));
252 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);