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 (accidental);
68 DECLARE_ACKNOWLEDGER (stem_tremolo);
69 DECLARE_ACKNOWLEDGER (note_column);
70 DECLARE_ACKNOWLEDGER (slur);
71 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
72 DECLARE_TRANSLATOR_LISTENER (span_dynamic);
75 virtual void finalize ();
76 void stop_translation_timestep ();
77 void process_music ();
80 Dynamic_engraver::Dynamic_engraver ()
85 finished_line_spanner_ = 0;
86 current_cresc_ev_ = 0;
90 accepted_spanevents_drul_[START] = 0;
91 accepted_spanevents_drul_[STOP] = 0;
94 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
96 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
99 TODO: probably broken.
101 ASSIGN_EVENT_ONCE (script_ev_, ev);
104 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
106 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
108 Direction d = to_dir (ev->get_property ("span-direction"));
111 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[START], ev);
113 /* Cancel any ongoing crescendo, either explicitly by \! or
114 implicitly by a new crescendo. Also avoid warning if cresc is
115 cancelled both implicitly and explicitly. */
116 if ((d == STOP || current_cresc_ev_) && !accepted_spanevents_drul_[STOP])
117 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[STOP], ev);
121 Dynamic_engraver::process_music ()
123 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
127 Stream_event *rq = accepted_spanevents_drul_[START];
128 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
135 During a (de)crescendo, pending event will not be cleared,
136 and a line-spanner will always be created, as \< \! are already
139 Note: line-spanner must always have at least same duration
140 as (de)crecsendo, b.o. line-breaking.
144 maybe we should leave dynamic texts to the text-engraver and
145 simply acknowledge them?
149 script_ = make_item ("DynamicText", script_ev_->self_scm ());
150 script_->set_property ("text",
151 script_ev_->get_property ("text"));
153 if (Direction d = to_dir (script_ev_->get_property ("direction")))
154 set_grob_direction (line_spanner_, d);
155 else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
156 set_grob_direction (script_, d);
158 Axis_group_interface::add_element (line_spanner_, script_);
161 Stream_event *stop_ev = accepted_spanevents_drul_ [STOP]
162 ? accepted_spanevents_drul_[STOP] : script_ev_;
164 if (accepted_spanevents_drul_[STOP] || script_ev_)
167 finish side position alignment if the (de)cresc ends here, and
168 there are no new dynamics.
173 assert (!finished_cresc_ && cresc_);
177 cresc_->set_bound (RIGHT, script_);
178 add_bound_item (line_spanner_, script_);
181 finished_cresc_ = cresc_;
183 current_cresc_ev_ = 0;
185 else if (accepted_spanevents_drul_[STOP])
187 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("cannot find start of (de)crescendo"));
192 if (accepted_spanevents_drul_[START])
194 if (current_cresc_ev_)
196 string msg = _ ("already have a decrescendo");
197 if (current_cresc_ev_->in_event_class ("crescendo-event"))
198 msg = _ ("already have a crescendo");
200 accepted_spanevents_drul_[START]->origin ()->warning (msg);
201 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
205 current_cresc_ev_ = accepted_spanevents_drul_[START];
207 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
208 set_grob_direction (line_spanner_, d);
214 SCM start_sym = current_cresc_ev_->get_property ("class");
217 if (start_sym == ly_symbol2scm ("decrescendo-event"))
218 start_type = "decrescendo";
219 else if (start_sym == ly_symbol2scm ("crescendo-event"))
220 start_type = "crescendo";
223 programming_error ("unknown dynamic spanner type");
228 UGH. TODO: should read from original event, so appearance
229 may be altered with \tweak.
231 SCM s = get_property ((start_type + "Spanner").c_str ());
232 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
234 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
237 Pointer_group_interface::add_grob (finished_cresc_,
238 ly_symbol2scm ("adjacent-hairpins"),
241 Pointer_group_interface::add_grob (cresc_,
242 ly_symbol2scm ("adjacent-hairpins"),
248 This is a convenient (and legacy) interface to TextSpanners
249 for use in (de)crescendi.
254 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
255 cresc_->set_property ("style", s);
256 context ()->set_property ((start_type
257 + "Spanner").c_str (), SCM_EOL);
258 s = get_property ((start_type + "Text").c_str ());
259 if (Text_interface::is_markup (s))
261 cresc_->set_property ("text", s);
262 context ()->set_property ((start_type + "Text").c_str (),
269 cresc_->set_bound (LEFT, script_);
270 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
273 Axis_group_interface::add_element (line_spanner_, cresc_);
279 Dynamic_engraver::stop_translation_timestep ()
281 if (!current_cresc_ev_ && line_spanner_)
283 assert (!finished_line_spanner_);
284 finished_line_spanner_ = line_spanner_;
290 if (cresc_ && !cresc_->get_bound (LEFT))
292 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
293 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
297 accepted_spanevents_drul_[START] = 0;
298 accepted_spanevents_drul_[STOP] = 0;
302 Dynamic_engraver::finalize ()
307 && !line_spanner_->is_live ())
311 finished_line_spanner_ = line_spanner_;
316 && !cresc_->is_live ())
320 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
327 Dynamic_engraver::typeset_all ()
331 bool use_bar = to_boolean (get_property ("hairpinToBarline"))
332 && scm_is_string (get_property ("whichBar"))
336 if (!finished_cresc_->get_bound (RIGHT)
340 Grob *column_bound = 0;
343 column_bound = unsmob_grob (get_property ("breakableSeparationItem"));
347 column_bound = unsmob_grob (use_bar
348 ? get_property ("currentCommandColumn")
349 : get_property ("currentMusicalColumn"));
351 finished_cresc_->set_bound (RIGHT, script_
355 if (finished_line_spanner_)
356 add_bound_item (finished_line_spanner_,
357 finished_cresc_->get_bound (RIGHT));
363 if (finished_line_spanner_)
368 extend-spanner-over-elements (finished_line_spanner_);
370 but this is rather kludgy, since finished_line_spanner_
371 typically has a staff-symbol field set , extending it over the
376 Grob *l = finished_line_spanner_->get_bound (LEFT);
377 Grob *r = finished_line_spanner_->get_bound (RIGHT);
379 finished_line_spanner_->set_bound (RIGHT, l);
381 finished_line_spanner_->set_bound (LEFT, r);
385 This is a isolated dynamic apparently, and does not even have
386 any interesting support item.
388 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
389 Item *ci = dynamic_cast<Item *> (cc);
390 finished_line_spanner_->set_bound (RIGHT, ci);
391 finished_line_spanner_->set_bound (LEFT, ci);
394 finished_line_spanner_ = 0;
399 Dynamic_engraver::acknowledge_accidental (Grob_info info)
402 Side_position_interface::add_support (line_spanner_, info.grob ());
406 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
409 Side_position_interface::add_support (line_spanner_, info.grob ());
413 Dynamic_engraver::acknowledge_slur (Grob_info info)
416 Side_position_interface::add_support (line_spanner_, info.grob ());
420 Dynamic_engraver::acknowledge_note_column (Grob_info info)
426 /* Don't refill killed spanner */
427 && line_spanner_->is_live ())
429 Side_position_interface::add_support (line_spanner_, info.grob ());
430 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
433 if (script_ && !script_->get_parent (X_AXIS))
435 extract_grob_set (info.grob (), "note-heads", heads);
438 Grob *head = heads[0];
439 script_->set_parent (head, X_AXIS);
440 Self_alignment_interface::set_center_parent (script_, X_AXIS);
446 if (!cresc_->get_bound (LEFT))
448 cresc_->set_bound (LEFT, info.grob ());
449 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
453 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
454 finished_cresc_->set_bound (RIGHT, info.grob ());
457 ADD_ACKNOWLEDGER (Dynamic_engraver, accidental);
458 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
459 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
460 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
462 ADD_TRANSLATOR (Dynamic_engraver,
464 "This engraver creates hairpins, dynamic texts, and their vertical\n"
465 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
466 "which takes care of vertical positioning. ",
469 "DynamicLineSpanner "