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