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