]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
* lily/include/grob-info.hh (class Grob_info): make data member
[lilypond.git] / lily / chord-tremolo-engraver.cc
1 /*
2   chord-tremolo-engraver.cc -- implement Chord_tremolo_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "beam.hh"
10 #include "repeated-music.hh"
11 #include "stem.hh"
12 #include "rhythmic-head.hh"
13 #include "engraver-group-engraver.hh"
14 #include "warn.hh"
15 #include "misc.hh"
16 #include "note-head.hh"
17 #include "spanner.hh"
18 #include "item.hh"
19 #include "chord-tremolo-iterator.hh"
20 #include "stem-tremolo.hh"
21 #include "math.h" // ceil
22
23 /**
24
25 This acknowledges repeated music with "tremolo" style.  It typesets
26 a beam.
27
28 TODO:
29
30 - perhaps use engraver this to steer other engravers? That would
31 create dependencies between engravers, which is bad.
32
33 - create dots if appropriate.
34
35 - create TremoloBeam iso Beam?
36 */
37 class Chord_tremolo_engraver : public Engraver
38 {
39   void typeset_beam ();
40   TRANSLATOR_DECLARATIONS (Chord_tremolo_engraver);
41 protected:
42   Music *repeat_;
43
44   /// moment (global time) where beam started.
45   Moment start_mom_;
46   Moment stop_mom_;
47   int flags_;
48   int total_duration_flags_;
49
50   /// location  within measure where beam started.
51   Moment beam_start_location_;
52
53   bool body_is_sequential_;
54   Spanner *beam_;
55   Spanner *finished_beam_;
56   Item *stem_tremolo_;
57 protected:
58   virtual void finalize ();
59   virtual bool try_music (Music *);
60   virtual void acknowledge_grob (Grob_info);
61   virtual void stop_translation_timestep ();
62   virtual void start_translation_timestep ();
63   virtual void process_music ();
64 };
65
66 Chord_tremolo_engraver::Chord_tremolo_engraver ()
67 {
68   beam_ = finished_beam_ = 0;
69   repeat_ = 0;
70   flags_ = 0;
71   stem_tremolo_ = 0;
72   body_is_sequential_ = false;
73 }
74
75 bool
76 Chord_tremolo_engraver::try_music (Music *m)
77 {
78   if (m->is_mus_type ("repeated-music")
79       && m->get_property ("iterator-ctor") == Chord_tremolo_iterator::constructor_proc
80       && !repeat_)
81     {
82       Moment l = m->get_length ();
83       repeat_ = m;
84       start_mom_ = now_mom ();
85       stop_mom_ = start_mom_ + l;
86
87       Music *body = Repeated_music::body (m);
88       body_is_sequential_ = body->is_mus_type ("sequential-music");
89
90       int elt_count = body_is_sequential_ ? scm_ilength (body->get_property ("elements")) : 1;
91
92       if (body_is_sequential_ && elt_count != 2)
93         {
94           m->origin ()->warning (_f ("expect 2 elements for chord tremolo, found %d", elt_count));
95         }
96
97       if (elt_count <= 0)
98         elt_count = 1;
99
100       Rational total_dur = l.main_part_;
101       Rational note_dur = total_dur / Rational (elt_count * Repeated_music::repeat_count (repeat_));
102
103       total_duration_flags_ = max (0, (intlog2 (total_dur.den ()) - 2));
104
105       flags_ = intlog2 (note_dur.den ()) -2;
106
107       return true;
108     }
109
110   return false;
111 }
112
113 void
114 Chord_tremolo_engraver::process_music ()
115 {
116   if (repeat_ && body_is_sequential_ && !beam_)
117     {
118       beam_ = make_spanner ("Beam", repeat_->self_scm ());
119       beam_->set_property ("chord-tremolo", SCM_BOOL_T);
120
121       beam_start_location_ = robust_scm2moment (get_property ("measurePosition"), Moment (0));
122     }
123 }
124
125 void
126 Chord_tremolo_engraver::finalize ()
127 {
128   typeset_beam ();
129   if (beam_)
130     {
131       repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
132       beam_->suicide ();
133     }
134 }
135
136 void
137 Chord_tremolo_engraver::typeset_beam ()
138 {
139   finished_beam_ = 0;
140 }
141
142 void
143 Chord_tremolo_engraver::acknowledge_grob (Grob_info info)
144 {
145   if (beam_ && Stem::has_interface (info.grob ()))
146     {
147       Grob *s = info.grob ();
148
149       if (start_mom_ == now_mom ())
150         Stem::set_beaming (s, flags_, RIGHT);
151       else
152         Stem::set_beaming (s, flags_, LEFT);
153
154       if (Stem::duration_log (s) != 1)
155         {
156           beam_->set_property ("gap-count", scm_int2num (flags_ - total_duration_flags_));
157         }
158
159       if (info.music_cause ()->is_mus_type ("rhythmic-event"))
160         {
161           Beam::add_stem (beam_, s);
162         }
163       else
164         {
165           String s = _ ("stem must have Rhythmic structure");
166           if (info.music_cause ())
167             info.music_cause ()->origin ()->warning (s);
168           else
169             ::warning (s);
170         }
171     }
172   else if (repeat_
173            && flags_ && !body_is_sequential_ && Stem::has_interface (info.grob ()))
174     {
175       stem_tremolo_ = make_item ("StemTremolo", repeat_->self_scm ());
176       stem_tremolo_->set_property ("flag-count",
177                                    scm_int2num (flags_));
178       stem_tremolo_->set_property ("stem",
179                                    info.grob ()->self_scm ());
180       stem_tremolo_->set_parent (info.grob (), X_AXIS);
181     }
182 }
183
184 void
185 Chord_tremolo_engraver::start_translation_timestep ()
186 {
187   if (beam_ && stop_mom_ == now_mom ())
188     {
189       finished_beam_ = beam_;
190       repeat_ = 0;
191       beam_ = 0;
192     }
193 }
194
195 void
196 Chord_tremolo_engraver::stop_translation_timestep ()
197 {
198   if (stem_tremolo_)
199     {
200       repeat_ = 0;
201       if (beam_)
202         programming_error ("beam and stem tremolo?");
203       stem_tremolo_ = 0;
204     }
205
206   typeset_beam ();
207 }
208
209 ADD_TRANSLATOR (Chord_tremolo_engraver,
210                 /* descr */ "Generates beams for  tremolo repeats.",
211                 /* creats*/ "Beam",
212                 /* accepts */ "repeated-music",
213                 /* acks  */ "stem-interface note-head-interface",
214                 /* reads */ "",
215                 /* write */ "");