]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
2003 -> 2004
[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 "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   int total_duration_flags_;
54   
55   /// location  within measure where beam started.
56   Moment beam_start_location_;
57
58   bool sequential_body_b_;
59   Spanner * beam_;
60   Spanner * finished_beam_;
61   Item * stem_tremolo_;
62 protected:
63   virtual void finalize ();
64   virtual bool try_music (Music*);
65   virtual void acknowledge_grob (Grob_info);
66   virtual void stop_translation_timestep ();
67   virtual void start_translation_timestep ();
68   virtual void process_music ();
69 };
70
71 Chord_tremolo_engraver::Chord_tremolo_engraver ()
72 {
73   beam_  = finished_beam_ = 0;
74   repeat_ =0;
75   flags_ = 0;
76   stem_tremolo_ = 0;
77   sequential_body_b_ = false;
78 }
79
80 bool
81 Chord_tremolo_engraver::try_music (Music * m)
82 {
83   Repeated_music * rp = dynamic_cast<Repeated_music*> (m);
84   if (rp
85       && rp->get_mus_property ("iterator-ctor") == Chord_tremolo_iterator::constructor_proc
86       && !repeat_) 
87     {
88       Moment l = rp->get_length ();
89       repeat_ = rp;
90       start_mom_ = now_mom ();
91       stop_mom_ = start_mom_ + l;
92
93       Sequential_music * seq = dynamic_cast<Sequential_music*> (rp->body ());
94       sequential_body_b_ = seq;
95
96       int elt_count = seq ? scm_ilength (seq-> music_list ()) : 1;
97
98       if (seq && elt_count != 2)
99         {
100           rp->origin ()->warning (_f ("Chord tremolo with %d elements. Must have two elements.", elt_count));
101         }
102
103       if (elt_count <= 0)
104         elt_count = 1;
105           
106       Rational total_dur = l.main_part_;
107       Rational note_dur = total_dur / Rational (elt_count * repeat_->repeat_count ());
108
109       total_duration_flags_ = 0 >? (intlog2 (total_dur.den ()) - 2);
110       
111       flags_ = intlog2 (note_dur.den ()) -2 ;
112       
113       return true;
114     }
115
116   return false;
117 }
118
119 void
120 Chord_tremolo_engraver::process_music ()
121 {
122   if (repeat_)
123     {
124       if (sequential_body_b_ && !beam_)
125         {
126           beam_ = make_spanner ("Beam");
127           beam_->set_grob_property ("chord-tremolo", SCM_BOOL_T);
128
129           SCM smp = get_property ("measurePosition");
130           Moment mp
131             = (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
132           beam_start_location_ = mp;
133           announce_grob(beam_, repeat_->self_scm());
134         }
135       else if (!sequential_body_b_ && !stem_tremolo_)
136         {
137           if (flags_)
138             {
139               stem_tremolo_ = make_item ("StemTremolo");
140               announce_grob(stem_tremolo_, repeat_->self_scm());
141               stem_tremolo_->set_grob_property ("flag-count",
142                                                 scm_int2num (flags_));
143             }
144         }
145     }
146 }
147 void
148 Chord_tremolo_engraver::finalize ()
149 {
150   typeset_beam ();
151   if (beam_)
152     {
153       repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
154       beam_->suicide ();
155     }
156 }
157
158 void
159 Chord_tremolo_engraver::typeset_beam ()
160 {
161   if (finished_beam_)
162     {
163       typeset_grob (finished_beam_);
164       finished_beam_ = 0;
165     }
166 }
167
168 void
169 Chord_tremolo_engraver::acknowledge_grob (Grob_info info)
170 {
171   if (beam_ && Stem::has_interface (info.grob_))
172     {
173       Grob * s = info.grob_;
174
175       if (start_mom_ == now_mom())
176         Stem::set_beaming (s, flags_, RIGHT);
177       else
178         Stem::set_beaming (s, flags_, LEFT);
179           
180       if (Stem::duration_log (s) != 1)
181         {
182           beam_->set_grob_property ("gap-count", gh_int2scm (flags_ - total_duration_flags_));
183         }
184
185       if (info.music_cause ()->is_mus_type ("rhythmic-event"))
186         {
187           Beam::add_stem (beam_, s);
188         }
189       else
190         {
191           String s = _ ("stem must have Rhythmic structure");
192           if (info.music_cause ())
193             info.music_cause ()->origin ()->warning (s);
194           else
195             ::warning (s);
196         }
197     }
198   else if (stem_tremolo_ && Stem::has_interface (info.grob_))
199     {
200        Stem_tremolo::set_stem (stem_tremolo_, info.grob_);
201        stem_tremolo_->set_parent (info.grob_,X_AXIS);
202     }
203 }
204
205
206 void
207 Chord_tremolo_engraver::start_translation_timestep ()
208 {
209   if (beam_ && stop_mom_ == now_mom ())
210     {
211       finished_beam_ = beam_;
212       repeat_ = 0;
213       beam_ = 0;
214     }
215 }
216
217
218 void
219 Chord_tremolo_engraver::stop_translation_timestep ()
220 {
221   typeset_beam ();
222
223   if (stem_tremolo_)
224     {
225       typeset_grob (stem_tremolo_);
226       stem_tremolo_ = 0;
227     }
228 }
229
230
231
232 ENTER_DESCRIPTION(Chord_tremolo_engraver,
233 /* descr */       "Generates beams for  tremolo repeats.",
234 /* creats*/       "Beam",
235 /* accepts */     "repeated-music",
236 /* acks  */      "stem-interface note-head-interface",
237 /* reads */       "",
238 /* write */       "");