]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
Fix 453.
[lilypond.git] / lily / chord-tremolo-engraver.cc
1 /*
2   chord-tremolo-engraver.cc -- implement Chord_tremolo_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7                  Erik Sandberg <mandolaerik@gmail.com>
8 */
9
10 #include "beam.hh"
11 #include "engraver-group.hh"
12 #include "international.hh"
13 #include "item.hh"
14 #include "math.h" // ceil
15 #include "misc.hh"
16 #include "repeated-music.hh"
17 #include "rhythmic-head.hh"
18 #include "spanner.hh"
19 #include "stem-tremolo.hh"
20 #include "stem.hh"
21 #include "stream-event.hh"
22 #include "warn.hh"
23
24 #include "translator.icc"
25
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 class Chord_tremolo_engraver : public Engraver
41 {
42   TRANSLATOR_DECLARATIONS (Chord_tremolo_engraver);
43 protected:
44   Stream_event *repeat_;
45
46   // current direction of beam (first RIGHT, then LEFT)
47   Direction beam_dir_;
48
49   Spanner *beam_;
50
51 protected:
52   virtual void finalize ();
53   void process_music ();
54   DECLARE_TRANSLATOR_LISTENER (tremolo_span);
55   DECLARE_ACKNOWLEDGER (stem);
56 };
57
58 Chord_tremolo_engraver::Chord_tremolo_engraver ()
59 {
60   beam_ = 0;
61   repeat_ = 0;
62   beam_dir_ = CENTER;
63 }
64
65 IMPLEMENT_TRANSLATOR_LISTENER (Chord_tremolo_engraver, tremolo_span);
66 void
67 Chord_tremolo_engraver::listen_tremolo_span (Stream_event *ev)
68 {
69   Direction span_dir = to_dir (ev->get_property ("span-direction"));
70   if (span_dir == START)
71     {
72       if (ASSIGN_EVENT_ONCE (repeat_, ev))
73         {
74           beam_dir_ = RIGHT;
75         }
76     }
77   else if (span_dir == STOP)
78     {
79       if (!repeat_)
80         ev->origin ()->warning (_ ("No tremolo to end"));
81       repeat_ = 0;
82       beam_ = 0;
83       beam_dir_ = CENTER;
84     }
85 }
86
87 void
88 Chord_tremolo_engraver::process_music ()
89 {
90   if (repeat_ && !beam_)
91     {
92       beam_ = make_spanner ("Beam", repeat_->self_scm ());
93     }
94 }
95
96 void
97 Chord_tremolo_engraver::finalize ()
98 {
99   if (beam_)
100     {
101       repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
102       announce_end_grob (beam_, SCM_EOL);
103       beam_->suicide ();
104     }
105 }
106
107 void
108 Chord_tremolo_engraver::acknowledge_stem (Grob_info info)
109 {
110   if (beam_)
111     {
112       int tremolo_type = robust_scm2int (repeat_->get_property ("tremolo-type"), 1);
113       int flags = max (0, intlog2 (tremolo_type) - 2);
114       int repeat_count = robust_scm2int (repeat_->get_property ("repeat-count"), 1);
115       int gap_count = min (flags, intlog2 (repeat_count) + 1);
116
117       Grob *s = info.grob ();
118       Stem::set_beaming (s, flags, beam_dir_);
119
120       if (Stem::duration_log (s) != 1)
121         beam_->set_property ("gap-count", scm_from_int (gap_count));
122
123       if (beam_dir_ == RIGHT)
124         {
125           beam_dir_ = LEFT;
126           announce_end_grob (beam_, s->self_scm ());
127         }
128       
129       if (info.ultimate_event_cause ()->in_event_class ("rhythmic-event"))
130         Beam::add_stem (beam_, s);
131       else
132         {
133           string s = _ ("stem must have Rhythmic structure");
134           if (info.event_cause ())
135             info.event_cause ()->origin ()->warning (s);
136           else
137             ::warning (s);
138         }
139     }
140 }
141
142 ADD_ACKNOWLEDGER (Chord_tremolo_engraver, stem);
143 ADD_TRANSLATOR (Chord_tremolo_engraver,
144                 /* doc */
145                 "Generate beams for tremolo repeats.",
146
147                 /* create */
148                 "Beam ",
149
150                 /* read */
151                 "",
152
153                 /* write */
154                 ""
155                 );