]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
*** empty log message ***
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "math.h" // ceil
10
11 #include "beam.hh"
12 #include "chord-tremolo-iterator.hh"
13 #include "engraver-group.hh"
14 #include "international.hh"
15 #include "item.hh"
16 #include "misc.hh"
17 #include "repeated-music.hh"
18 #include "rhythmic-head.hh"
19 #include "spanner.hh"
20 #include "stem-tremolo.hh"
21 #include "stem.hh"
22 #include "warn.hh"
23
24 #include "translator.icc"
25
26 /**
27
28 This acknowledges repeated music with "tremolo" style.  It typesets
29 a beam.
30
31 TODO:
32
33 - perhaps use engraver this to steer other engravers? That would
34 create dependencies between engravers, which is bad.
35
36 - create dots if appropriate.
37
38 - create TremoloBeam iso Beam?
39 */
40 class Chord_tremolo_engraver : public Engraver
41 {
42   void typeset_beam ();
43   TRANSLATOR_DECLARATIONS (Chord_tremolo_engraver);
44 protected:
45   Music *repeat_;
46
47   /// moment (global time) where beam started.
48   Moment start_mom_;
49   Moment stop_mom_;
50   int flags_;
51   int total_duration_flags_;
52
53   /// location  within measure where beam started.
54   Moment beam_start_location_;
55
56   bool body_is_sequential_;
57   Spanner *beam_;
58   Spanner *finished_beam_;
59   Item *stem_tremolo_;
60 protected:
61   virtual void finalize ();
62   virtual bool try_music (Music *);
63   void stop_translation_timestep ();
64   void start_translation_timestep ();
65   void process_music ();
66   DECLARE_ACKNOWLEDGER (stem);
67 };
68
69 Chord_tremolo_engraver::Chord_tremolo_engraver ()
70 {
71   beam_ = finished_beam_ = 0;
72   repeat_ = 0;
73   flags_ = 0;
74   stem_tremolo_ = 0;
75   body_is_sequential_ = false;
76 }
77
78 bool
79 Chord_tremolo_engraver::try_music (Music *m)
80 {
81   if (m->is_mus_type ("repeated-music")
82       && m->get_property ("iterator-ctor") == Chord_tremolo_iterator::constructor_proc
83       && !repeat_)
84     {
85       Moment l = m->get_length ();
86       repeat_ = m;
87       start_mom_ = now_mom ();
88       stop_mom_ = start_mom_ + l;
89
90       Music *body = Repeated_music::body (m);
91       body_is_sequential_ = body->is_mus_type ("sequential-music");
92
93       int elt_count = body_is_sequential_ ? scm_ilength (body->get_property ("elements")) : 1;
94
95       if (body_is_sequential_ && elt_count != 2)
96         m->origin ()->warning (_f ("expect 2 elements for chord tremolo, found %d", elt_count));
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         beam_->set_property ("gap-count", scm_from_int (flags_ - total_duration_flags_));
157
158       if (info.ultimate_music_cause ()->is_mus_type ("rhythmic-event"))
159         Beam::add_stem (beam_, s);
160       else
161         {
162           string s = _ ("stem must have Rhythmic structure");
163           if (info.music_cause ())
164             info.music_cause ()->origin ()->warning (s);
165           else
166             ::warning (s);
167         }
168     }
169   else if (repeat_
170            && flags_
171            && !body_is_sequential_)
172     {
173       stem_tremolo_ = make_item ("StemTremolo", repeat_->self_scm ());
174       stem_tremolo_->set_property ("flag-count",
175                                    scm_from_int (flags_));
176       stem_tremolo_->set_object ("stem",
177                                  info.grob ()->self_scm ());
178       stem_tremolo_->set_parent (info.grob (), X_AXIS);
179       info.grob ()->set_object ("tremolo-flag", stem_tremolo_->self_scm ());
180     }
181 }
182
183 void
184 Chord_tremolo_engraver::start_translation_timestep ()
185 {
186   if (beam_ && stop_mom_ == now_mom ())
187     {
188       finished_beam_ = beam_;
189       repeat_ = 0;
190       beam_ = 0;
191     }
192 }
193
194 void
195 Chord_tremolo_engraver::stop_translation_timestep ()
196 {
197   if (stem_tremolo_)
198     {
199       repeat_ = 0;
200       if (beam_)
201         programming_error ("beam and stem tremolo?");
202       stem_tremolo_ = 0;
203     }
204
205   typeset_beam ();
206 }
207
208 ADD_ACKNOWLEDGER (Chord_tremolo_engraver, stem);
209 ADD_TRANSLATOR (Chord_tremolo_engraver,
210                 /* doc */ "Generates beams for  tremolo repeats.",
211                 /* create */ "Beam",
212                 /* accept */ "repeated-music",
213                 /* read */ "",
214                 /* write */ "");