]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
(Paper_column): copy rank_. This fixes
[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 "music-list.hh"
23 #include "math.h"           // ceil
24
25 /**
26
27 This acknowledges repeated music with "tremolo" style.  It typesets
28 a beam.
29
30 TODO:
31
32 - perhaps use engraver this to steer other engravers? That would
33 create dependencies between engravers, which is bad.
34
35 - create dots if appropriate.
36
37 - create TremoloBeam iso Beam?
38
39 */
40 class Chord_tremolo_engraver : public Engraver
41 {
42   void typeset_beam ();
43   TRANSLATOR_DECLARATIONS (Chord_tremolo_engraver);
44 protected:
45   Repeated_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 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_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 (seq && 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       total_duration_flags_ = 0 >? (intlog2 (total_dur.den ()) - 2);
108       
109       flags_ = intlog2 (note_dur.den ()) -2 ;
110       
111       return true;
112     }
113
114   return false;
115 }
116
117 void
118 Chord_tremolo_engraver::process_music ()
119 {
120   if (repeat_ && sequential_body_b_ && !beam_)
121     {
122       beam_ = make_spanner ("Beam", repeat_->self_scm ());
123       beam_->set_property ("chord-tremolo", SCM_BOOL_T);
124
125       beam_start_location_ = robust_scm2moment (get_property ("measurePosition"), Moment (0));
126     }
127 }
128
129 void
130 Chord_tremolo_engraver::finalize ()
131 {
132   typeset_beam ();
133   if (beam_)
134     {
135       repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
136       beam_->suicide ();
137     }
138 }
139
140 void
141 Chord_tremolo_engraver::typeset_beam ()
142 {
143   finished_beam_ = 0;
144 }
145
146 void
147 Chord_tremolo_engraver::acknowledge_grob (Grob_info info)
148 {
149   if (beam_ && Stem::has_interface (info.grob_))
150     {
151       Grob * s = info.grob_;
152
153       if (start_mom_ == now_mom ())
154         Stem::set_beaming (s, flags_, RIGHT);
155       else
156         Stem::set_beaming (s, flags_, LEFT);
157           
158       if (Stem::duration_log (s) != 1)
159         {
160           beam_->set_property ("gap-count", scm_int2num (flags_ - total_duration_flags_));
161         }
162
163       if (info.music_cause ()->is_mus_type ("rhythmic-event"))
164         {
165           Beam::add_stem (beam_, s);
166         }
167       else
168         {
169           String s = _ ("stem must have Rhythmic structure");
170           if (info.music_cause ())
171             info.music_cause ()->origin ()->warning (s);
172           else
173             ::warning (s);
174         }
175     }
176   else if (repeat_ &&
177            flags_ && !sequential_body_b_ && Stem::has_interface (info.grob_))
178     {
179       stem_tremolo_ = make_item ("StemTremolo", repeat_->self_scm ());
180       stem_tremolo_->set_property ("flag-count",
181                                    scm_int2num (flags_));
182       stem_tremolo_->set_property ("stem",
183                                    info.grob_->self_scm ());
184       stem_tremolo_->set_parent (info.grob_, X_AXIS);
185     }
186 }
187
188
189 void
190 Chord_tremolo_engraver::start_translation_timestep ()
191 {
192   if (beam_ && stop_mom_ == now_mom ())
193     {
194       finished_beam_ = beam_;
195       repeat_ = 0;
196       beam_ = 0;
197     }
198 }
199
200
201 void
202 Chord_tremolo_engraver::stop_translation_timestep ()
203 {
204   if (stem_tremolo_)
205     {
206       repeat_ = 0;
207       if (beam_)
208         programming_error ("Huh, beam and stem tremolo?");
209       stem_tremolo_ = 0;
210     }
211
212   typeset_beam ();
213 }
214
215
216
217 ENTER_DESCRIPTION (Chord_tremolo_engraver,
218 /* descr */       "Generates beams for  tremolo repeats.",
219 /* creats*/       "Beam",
220 /* accepts */     "repeated-music",
221 /* acks  */      "stem-interface note-head-interface",
222 /* reads */       "",
223 /* write */       "");