]> git.donarmstrong.com Git - lilypond.git/blob - lily/quote-iterator.cc
* lily/translator.cc (derived_mark): new function.
[lilypond.git] / lily / quote-iterator.cc
1 /*   
2   quote-iterator.cc --  implement Quote_iterator
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "context.hh"
11 #include "event.hh"
12 #include "music-sequence.hh"
13 #include "lily-guile.hh"
14 #include "music-iterator.hh"
15 #include "music.hh"
16 #include "input.hh"
17 #include "warn.hh"
18
19
20 class Quote_iterator : public Music_iterator
21 {
22 public:
23   Quote_iterator ();
24   
25   Moment start_moment_;
26   SCM event_vector_;
27   int event_idx_;
28   int end_idx_ ;
29
30   SCM transposed_musics_;
31   
32   DECLARE_SCHEME_CALLBACK (constructor, ()); 
33
34   bool accept_music_type (Music*) const;
35 protected:
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;
41 };
42
43 bool
44 Quote_iterator::accept_music_type (Music *mus) const
45 {
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))
49     {
50       if (scm_memq (ly_car (s), accept) != SCM_BOOL_F)
51         return true;
52     }
53
54   return false;
55 }
56
57
58 void
59 Quote_iterator::derived_mark () const
60 {
61   scm_gc_mark (transposed_musics_ );
62 }
63
64 Quote_iterator::Quote_iterator ()
65 {
66   event_vector_ = SCM_EOL;
67   event_idx_ = 0;
68   end_idx_ = 0;
69 }
70
71 bool
72 moment_less (SCM a, SCM b)
73 {
74   return  *unsmob_moment (a) < *unsmob_moment (b);
75 }
76
77
78 int
79 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less)(SCM a,SCM b))
80 {
81   int lo = 0;
82   int hi = SCM_VECTOR_LENGTH (vec);
83
84   /* binary search */
85   do
86   {
87     int cmp = (lo + hi) / 2;
88
89       SCM when = ly_caar (SCM_VECTOR_REF (vec, cmp));
90       bool result =  (*is_less) (key, when);
91       if (result)
92           hi = cmp;
93       else
94           lo = cmp;
95     }
96   while (hi - lo > 1);
97
98   return lo;
99 }
100
101
102 void
103 Quote_iterator::construct_children ()
104 {
105   SCM dur = get_music ()->get_property ("duration");
106   if (!unsmob_duration (dur))
107     return ;
108
109   set_translator (get_outlet ()->get_default_interpreter ());
110   
111   Moment now = get_outlet ()->now_mom ();
112   Moment stop = now + unsmob_duration (dur)->get_length ();
113
114   start_moment_ = now;
115   event_vector_ = get_music ()->get_property ("quoted-events");
116
117   if (ly_c_vector_p (event_vector_))
118     {
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);
121     }
122   else
123     {
124       get_music ()->origin()->warning (_("No events found for \\quote"));
125     }
126 }
127
128
129 bool
130 Quote_iterator::ok () const
131 {
132   return ly_c_vector_p (event_vector_) && (event_idx_ <= end_idx_);
133 }
134
135 Moment
136 Quote_iterator::pending_moment () const
137 {
138   SCM entry = SCM_VECTOR_REF (event_vector_, event_idx_);
139   return *unsmob_moment (ly_caar (entry)) - start_moment_;
140 }
141
142 void
143 Quote_iterator::process (Moment m)
144 {
145   SCM entry = SCM_EOL;
146
147   m += start_moment_;
148   while (event_idx_ < end_idx_)
149     {
150       entry = SCM_VECTOR_REF (event_vector_, event_idx_);
151
152       Moment em = *unsmob_moment (ly_caar (entry));
153
154       if (em > m)
155         return ;
156
157       if (em == m)
158         break ;
159
160       event_idx_++;
161     }
162
163   if (ly_c_pair_p (entry))
164     {
165       Pitch * quote_pitch = unsmob_pitch (ly_cdar (entry));
166       Pitch * me_pitch = unsmob_pitch (get_outlet ()->get_property ("instrumentTransposition"));
167       
168       for (SCM s = ly_cdr (entry); ly_c_pair_p (s); s = ly_cdr (s))
169         {
170           SCM ev_acc = ly_car (s);
171
172           Music * mus = unsmob_music (ly_car (ev_acc));
173           if (!mus)
174             programming_error ("need music in quote.");
175           else if (accept_music_type (mus))
176             {
177               if (quote_pitch || me_pitch)
178                 {
179                   Pitch qp, mp;
180                   if (quote_pitch)
181                     qp = *quote_pitch;
182                   if (me_pitch)
183                     mp = *me_pitch;
184
185                   Pitch diff = interval (mp, qp);
186
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);
191
192                   
193                   mus->transpose (diff);
194                 }
195
196               
197               bool b = get_outlet ()->try_music (mus);
198       
199               if (!b)
200                 mus->origin ()->warning (_f ("In quotation: junking event %s", mus->name ()));
201             }
202         }
203     }
204   event_idx_ ++; 
205 }
206
207 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);