]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
* scm/define-grob-properties.scm (all-internal-grob-properties):
[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_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_stem (Grob_info info)
143 {
144   if (beam_)
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         beam_->set_property ("gap-count", scm_from_int (flags_ - total_duration_flags_));
155
156       if (info.ultimate_music_cause ()->is_mus_type ("rhythmic-event"))
157         Beam::add_stem (beam_, s);
158       else
159         {
160           string s = _ ("stem must have Rhythmic structure");
161           if (info.music_cause ())
162             info.music_cause ()->origin ()->warning (s);
163           else
164             ::warning (s);
165         }
166     }
167   else if (repeat_
168            && flags_
169            && !body_is_sequential_)
170     {
171       stem_tremolo_ = make_item ("StemTremolo", repeat_->self_scm ());
172       stem_tremolo_->set_property ("flag-count",
173                                    scm_from_int (flags_));
174       stem_tremolo_->set_object ("stem",
175                                  info.grob ()->self_scm ());
176       stem_tremolo_->set_parent (info.grob (), X_AXIS);
177       info.grob ()->set_object ("tremolo-flag", stem_tremolo_->self_scm ());
178     }
179 }
180
181 void
182 Chord_tremolo_engraver::start_translation_timestep ()
183 {
184   if (beam_ && stop_mom_ == now_mom ())
185     {
186       finished_beam_ = beam_;
187       repeat_ = 0;
188       beam_ = 0;
189     }
190 }
191
192 void
193 Chord_tremolo_engraver::stop_translation_timestep ()
194 {
195   if (stem_tremolo_)
196     {
197       repeat_ = 0;
198       if (beam_)
199         programming_error ("beam and stem tremolo?");
200       stem_tremolo_ = 0;
201     }
202
203   typeset_beam ();
204 }
205
206 ADD_ACKNOWLEDGER (Chord_tremolo_engraver, stem);
207 ADD_TRANSLATOR (Chord_tremolo_engraver,
208                 /* doc */ "Generates beams for  tremolo repeats.",
209                 /* create */ "Beam",
210                 /* accept */ "repeated-music",
211                 /* read */ "",
212                 /* write */ "");