]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
* scm/lily.scm: remove reduce-no-unit
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "engraver.hh"
11 #include "beam.hh"
12 #include "repeated-music.hh"
13 #include "stem.hh"
14 #include "rhythmic-head.hh"
15 #include "engraver-group-engraver.hh"
16 #include "event.hh"
17 #include "warn.hh"
18 #include "misc.hh"
19 #include "note-head.hh"
20 #include "spanner.hh"
21 #include "item.hh"
22 #include "chord-tremolo-iterator.hh"
23 #include "stem-tremolo.hh"
24 #include "music-list.hh"
25 #include "math.h"           // ceil
26
27 /**
28
29   This acknowledges repeated music with "tremolo" style.  It typesets
30   a beam.
31
32   TODO:
33
34   - perhaps use engraver this to steer other engravers? That would
35   create dependencies between engravers, which is bad.
36
37   - create dots if appropriate.
38
39   - create TremoloBeam iso Beam?
40
41 */
42 class Chord_tremolo_engraver : public Engraver
43 {
44   void typeset_beam ();
45   TRANSLATOR_DECLARATIONS(Chord_tremolo_engraver);
46 protected:
47   Repeated_music * repeat_;
48
49   /// moment (global time) where beam started.
50   Moment start_mom_;
51   Moment stop_mom_;
52   int flags_ ;
53   /// location  within measure where beam started.
54   Moment beam_start_location_;
55
56   bool sequential_body_b_;
57   Spanner * beam_;
58   Spanner * finished_beam_;
59   Item * stem_tremolo_;
60 protected:
61   virtual void finalize ();
62   virtual bool try_music (Music*);
63   virtual void acknowledge_grob (Grob_info);
64   virtual void stop_translation_timestep ();
65   virtual void start_translation_timestep ();
66   virtual void process_music ();
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   sequential_body_b_ = false;
76 }
77
78 bool
79 Chord_tremolo_engraver::try_music (Music * m)
80 {
81   Repeated_music * rp = dynamic_cast<Repeated_music*> (m);
82   if (rp
83       && rp->get_mus_property ("iterator-ctor") == Chord_tremolo_iterator::constructor_proc
84       && !repeat_) 
85     {
86       Moment l = rp->get_length ();
87       repeat_ = rp;
88       start_mom_ = now_mom ();
89       stop_mom_ = start_mom_ + l;
90
91       Sequential_music * seq = dynamic_cast<Sequential_music*> (rp->body ());
92       sequential_body_b_ = seq;
93
94       int elt_count = seq ? scm_ilength (seq-> music_list ()) : 1;
95
96       if (elt_count != 2)
97         {
98           rp->origin ()->warning (_f ("Chord tremolo with %d elements. Must have two elements.", elt_count));
99         }
100
101       if (elt_count <= 0)
102         elt_count = 1;
103           
104       Rational total_dur = l.main_part_;
105       Rational note_dur = total_dur / Rational (elt_count * repeat_->repeat_count ());
106
107       if (total_dur < Rational (1,4))
108         {
109           /*
110             This would require beams between flagged (8th) notes.
111           */
112           rp->origin ()->warning ("Chord tremolo is too short to denote properly.");
113         }
114       
115       flags_ = intlog2 (note_dur.den ()) -2 ;
116       
117       return true;
118     }
119
120   return false;
121 }
122
123 void
124 Chord_tremolo_engraver::process_music ()
125 {
126   if (repeat_)
127     {
128       if (sequential_body_b_ && !beam_)
129         {
130           beam_ = new Spanner (get_property ("Beam"));
131           beam_->set_grob_property ("chord-tremolo", SCM_BOOL_T);
132
133           SCM smp = get_property ("measurePosition");
134           Moment mp
135             = (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
136           beam_start_location_ = mp;
137           announce_grob(beam_, repeat_->self_scm());
138         }
139       else if (!sequential_body_b_ && !stem_tremolo_)
140         {
141           if (flags_)
142             {
143               stem_tremolo_ = new Item (get_property ("StemTremolo"));
144               announce_grob(stem_tremolo_, repeat_->self_scm());
145               stem_tremolo_->set_grob_property ("flag-count",
146                                                 scm_int2num (flags_));
147             }
148         }
149     }
150 }
151 void
152 Chord_tremolo_engraver::finalize ()
153 {
154   typeset_beam ();
155   if (beam_)
156     {
157       repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
158       beam_->suicide ();
159     }
160 }
161
162 void
163 Chord_tremolo_engraver::typeset_beam ()
164 {
165   if (finished_beam_)
166     {
167       typeset_grob (finished_beam_);
168       finished_beam_ = 0;
169     }
170 }
171
172 void
173 Chord_tremolo_engraver::acknowledge_grob (Grob_info info)
174 {
175   if (beam_ && Stem::has_interface (info.grob_))
176     {
177       Grob * s = info.grob_;
178
179       if (start_mom_ == now_mom())
180         Stem::set_beaming (s, flags_, RIGHT);
181       else
182         Stem::set_beaming (s, flags_, LEFT);
183           
184       SCM d = s->get_grob_property ("direction");
185       if (Stem::duration_log (s) != 1)
186         {
187           beam_->set_grob_property ("gap", gh_double2scm (0.8));
188         }
189       s->set_grob_property ("direction", d);
190
191       if (info.music_cause ()->is_mus_type ("rhythmic-event"))
192         {
193           Beam::add_stem (beam_, s);
194         }
195       else
196         {
197           String s = _ ("stem must have Rhythmic structure");
198           if (info.music_cause ())
199             info.music_cause ()->origin ()->warning (s);
200           else
201             ::warning (s);
202         }
203     }
204   else if (stem_tremolo_ && Stem::has_interface (info.grob_))
205     {
206        Stem_tremolo::set_stem (stem_tremolo_, info.grob_);
207        stem_tremolo_->set_parent (info.grob_,X_AXIS);
208     }
209 }
210
211
212 void
213 Chord_tremolo_engraver::start_translation_timestep ()
214 {
215   if (beam_ && stop_mom_ == now_mom ())
216     {
217       finished_beam_ = beam_;
218       repeat_ = 0;
219       beam_ = 0;
220     }
221 }
222
223
224 void
225 Chord_tremolo_engraver::stop_translation_timestep ()
226 {
227   typeset_beam ();
228
229   if (stem_tremolo_)
230     {
231       typeset_grob (stem_tremolo_);
232       stem_tremolo_ = 0;
233     }
234 }
235
236
237
238 ENTER_DESCRIPTION(Chord_tremolo_engraver,
239 /* descr */       "Generates beams for  tremolo repeats.",
240 /* creats*/       "Beam",
241 /* accepts */     "repeated-music",
242 /* acks  */      "stem-interface note-head-interface",
243 /* reads */       "",
244 /* write */       "");