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-wrapper-iterator.hh"
18 #include "interpretation-context-handle.hh"
20 class Quote_iterator : public Music_wrapper_iterator
24 Moment vector_moment (int idx) const;
25 Interpretation_context_handle quote_outlet_;
33 SCM transposed_musics_;
35 DECLARE_SCHEME_CALLBACK (constructor, ());
36 bool quote_ok () const;
37 bool accept_music_type (Music*) const;
39 virtual void derived_mark () const;
40 virtual void construct_children ();
41 virtual Moment pending_moment () const;
42 virtual void process (Moment);
43 virtual bool ok () const;
47 Quote_iterator::accept_music_type (Music *mus) const
49 SCM accept = get_outlet()->get_property ("quotedEventTypes");
50 for (SCM s = mus->get_property ("types");
51 scm_is_pair (s); s = scm_cdr (s))
53 if (scm_memq (scm_car (s), accept) != SCM_BOOL_F)
62 Quote_iterator::derived_mark () const
64 scm_gc_mark (transposed_musics_ );
67 Quote_iterator::Quote_iterator ()
69 transposed_musics_ = SCM_EOL;
70 event_vector_ = SCM_EOL;
77 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less)(SCM a,SCM b))
80 int hi = SCM_VECTOR_LENGTH (vec);
85 int cmp = (lo + hi) / 2;
87 SCM when = scm_caar (SCM_VECTOR_REF (vec, cmp));
88 bool result = (*is_less) (key, when);
101 Quote_iterator::construct_children ()
103 Music_wrapper_iterator::construct_children ();
105 SCM name = get_music ()->get_property ("quoted-context-type");
106 SCM id = get_music ()->get_property ("quoted-context-id");
108 if (scm_is_string (id)
109 && scm_is_symbol (name))
111 Context *cue_context = get_outlet()->find_create_context (name,
112 ly_scm2string (id), SCM_EOL);
113 quote_outlet_.set_context (cue_context);
117 quote_outlet_.set_context (get_outlet ());
121 event_vector_ = get_music ()->get_property ("quoted-events");
124 We have to delay initting event_idx_ , since we have to
125 take starting grace notes into account. Those may offset
133 Quote_iterator::ok () const
136 Music_wrapper_iterator::ok()
141 Quote_iterator::quote_ok () const
143 return (event_idx_ >= 0
144 && ly_c_vector_p (event_vector_)
145 && event_idx_ <= end_idx_
148 Don't quote the grace notes leading to an unquoted note.
150 && vector_moment (event_idx_).main_part_ < stop_moment_.main_part_
155 Quote_iterator::pending_moment () const
158 infty.set_infinite (1);
161 if (Music_wrapper_iterator::ok())
162 m = m <? Music_wrapper_iterator::pending_moment();
165 In case event_idx_ < 0, we're not initted yet, and the wrapped
166 music expression determines the starting moment.
169 m = m <? vector_moment (event_idx_) - start_moment_;
175 Quote_iterator::vector_moment (int idx) const
177 SCM entry = SCM_VECTOR_REF (event_vector_, idx);
178 return *unsmob_moment (scm_caar (entry));
182 Quote_iterator::process (Moment m)
184 if (Music_wrapper_iterator::ok())
185 Music_wrapper_iterator::process (m);
187 if (!ly_c_vector_p (event_vector_))
192 event_idx_ = binsearch_scm_vector (event_vector_,
193 get_outlet ()->now_mom ().smobbed_copy (),
195 start_moment_ = get_outlet ()->now_mom () - music_start_mom();
196 stop_moment_ = start_moment_ + get_music()->get_length ();
198 end_idx_ = binsearch_scm_vector (event_vector_,
199 stop_moment_.smobbed_copy (),
204 while (event_idx_ <= end_idx_)
206 Moment em = vector_moment (event_idx_);
218 SCM entry = SCM_VECTOR_REF (event_vector_, event_idx_);
219 Pitch * quote_pitch = unsmob_pitch (scm_cdar (entry));
222 The pitch that sounds like central C
224 Pitch * me_pitch = unsmob_pitch (get_outlet ()->get_property ("instrumentTransposition"));
226 for (SCM s = scm_cdr (entry); scm_is_pair (s); s = scm_cdr (s))
228 SCM ev_acc = scm_car (s);
230 Music * mus = unsmob_music (scm_car (ev_acc));
232 programming_error ("need music in quote.");
233 else if (accept_music_type (mus))
235 if (quote_pitch || me_pitch)
243 Pitch diff = pitch_interval (qp, mp);
245 SCM copy = ly_deep_mus_copy (mus->self_scm ());
246 mus = unsmob_music (copy);
248 transposed_musics_ = scm_cons (copy, transposed_musics_);
249 mus->transpose (diff);
252 bool b = quote_outlet_.get_outlet ()->try_music (mus);
254 mus->origin ()->warning (_f ("In quotation: junking event %s", mus->name ()));
262 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);