]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / chord-tremolo-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5                  Erik Sandberg <mandolaerik@gmail.com>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "beam.hh"
22 #include "engraver-group.hh"
23 #include "international.hh"
24 #include "item.hh"
25 #include "math.h" // ceil
26 #include "misc.hh"
27 #include "repeated-music.hh"
28 #include "rhythmic-head.hh"
29 #include "spanner.hh"
30 #include "stem-tremolo.hh"
31 #include "stem.hh"
32 #include "stream-event.hh"
33 #include "warn.hh"
34
35 #include "translator.icc"
36
37 /**
38
39 This acknowledges repeated music with "tremolo" style.  It typesets
40 a beam.
41
42 TODO:
43
44 - perhaps use engraver this to steer other engravers? That would
45 create dependencies between engravers, which is bad.
46
47 - create dots if appropriate.
48
49 - create TremoloBeam iso Beam?
50 */
51 class Chord_tremolo_engraver : public Engraver
52 {
53   TRANSLATOR_DECLARATIONS (Chord_tremolo_engraver);
54 protected:
55   Stream_event *repeat_;
56
57   // current direction of beam (first RIGHT, then LEFT)
58   Direction beam_dir_;
59
60   Spanner *beam_;
61
62 protected:
63   virtual void finalize ();
64   void process_music ();
65   DECLARE_TRANSLATOR_LISTENER (tremolo_span);
66   DECLARE_ACKNOWLEDGER (stem);
67 };
68
69 Chord_tremolo_engraver::Chord_tremolo_engraver ()
70 {
71   beam_ = 0;
72   repeat_ = 0;
73   beam_dir_ = CENTER;
74 }
75
76 IMPLEMENT_TRANSLATOR_LISTENER (Chord_tremolo_engraver, tremolo_span);
77 void
78 Chord_tremolo_engraver::listen_tremolo_span (Stream_event *ev)
79 {
80   Direction span_dir = to_dir (ev->get_property ("span-direction"));
81   if (span_dir == START)
82     {
83       if (ASSIGN_EVENT_ONCE (repeat_, ev))
84         {
85           beam_dir_ = RIGHT;
86         }
87     }
88   else if (span_dir == STOP)
89     {
90       if (!repeat_)
91         ev->origin ()->warning (_ ("No tremolo to end"));
92       repeat_ = 0;
93       beam_ = 0;
94       beam_dir_ = CENTER;
95     }
96 }
97
98 void
99 Chord_tremolo_engraver::process_music ()
100 {
101   if (repeat_ && !beam_)
102     {
103       beam_ = make_spanner ("Beam", repeat_->self_scm ());
104     }
105 }
106
107 void
108 Chord_tremolo_engraver::finalize ()
109 {
110   if (beam_)
111     {
112       repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
113       announce_end_grob (beam_, SCM_EOL);
114       beam_->suicide ();
115     }
116 }
117
118 void
119 Chord_tremolo_engraver::acknowledge_stem (Grob_info info)
120 {
121   if (beam_)
122     {
123       int tremolo_type = robust_scm2int (repeat_->get_property ("tremolo-type"), 1);
124       int flags = max (0, intlog2 (tremolo_type) - 2);
125       int repeat_count = robust_scm2int (repeat_->get_property ("repeat-count"), 1);
126       int gap_count = min (flags, intlog2 (repeat_count) + 1);
127
128       Grob *s = info.grob ();
129       Stem::set_beaming (s, flags, beam_dir_);
130
131       if (Stem::duration_log (s) != 1)
132         beam_->set_property ("gap-count", scm_from_int (gap_count));
133
134       if (beam_dir_ == RIGHT)
135         {
136           beam_dir_ = LEFT;
137           announce_end_grob (beam_, s->self_scm ());
138         }
139       
140       if (info.ultimate_event_cause ()->in_event_class ("rhythmic-event"))
141         Beam::add_stem (beam_, s);
142       else
143         {
144           string s = _ ("stem must have Rhythmic structure");
145           if (info.event_cause ())
146             info.event_cause ()->origin ()->warning (s);
147           else
148             ::warning (s);
149         }
150     }
151 }
152
153 ADD_ACKNOWLEDGER (Chord_tremolo_engraver, stem);
154 ADD_TRANSLATOR (Chord_tremolo_engraver,
155                 /* doc */
156                 "Generate beams for tremolo repeats.",
157
158                 /* create */
159                 "Beam ",
160
161                 /* read */
162                 "",
163
164                 /* write */
165                 ""
166                 );