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