1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 2000--2021 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 "misc.hh"
26 #include "rhythmic-head.hh"
27 #include "spanner.hh"
28 #include "stem-tremolo.hh"
29 #include "stem.hh"
30 #include "stream-event.hh"
31 #include "warn.hh"
32 
33 #include "translator.icc"
34 
35 using std::string;
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   Spanner *beam_;
58   // Store the pointer to the previous stem, so we can create a beam if
59   // necessary and end the spanner
60   Grob *previous_stem_;
61 
62 protected:
63   void finalize () override;
64   void process_music ();
65   void listen_tremolo_span (Stream_event *);
66   void acknowledge_stem (Grob_info);
67 };
68 
Chord_tremolo_engraver(Context * c)69 Chord_tremolo_engraver::Chord_tremolo_engraver (Context *c)
70   : Engraver (c)
71 {
72   beam_ = 0;
73   repeat_ = 0;
74   previous_stem_ = 0;
75 }
76 
77 void
listen_tremolo_span(Stream_event * ev)78 Chord_tremolo_engraver::listen_tremolo_span (Stream_event *ev)
79 {
80   Direction span_dir = from_scm<Direction> (get_property (ev, "span-direction"));
81   if (span_dir == START)
82     {
83       ASSIGN_EVENT_ONCE (repeat_, ev);
84     }
85   else if (span_dir == STOP)
86     {
87       if (!repeat_)
88         ev->warning (_ ("No tremolo to end"));
89       repeat_ = 0;
90       beam_ = 0;
91       previous_stem_ = 0;
92     }
93 }
94 
95 void
process_music()96 Chord_tremolo_engraver::process_music ()
97 {
98   if (repeat_ && !beam_)
99     {
100       beam_ = make_spanner ("Beam", repeat_->self_scm ());
101     }
102 }
103 
104 void
finalize()105 Chord_tremolo_engraver::finalize ()
106 {
107   if (beam_)
108     {
109       repeat_->warning (_ ("unterminated chord tremolo"));
110       announce_end_grob (beam_, SCM_EOL);
111       beam_->suicide ();
112     }
113 }
114 
115 void
acknowledge_stem(Grob_info info)116 Chord_tremolo_engraver::acknowledge_stem (Grob_info info)
117 {
118   if (beam_)
119     {
120       int tremolo_type = from_scm (get_property (repeat_, "tremolo-type"), 1);
121       int flags = std::max (0, intlog2 (tremolo_type) - 2);
122       int repeat_count = from_scm (get_property (repeat_, "repeat-count"), 1);
123       int gap_count = std::min (flags, intlog2 (repeat_count) + 1);
124 
125       Grob *s = info.grob ();
126       if (previous_stem_)
127         {
128           // FIXME: We know that the beam has ended only in listen_tremolo_span
129           //        but then it is too late for Spanner_break_forbid_engraver
130           //        to allow a line break... So, as a nasty hack, announce the
131           //        spanner's end after each note except the first. The only
132           //        "drawback" is that for multi-note tremolos a break would
133           //        theoretically be allowed after the second note (but since
134           //        that note is typically not at a barline, I don't think
135           //        anyone will ever notice!)
136           announce_end_grob (beam_, previous_stem_->self_scm ());
137           // Create the whole beam between previous and current note
138           Stem::set_beaming (previous_stem_, flags, RIGHT);
139           Stem::set_beaming (s, flags, LEFT);
140         }
141 
142       if (Stem::duration_log (s) != 1)
143         set_property (beam_, "gap-count", to_scm (gap_count));
144 
145       if (info.ultimate_event_cause ()->in_event_class ("rhythmic-event"))
146         Beam::add_stem (beam_, s);
147       else
148         s->warning (_ ("stem must have Rhythmic structure"));
149 
150       // Store current grob, so we can possibly end the spanner here (and
151       // reset the beam direction to RIGHT)
152       previous_stem_ = s;
153     }
154 }
155 
156 void
boot()157 Chord_tremolo_engraver::boot ()
158 {
159   ADD_LISTENER (Chord_tremolo_engraver, tremolo_span);
160   ADD_ACKNOWLEDGER (Chord_tremolo_engraver, stem);
161 }
162 
163 ADD_TRANSLATOR (Chord_tremolo_engraver,
164                 /* doc */
165                 "Generate beams for tremolo repeats.",
166 
167                 /* create */
168                 "Beam ",
169 
170                 /* read */
171                 "",
172 
173                 /* write */
174                 ""
175                );
176