]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
patch::: 1.3.109.jcn1
[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 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 "musical-request.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
24 /**
25   This acknowledges repeated music with "tremolo" style.  It typesets
26   a beam.
27
28   TODO:
29
30   - perhaps use engraver this to steer other engravers? That would
31   create dependencies between engravers, which is bad.
32
33   - create dots if appropriate.
34
35   - create  TremoloBeam iso Beam?
36  */
37
38 class Chord_tremolo_engraver : public Engraver
39 {
40   void typeset_beam ();
41 public:
42   VIRTUAL_COPY_CONS(Translator);
43   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
51   /// location  within measure where beam started.
52   Moment beam_start_location_;
53
54   int note_head_i_;
55   
56   Spanner * beam_p_;
57   Spanner * finished_beam_p_;
58   
59 protected:
60   virtual void do_removal_processing();
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 create_grobs ();
66 };
67
68 Chord_tremolo_engraver::Chord_tremolo_engraver()
69 {
70   beam_p_  = finished_beam_p_ = 0;
71   repeat_ =0;
72   note_head_i_ = 0;
73 }
74
75 bool
76 Chord_tremolo_engraver::try_music (Music * m)
77 {
78   Repeated_music * rp = dynamic_cast<Repeated_music*> (m);
79   if (rp
80       && rp->get_mus_property ("type") == Chord_tremolo_iterator::constructor_cxx_function
81       && !repeat_) 
82     {
83       Moment l = rp->body_length_mom ();
84       repeat_ = rp;
85       start_mom_ = now_mom ();
86       stop_mom_ = start_mom_ + l;
87
88       // ugh. should generate dots, triplet beams.      
89       note_head_i_ = l.den () <? 4; 
90       return true;
91     }
92   return false;
93 }
94
95 void
96 Chord_tremolo_engraver::create_grobs ()
97 {
98   if (repeat_ && !beam_p_)
99     {
100       beam_p_ = new Spanner (get_property ("Beam"));
101       Beam::set_interface (beam_p_);
102       beam_p_->set_grob_property ("chord-tremolo", SCM_BOOL_T);
103
104
105       SCM smp = get_property ("measurePosition");
106       Moment mp =  (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
107       beam_start_location_ = mp;
108       announce_grob (beam_p_, repeat_);
109     }
110 }
111
112
113 void
114 Chord_tremolo_engraver::do_removal_processing ()
115 {
116   typeset_beam ();
117   if (beam_p_)
118     {
119       repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
120 #if 0
121       finished_beam_p_ = beam_p_;
122       typeset_beam ();
123 #else
124       beam_p_->suicide ();
125 #endif
126     }
127 }
128
129 void
130 Chord_tremolo_engraver::typeset_beam ()
131 {
132   if (finished_beam_p_)
133     {
134       typeset_grob (finished_beam_p_);
135       finished_beam_p_ = 0;
136     }
137 }
138
139
140 void
141 Chord_tremolo_engraver::acknowledge_grob (Grob_info info)
142 {
143   if (beam_p_)
144     {
145       if (Stem::has_interface (info.elem_l_))
146         {
147           Grob * s = info.elem_l_;
148           int f = Stem::flag_i (s);
149           f = (f > 2) ? f - 2 : 1;
150           Stem::set_beaming (s, f, LEFT);
151           Stem::set_beaming (s, f, RIGHT);
152           
153           /*
154             URG: this sets the direction of the Stem s.
155             It's amazing Mike:
156             
157               Stem:: type_i () ->first_head ()->get_direction () ->
158                       Directional_element_interface::set (me, d);
159
160
161               don't understand this comment.
162                       --hwn.
163            */
164           SCM d = s->get_grob_property ("direction");
165           if (Stem::type_i (s ) != 1)
166             {
167               int gap_i =Stem::flag_i (s ) - ((Stem::type_i (s ) >? 2) - 2);
168               beam_p_->set_grob_property ("beam-gap", gh_int2scm(gap_i));
169             }
170           s->set_grob_property ("direction", d);
171
172           if (dynamic_cast <Rhythmic_req *> (info.req_l_))
173             {
174               Beam::add_stem (beam_p_, s);
175             }
176           else
177             {
178               String s = _ ("stem must have Rhythmic structure");
179               if (info.req_l_)
180                 info.req_l_->origin ()->warning (s);
181               else
182                 ::warning (s);
183             }
184         }
185       if (Note_head::has_interface (info.elem_l_))
186         {
187           info.elem_l_->set_grob_property ("duration-log", gh_int2scm (intlog2 (note_head_i_)));
188         }
189     }
190 }
191
192
193 void
194 Chord_tremolo_engraver::start_translation_timestep ()
195 {
196   if (beam_p_ && stop_mom_ == now_mom ())
197     {
198       finished_beam_p_ = beam_p_;
199
200       repeat_ = 0;
201       beam_p_ = 0;
202     }
203 }
204
205
206 void
207 Chord_tremolo_engraver::stop_translation_timestep ()
208 {
209   typeset_beam ();
210 }
211
212 ADD_THIS_TRANSLATOR(Chord_tremolo_engraver);
213