1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 2008--2020 Han-Wen Nienhuys <hanwen@lilypond.org>
5
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "engraver.hh"
21 #include "hairpin.hh"
22 #include "international.hh"
23 #include "item.hh"
24 #include "note-column.hh"
25 #include "pointer-group-interface.hh"
26 #include "self-alignment-interface.hh"
27 #include "spanner.hh"
28 #include "stream-event.hh"
29 #include "text-interface.hh"
30
31 #include "translator.icc"
32
33 using std::string;
34
35 class Dynamic_engraver : public Engraver
36 {
37 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
38 void acknowledge_note_column (Grob_info);
39 void listen_absolute_dynamic (Stream_event *);
40 void listen_span_dynamic (Stream_event *);
41 void listen_break_span (Stream_event *);
42
43 protected:
44 virtual void process_music ();
45 virtual void stop_translation_timestep ();
46 void finalize () override;
47
48 private:
49 SCM get_property_setting (Stream_event *evt, char const *evprop,
50 char const *ctxprop);
51 string get_spanner_type (Stream_event *ev);
52
53 Drul_array<Stream_event *> accepted_spanevents_drul_;
54 Spanner *current_spanner_;
55 Spanner *finished_spanner_;
56
57 Item *script_;
58 Stream_event *script_event_;
59 Stream_event *current_span_event_;
60 bool end_new_spanner_;
61 };
62
Dynamic_engraver(Context * c)63 Dynamic_engraver::Dynamic_engraver (Context *c)
64 : Engraver (c)
65 {
66 script_event_ = 0;
67 current_span_event_ = 0;
68 script_ = 0;
69 finished_spanner_ = 0;
70 current_spanner_ = 0;
71 accepted_spanevents_drul_.set (0, 0);
72 end_new_spanner_ = false;
73 }
74
75 void
listen_absolute_dynamic(Stream_event * ev)76 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
77 {
78 ASSIGN_EVENT_ONCE (script_event_, ev);
79 }
80
81 void
listen_span_dynamic(Stream_event * ev)82 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
83 {
84 Direction d = from_scm<Direction> (get_property (ev, "span-direction"));
85
86 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[d], ev);
87 }
88
89 void
listen_break_span(Stream_event * event)90 Dynamic_engraver::listen_break_span (Stream_event *event)
91 {
92 if (event->in_event_class ("break-dynamic-span-event"))
93 {
94 // Case 1: Already have a start dynamic event -> break applies to new
95 // spanner (created later) -> set a flag
96 // Case 2: no new spanner, but spanner already active -> break it now
97 if (accepted_spanevents_drul_[START])
98 end_new_spanner_ = true;
99 else if (current_spanner_)
100 set_property (current_spanner_, "spanner-broken", SCM_BOOL_T);
101 }
102 }
103
104 SCM
get_property_setting(Stream_event * evt,char const * evprop,char const * ctxprop)105 Dynamic_engraver::get_property_setting (Stream_event *evt,
106 char const *evprop,
107 char const *ctxprop)
108 {
109 SCM spanner_type = get_property (evt, evprop);
110 if (scm_is_null (spanner_type))
111 spanner_type = get_property (this, ctxprop);
112 return spanner_type;
113 }
114
115 void
process_music()116 Dynamic_engraver::process_music ()
117 {
118 if (current_spanner_
119 && (accepted_spanevents_drul_[STOP]
120 || script_event_
121 || accepted_spanevents_drul_[START]))
122 {
123 Stream_event *ender = accepted_spanevents_drul_[STOP];
124 if (!ender)
125 ender = script_event_;
126
127 if (!ender)
128 ender = accepted_spanevents_drul_[START];
129
130 finished_spanner_ = current_spanner_;
131 announce_end_grob (finished_spanner_, ender->self_scm ());
132 current_spanner_ = 0;
133 current_span_event_ = 0;
134 }
135
136 if (accepted_spanevents_drul_[START])
137 {
138 current_span_event_ = accepted_spanevents_drul_[START];
139
140 string start_type = get_spanner_type (current_span_event_);
141 SCM cresc_type = get_property_setting (current_span_event_, "span-type",
142 (start_type + "Spanner").c_str ());
143
144 if (scm_is_eq (cresc_type, ly_symbol2scm ("text")))
145 {
146 current_spanner_
147 = make_spanner ("DynamicTextSpanner",
148 accepted_spanevents_drul_[START]->self_scm ());
149
150 SCM text = get_property_setting (current_span_event_, "span-text",
151 (start_type + "Text").c_str ());
152 if (Text_interface::is_markup (text))
153 set_property (current_spanner_, "text", text);
154 /*
155 If the line of a text spanner is hidden, end the alignment spanner
156 early: this allows dynamics to be spaced individually instead of
157 being linked together.
158 */
159 if (scm_is_eq (get_property (current_spanner_, "style"),
160 ly_symbol2scm ("none")))
161 set_property (current_spanner_, "spanner-broken", SCM_BOOL_T);
162 }
163 else
164 {
165 if (!scm_is_eq (cresc_type, ly_symbol2scm ("hairpin")))
166 {
167 string as_string = ly_scm_write_string (cresc_type);
168 current_span_event_
169 ->warning (_f ("unknown crescendo style: %s\ndefaulting to hairpin.",
170 as_string.c_str ()));
171 }
172 current_spanner_ = make_spanner ("Hairpin",
173 current_span_event_->self_scm ());
174 }
175 // if we have a break-dynamic-span event right after the start dynamic, break the new spanner immediately
176 if (end_new_spanner_)
177 {
178 set_property (current_spanner_, "spanner-broken", SCM_BOOL_T);
179 end_new_spanner_ = false;
180 }
181 if (finished_spanner_)
182 {
183 if (has_interface<Hairpin> (finished_spanner_))
184 Pointer_group_interface::add_grob (finished_spanner_,
185 ly_symbol2scm ("adjacent-spanners"),
186 current_spanner_);
187 if (has_interface<Hairpin> (current_spanner_))
188 Pointer_group_interface::add_grob (current_spanner_,
189 ly_symbol2scm ("adjacent-spanners"),
190 finished_spanner_);
191 }
192 }
193
194 if (script_event_)
195 {
196 script_ = make_item ("DynamicText", script_event_->self_scm ());
197 set_property (script_, "text",
198 get_property (script_event_, "text"));
199
200 if (finished_spanner_)
201 finished_spanner_->set_bound (RIGHT, script_);
202 if (current_spanner_)
203 current_spanner_->set_bound (LEFT, script_);
204 }
205 }
206
207 void
stop_translation_timestep()208 Dynamic_engraver::stop_translation_timestep ()
209 {
210 if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
211 finished_spanner_
212 ->set_bound (RIGHT,
213 unsmob<Grob> (get_property (this, "currentMusicalColumn")));
214
215 if (current_spanner_ && !current_spanner_->get_bound (LEFT))
216 current_spanner_
217 ->set_bound (LEFT,
218 unsmob<Grob> (get_property (this, "currentMusicalColumn")));
219 script_ = 0;
220 script_event_ = 0;
221 accepted_spanevents_drul_.set (0, 0);
222 finished_spanner_ = 0;
223 end_new_spanner_ = false;
224 }
225
226 void
finalize()227 Dynamic_engraver::finalize ()
228 {
229 if (current_spanner_
230 && !current_spanner_->is_live ())
231 current_spanner_ = 0;
232 if (current_spanner_)
233 {
234 current_span_event_
235 ->warning (_f ("unterminated %s",
236 get_spanner_type (current_span_event_).c_str ()));
237 current_spanner_->suicide ();
238 current_spanner_ = 0;
239 }
240 }
241
242 string
get_spanner_type(Stream_event * ev)243 Dynamic_engraver::get_spanner_type (Stream_event *ev)
244 {
245 string type;
246 SCM start_sym = scm_car (get_property (ev, "class"));
247
248 if (scm_is_eq (start_sym, ly_symbol2scm ("decrescendo-event")))
249 type = "decrescendo";
250 else if (scm_is_eq (start_sym, ly_symbol2scm ("crescendo-event")))
251 type = "crescendo";
252 else
253 programming_error ("unknown dynamic spanner type");
254
255 return type;
256 }
257
258 void
acknowledge_note_column(Grob_info info)259 Dynamic_engraver::acknowledge_note_column (Grob_info info)
260 {
261 if (script_ && !script_->get_x_parent ())
262 {
263 extract_grob_set (info.grob (), "note-heads", heads);
264 /*
265 Spacing constraints may require dynamics to be attached to rests,
266 so check for a rest if this note column has no note heads.
267 */
268 Grob *x_parent = (heads.size ()
269 ? info.grob ()
270 : unsmob<Grob> (get_object (info.grob (), "rest")));
271 if (x_parent)
272 script_->set_x_parent (x_parent);
273 }
274
275 if (current_spanner_ && !current_spanner_->get_bound (LEFT))
276 current_spanner_->set_bound (LEFT, info.grob ());
277 if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
278 finished_spanner_->set_bound (RIGHT, info.grob ());
279 }
280
281 void
boot()282 Dynamic_engraver::boot ()
283 {
284 ADD_LISTENER (Dynamic_engraver, absolute_dynamic);
285 ADD_LISTENER (Dynamic_engraver, span_dynamic);
286 ADD_LISTENER (Dynamic_engraver, break_span);
287 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
288 }
289
290 ADD_TRANSLATOR (Dynamic_engraver,
291 /* doc */
292 "Create hairpins, dynamic texts and dynamic text spanners.",
293
294 /* create */
295 "DynamicTextSpanner "
296 "DynamicText "
297 "Hairpin ",
298
299 /* read */
300 "crescendoSpanner "
301 "crescendoText "
302 "currentMusicalColumn "
303 "decrescendoSpanner "
304 "decrescendoText ",
305
306 /* write */
307 ""
308 );
309