]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
* lily/parser.yy (Repeated_music): bug fix beam multiplicity was
[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   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
41 class Chord_tremolo_engraver : public Engraver
42 {
43   void typeset_beam ();
44 TRANSLATOR_DECLARATIONS(Chord_tremolo_engraver);
45 protected:
46   Repeated_music * repeat_;
47
48   /// moment (global time) where beam started.
49   Moment start_mom_;
50   Moment stop_mom_;
51   int flags_ ;
52   /// location  within measure where beam started.
53   Moment beam_start_location_;
54
55   bool sequential_body_b_;
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   sequential_body_b_ = false;
75 }
76
77 bool
78 Chord_tremolo_engraver::try_music (Music * m)
79 {
80   Repeated_music * rp = dynamic_cast<Repeated_music*> (m);
81   if (rp
82       && rp->get_mus_property ("iterator-ctor") == Chord_tremolo_iterator::constructor_proc
83       && !repeat_) 
84     {
85       Moment l = rp->get_length ();
86       repeat_ = rp;
87       start_mom_ = now_mom ();
88       stop_mom_ = start_mom_ + l;
89
90       Sequential_music * seq = dynamic_cast<Sequential_music*> (rp->body ());
91       sequential_body_b_ = seq;
92
93       int elt_count = seq ? scm_ilength (seq-> music_list ()) : 1;
94
95       if (elt_count != 2)
96         {
97           rp->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 * repeat_->repeat_count ());
105
106       if (total_dur < Rational (1,4))
107         {
108           /*
109             This would require beams between flagged (8th) notes.
110           */
111           rp->origin ()->warning ("Chord tremolo is too short to denote properly.");
112         }
113       
114       Rational written_note_dur = total_dur / Rational (elt_count);
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
173 void
174 Chord_tremolo_engraver::acknowledge_grob (Grob_info info)
175 {
176   if (beam_ && Stem::has_interface (info.grob_))
177     {
178       Grob * s = info.grob_;
179       Stem::set_beaming (s, flags_, LEFT);
180       Stem::set_beaming (s, flags_, RIGHT);
181           
182       SCM d = s->get_grob_property ("direction");
183       if (Stem::duration_log (s) != 1)
184         {
185           beam_->set_grob_property ("gap", gh_double2scm (0.8));
186         }
187       s->set_grob_property ("direction", d);
188
189       if (info.music_cause ()->is_mus_type ("rhythmic-event"))
190         {
191           Beam::add_stem (beam_, s);
192         }
193       else
194         {
195           String s = _ ("stem must have Rhythmic structure");
196           if (info.music_cause ())
197             info.music_cause ()->origin ()->warning (s);
198           else
199             ::warning (s);
200         }
201     }
202   else if (stem_tremolo_ && Stem::has_interface (info.grob_))
203     {
204        Stem_tremolo::set_stem (stem_tremolo_, info.grob_);
205        stem_tremolo_->set_parent (info.grob_,X_AXIS);
206     }
207 }
208
209
210 void
211 Chord_tremolo_engraver::start_translation_timestep ()
212 {
213   if (beam_ && stop_mom_ == now_mom ())
214     {
215       finished_beam_ = beam_;
216
217       repeat_ = 0;
218       beam_ = 0;
219     }
220 }
221
222
223 void
224 Chord_tremolo_engraver::stop_translation_timestep ()
225 {
226   typeset_beam ();
227
228   if (stem_tremolo_)
229     {
230       typeset_grob (stem_tremolo_);
231       stem_tremolo_ = 0;
232     }
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 */       "");