2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2008--2015 Han-Wen Nienhuys <hanwen@lilypond.org>
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.
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.
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/>.
20 #include "engraver.hh"
22 #include "international.hh"
24 #include "note-column.hh"
25 #include "pointer-group-interface.hh"
26 #include "self-alignment-interface.hh"
28 #include "stream-event.hh"
29 #include "text-interface.hh"
31 #include "translator.icc"
33 class Dynamic_engraver : public Engraver
35 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
36 DECLARE_ACKNOWLEDGER (note_column);
37 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
38 DECLARE_TRANSLATOR_LISTENER (span_dynamic);
39 DECLARE_TRANSLATOR_LISTENER (break_span);
42 virtual void process_music ();
43 virtual void stop_translation_timestep ();
44 virtual void finalize ();
47 SCM get_property_setting (Stream_event *evt, char const *evprop,
49 string get_spanner_type (Stream_event *ev);
51 Drul_array<Stream_event *> accepted_spanevents_drul_;
52 Spanner *current_spanner_;
53 Spanner *finished_spanner_;
56 Stream_event *script_event_;
57 Stream_event *current_span_event_;
58 bool end_new_spanner_;
61 Dynamic_engraver::Dynamic_engraver ()
64 current_span_event_ = 0;
66 finished_spanner_ = 0;
68 accepted_spanevents_drul_.set (0, 0);
69 end_new_spanner_ = false;
72 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
74 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
76 ASSIGN_EVENT_ONCE (script_event_, ev);
79 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
81 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
83 Direction d = to_dir (ev->get_property ("span-direction"));
85 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[d], ev);
88 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, break_span);
90 Dynamic_engraver::listen_break_span (Stream_event *event)
92 if (event->in_event_class ("break-dynamic-span-event"))
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 current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
105 Dynamic_engraver::get_property_setting (Stream_event *evt,
109 SCM spanner_type = evt->get_property (evprop);
110 if (scm_is_null (spanner_type))
111 spanner_type = get_property (ctxprop);
116 Dynamic_engraver::process_music ()
119 && (accepted_spanevents_drul_[STOP]
121 || accepted_spanevents_drul_[START]))
123 Stream_event *ender = accepted_spanevents_drul_[STOP];
125 ender = script_event_;
128 ender = accepted_spanevents_drul_[START];
130 finished_spanner_ = current_spanner_;
131 announce_end_grob (finished_spanner_, ender->self_scm ());
132 current_spanner_ = 0;
133 current_span_event_ = 0;
136 if (accepted_spanevents_drul_[START])
138 current_span_event_ = accepted_spanevents_drul_[START];
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 ());
144 if (scm_is_eq (cresc_type, ly_symbol2scm ("text")))
147 = make_spanner ("DynamicTextSpanner",
148 accepted_spanevents_drul_[START]->self_scm ());
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 current_spanner_->set_property ("text", text);
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.
159 if (scm_is_eq (current_spanner_->get_property ("style"),
160 ly_symbol2scm ("none")))
161 current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
165 if (!scm_is_eq (cresc_type, ly_symbol2scm ("hairpin")))
167 string as_string = ly_scm_write_string (cresc_type);
169 ->origin ()->warning (_f ("unknown crescendo style: %s\ndefaulting to hairpin.", as_string.c_str ()));
171 current_spanner_ = make_spanner ("Hairpin",
172 current_span_event_->self_scm ());
174 // if we have a break-dynamic-span event right after the start dynamic, break the new spanner immediately
175 if (end_new_spanner_)
177 current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
178 end_new_spanner_ = false;
180 if (finished_spanner_)
182 if (has_interface<Hairpin> (finished_spanner_))
183 Pointer_group_interface::add_grob (finished_spanner_,
184 ly_symbol2scm ("adjacent-spanners"),
186 if (has_interface<Hairpin> (current_spanner_))
187 Pointer_group_interface::add_grob (current_spanner_,
188 ly_symbol2scm ("adjacent-spanners"),
195 script_ = make_item ("DynamicText", script_event_->self_scm ());
196 script_->set_property ("text",
197 script_event_->get_property ("text"));
199 if (finished_spanner_)
200 finished_spanner_->set_bound (RIGHT, script_);
201 if (current_spanner_)
202 current_spanner_->set_bound (LEFT, script_);
207 Dynamic_engraver::stop_translation_timestep ()
209 if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
212 unsmob<Grob> (get_property ("currentMusicalColumn")));
214 if (current_spanner_ && !current_spanner_->get_bound (LEFT))
217 unsmob<Grob> (get_property ("currentMusicalColumn")));
220 accepted_spanevents_drul_.set (0, 0);
221 finished_spanner_ = 0;
222 end_new_spanner_ = false;
226 Dynamic_engraver::finalize ()
229 && !current_spanner_->is_live ())
230 current_spanner_ = 0;
231 if (current_spanner_)
234 ->origin ()->warning (_f ("unterminated %s",
235 get_spanner_type (current_span_event_)
237 current_spanner_->suicide ();
238 current_spanner_ = 0;
243 Dynamic_engraver::get_spanner_type (Stream_event *ev)
246 SCM start_sym = scm_car (ev->get_property ("class"));
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")))
253 programming_error ("unknown dynamic spanner type");
259 Dynamic_engraver::acknowledge_note_column (Grob_info info)
261 if (script_ && !script_->get_parent (X_AXIS))
263 extract_grob_set (info.grob (), "note-heads", heads);
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.
268 Grob *x_parent = (heads.size ()
270 : unsmob<Grob> (info.grob ()->get_object ("rest")));
272 script_->set_parent (x_parent, X_AXIS);
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 ());
281 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
282 ADD_TRANSLATOR (Dynamic_engraver,
284 "Create hairpins, dynamic texts and dynamic text spanners.",
287 "DynamicTextSpanner "
294 "currentMusicalColumn "
295 "decrescendoSpanner "