2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "axis-group-interface.hh"
11 #include "dimensions.hh"
12 #include "directional-element-interface.hh"
13 #include "engraver.hh"
15 #include "international.hh"
16 #include "interval.hh"
17 #include "note-column.hh"
18 #include "paper-column.hh"
19 #include "pointer-group-interface.hh"
20 #include "script-interface.hh"
21 #include "self-alignment-interface.hh"
22 #include "side-position-interface.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "stream-event.hh"
27 #include "text-interface.hh"
29 #include "translator.icc"
34 * direction of text-dynamic-event if not equal to direction of
37 - TODO: this engraver is too complicated. We should split it into
38 the handling of the basic grobs and the linespanner
40 - TODO: the line-spanner is not killed after the (de)crescs are
45 print text & hairpin dynamics.
47 class Dynamic_engraver : public Engraver
50 Spanner *line_spanner_;
53 Spanner *finished_line_spanner_;
54 Spanner *finished_cresc_;
56 Stream_event *script_ev_;
57 Stream_event *current_cresc_ev_;
59 Drul_array<Stream_event *> accepted_spanevents_drul_;
61 vector<Note_column*> pending_columns_;
62 vector<Grob*> pending_elements_;
66 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
67 DECLARE_ACKNOWLEDGER (note_column);
68 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
69 DECLARE_TRANSLATOR_LISTENER (span_dynamic);
72 virtual void finalize ();
73 void stop_translation_timestep ();
74 void process_music ();
77 Dynamic_engraver::Dynamic_engraver ()
82 finished_line_spanner_ = 0;
83 current_cresc_ev_ = 0;
87 accepted_spanevents_drul_[START] = 0;
88 accepted_spanevents_drul_[STOP] = 0;
91 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
93 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
96 TODO: probably broken.
98 ASSIGN_EVENT_ONCE (script_ev_, ev);
101 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
103 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
105 Direction d = to_dir (ev->get_property ("span-direction"));
108 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[START], ev);
110 /* Cancel any ongoing crescendo, either explicitly by \! or
111 implicitly by a new crescendo. Also avoid warning if cresc is
112 cancelled both implicitly and explicitly. */
113 if ((d == STOP || current_cresc_ev_) && !accepted_spanevents_drul_[STOP])
114 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[STOP], ev);
118 Dynamic_engraver::process_music ()
120 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
124 Stream_event *rq = accepted_spanevents_drul_[START];
125 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
132 During a (de)crescendo, pending event will not be cleared,
133 and a line-spanner will always be created, as \< \! are already
136 Note: line-spanner must always have at least same duration
137 as (de)crecsendo, b.o. line-breaking.
141 maybe we should leave dynamic texts to the text-engraver and
142 simply acknowledge them?
146 script_ = make_item ("DynamicText", script_ev_->self_scm ());
147 script_->set_property ("text",
148 script_ev_->get_property ("text"));
150 if (Direction d = to_dir (script_ev_->get_property ("direction")))
151 set_grob_direction (line_spanner_, d);
152 else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
153 set_grob_direction (script_, d);
155 Axis_group_interface::add_element (line_spanner_, script_);
158 Stream_event *stop_ev = accepted_spanevents_drul_ [STOP]
159 ? accepted_spanevents_drul_[STOP] : script_ev_;
161 if (accepted_spanevents_drul_[STOP] || script_ev_)
164 finish side position alignment if the (de)cresc ends here, and
165 there are no new dynamics.
170 assert (!finished_cresc_ && cresc_);
174 cresc_->set_bound (RIGHT, script_);
175 add_bound_item (line_spanner_, script_);
178 finished_cresc_ = cresc_;
180 /* backwards compatibility with hairpinToBarline */
181 if (finished_cresc_->get_property ("to-barline") == SCM_EOL)
183 bool use_bar = to_boolean (get_property ("hairpinToBarline"))
184 && scm_is_string (get_property ("whichBar"));
186 finished_cresc_->set_property ("to-barline", scm_from_bool (use_bar));
189 announce_end_grob (finished_cresc_, SCM_EOL);
191 current_cresc_ev_ = 0;
193 else if (accepted_spanevents_drul_[STOP])
195 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("cannot find start of (de)crescendo"));
200 if (accepted_spanevents_drul_[START])
202 if (current_cresc_ev_)
204 string msg = _ ("already have a decrescendo");
205 if (current_cresc_ev_->in_event_class ("crescendo-event"))
206 msg = _ ("already have a crescendo");
208 accepted_spanevents_drul_[START]->origin ()->warning (msg);
209 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
213 current_cresc_ev_ = accepted_spanevents_drul_[START];
215 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
216 set_grob_direction (line_spanner_, d);
222 SCM start_sym = current_cresc_ev_->get_property ("class");
225 if (start_sym == ly_symbol2scm ("decrescendo-event"))
226 start_type = "decrescendo";
227 else if (start_sym == ly_symbol2scm ("crescendo-event"))
228 start_type = "crescendo";
231 programming_error ("unknown dynamic spanner type");
236 UGH. TODO: should read from original event, so appearance
237 may be altered with \tweak.
239 SCM s = get_property ((start_type + "Spanner").c_str ());
240 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
242 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
245 Pointer_group_interface::add_grob (finished_cresc_,
246 ly_symbol2scm ("adjacent-hairpins"),
249 Pointer_group_interface::add_grob (cresc_,
250 ly_symbol2scm ("adjacent-hairpins"),
256 This is a convenient (and legacy) interface to TextSpanners
257 for use in (de)crescendi.
262 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
263 cresc_->set_property ("style", s);
264 context ()->set_property ((start_type
265 + "Spanner").c_str (), SCM_EOL);
266 s = get_property ((start_type + "Text").c_str ());
267 if (Text_interface::is_markup (s))
269 cresc_->set_property ("text", s);
270 context ()->set_property ((start_type + "Text").c_str (),
276 set_nested_property (cresc_,
277 scm_list_3 (ly_symbol2scm ("bound-details"),
278 ly_symbol2scm ("left"),
279 ly_symbol2scm ("attach-dir")
281 scm_from_int (RIGHT));
287 cresc_->set_bound (LEFT, script_);
288 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
291 Axis_group_interface::add_element (line_spanner_, cresc_);
297 Dynamic_engraver::stop_translation_timestep ()
299 if (!current_cresc_ev_ && line_spanner_)
301 assert (!finished_line_spanner_);
302 finished_line_spanner_ = line_spanner_;
308 if (cresc_ && !cresc_->get_bound (LEFT))
310 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
311 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
315 accepted_spanevents_drul_[START] = 0;
316 accepted_spanevents_drul_[STOP] = 0;
320 Dynamic_engraver::finalize ()
325 && !line_spanner_->is_live ())
329 finished_line_spanner_ = line_spanner_;
334 && !cresc_->is_live ())
338 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
345 Dynamic_engraver::typeset_all ()
349 if (!finished_cresc_->get_bound (RIGHT))
352 Grob *column_bound = unsmob_grob (get_property ("currentMusicalColumn"));
354 finished_cresc_->set_bound (RIGHT, script_
358 if (finished_line_spanner_)
359 add_bound_item (finished_line_spanner_,
360 finished_cresc_->get_bound (RIGHT));
366 if (finished_line_spanner_)
371 extend-spanner-over-elements (finished_line_spanner_);
373 but this is rather kludgy, since finished_line_spanner_
374 typically has a staff-symbol field set , extending it over the
379 Grob *l = finished_line_spanner_->get_bound (LEFT);
380 Grob *r = finished_line_spanner_->get_bound (RIGHT);
382 finished_line_spanner_->set_bound (RIGHT, l);
384 finished_line_spanner_->set_bound (LEFT, r);
388 This is a isolated dynamic apparently, and does not even have
389 any interesting support item.
391 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
392 Item *ci = dynamic_cast<Item *> (cc);
393 finished_line_spanner_->set_bound (RIGHT, ci);
394 finished_line_spanner_->set_bound (LEFT, ci);
396 finished_line_spanner_ = 0;
401 Dynamic_engraver::acknowledge_note_column (Grob_info info)
407 /* Don't refill killed spanner */
408 && line_spanner_->is_live ())
410 Side_position_interface::add_support (line_spanner_, info.grob ());
411 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
414 if (script_ && !script_->get_parent (X_AXIS))
416 extract_grob_set (info.grob (), "note-heads", heads);
419 Grob *head = heads[0];
420 script_->set_parent (head, X_AXIS);
421 Self_alignment_interface::set_center_parent (script_, X_AXIS);
427 if (!cresc_->get_bound (LEFT))
429 cresc_->set_bound (LEFT, info.grob ());
430 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
434 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
435 finished_cresc_->set_bound (RIGHT, info.grob ());
438 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
440 ADD_TRANSLATOR (Dynamic_engraver,
442 "Create hairpins, dynamic texts, and their vertical"
443 " alignments. The symbols are collected onto a"
444 " @code{DynamicLineSpanner} grob which takes care of vertical"
448 "DynamicLineSpanner "
449 "DynamicTextSpanner "