]> git.donarmstrong.com Git - lilypond.git/blob - lily/quote-iterator.cc
9e012104de4cbd1edeeaebfea9d2cfbf5edb0a35
[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   Moment vector_moment (int idx) const;
25   
26   Moment start_moment_;
27   SCM event_vector_;
28   int event_idx_;
29   int end_idx_ ;
30
31   SCM transposed_musics_;
32   
33   DECLARE_SCHEME_CALLBACK (constructor, ()); 
34
35   bool accept_music_type (Music*) const;
36 protected:
37   virtual void derived_mark () const;
38   virtual void construct_children ();
39   virtual Moment pending_moment () const;
40   virtual void process (Moment);
41   virtual bool ok () const;
42 };
43
44 bool
45 Quote_iterator::accept_music_type (Music *mus) const
46 {
47   SCM accept = get_outlet()->get_property ("quotedEventTypes");
48   for (SCM s =  mus->get_property ("types");
49        scm_is_pair (s);  s = scm_cdr (s))
50     {
51       if (scm_memq (scm_car (s), accept) != SCM_BOOL_F)
52         return true;
53     }
54
55   return false;
56 }
57
58
59 void
60 Quote_iterator::derived_mark () const
61 {
62   scm_gc_mark (transposed_musics_ );
63 }
64
65 Quote_iterator::Quote_iterator ()
66 {
67   transposed_musics_ = SCM_EOL;
68   event_vector_ = SCM_EOL;
69   event_idx_ = 0;
70   end_idx_ = 0;
71 }
72
73 int
74 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less)(SCM a,SCM b));
75
76
77 void
78 Quote_iterator::construct_children ()
79 {
80   SCM dur = get_music ()->get_property ("duration");
81   if (!unsmob_duration (dur))
82     return ;
83
84   set_translator (get_outlet ()->get_default_interpreter ());
85   
86   Moment now = get_outlet ()->now_mom ();
87   Moment stop = now + unsmob_duration (dur)->get_length ();
88
89   start_moment_ = now;
90   event_vector_ = get_music ()->get_property ("quoted-events");
91
92   if (ly_c_vector_p (event_vector_))
93     {
94       event_idx_ = binsearch_scm_vector (event_vector_, now.smobbed_copy (), &moment_less);
95       end_idx_ = binsearch_scm_vector (event_vector_, stop.smobbed_copy (), &moment_less);
96     }
97   else
98     {
99       get_music ()->origin()->warning (_("No events found for \\quote"));
100     }
101 }
102
103
104 bool
105 Quote_iterator::ok () const
106 {
107   return ly_c_vector_p (event_vector_) && (event_idx_ <= end_idx_);
108 }
109
110 Moment
111 Quote_iterator::pending_moment () const
112 {
113   return vector_moment (event_idx_) - start_moment_;
114 }
115
116 Moment
117 Quote_iterator::vector_moment (int idx) const
118 {
119   SCM entry = SCM_VECTOR_REF (event_vector_, idx);
120   return *unsmob_moment (scm_caar (entry));
121 }
122   
123
124 void
125 Quote_iterator::process (Moment m)
126 {
127   m += start_moment_;
128   while (event_idx_ <= end_idx_)
129     {
130       Moment em = vector_moment (event_idx_);
131       if (em > m)
132         return ;
133
134       if (em == m)
135         break ;
136
137       event_idx_++;
138     }
139
140   if (event_idx_ <= end_idx_)
141     {
142       SCM entry = SCM_VECTOR_REF (event_vector_, event_idx_);
143       Pitch * quote_pitch = unsmob_pitch (scm_cdar (entry));
144
145       /*
146         The pitch that sounds like central C
147        */
148       Pitch * me_pitch = unsmob_pitch (get_outlet ()->get_property ("instrumentTransposition"));
149       
150       for (SCM s = scm_cdr (entry); scm_is_pair (s); s = scm_cdr (s))
151         {
152           SCM ev_acc = scm_car (s);
153
154           Music * mus = unsmob_music (scm_car (ev_acc));
155           if (!mus)
156             programming_error ("need music in quote.");
157           else if (accept_music_type (mus))
158             {
159               if (quote_pitch || me_pitch)
160                 {
161                   Pitch qp, mp;
162                   if (quote_pitch)
163                     qp = *quote_pitch;
164                   if (me_pitch)
165                     mp = *me_pitch;
166
167                   Pitch diff = pitch_interval (qp, mp);
168
169                   SCM copy = ly_deep_mus_copy (mus->self_scm ());
170                   mus = unsmob_music (copy);
171                   transposed_musics_ = scm_cons (copy, transposed_musics_);
172
173                   
174                   mus->transpose (diff);
175                 }
176
177               
178               bool b = get_outlet ()->try_music (mus);
179       
180               if (!b)
181                 mus->origin ()->warning (_f ("In quotation: junking event %s", mus->name ()));
182             }
183         }
184     }
185   event_idx_ ++; 
186 }
187
188 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);