2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 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 "translator.icc"
32 * direction of text-dynamic-event if not equal to direction of
35 - TODO: this engraver is too complicated. We should split it into
36 the handling of the basic grobs and the linespanner
38 - TODO: the line-spanner is not killed after the (de)crescs are
43 print text & hairpin dynamics.
45 class Dynamic_engraver
: public Engraver
48 Spanner
*line_spanner_
;
51 Spanner
*finished_line_spanner_
;
52 Spanner
*finished_cresc_
;
54 Stream_event
*script_ev_
;
55 Stream_event
*current_cresc_ev_
;
57 Drul_array
<Stream_event
*> accepted_spanevents_drul_
;
59 vector
<Note_column
*> pending_columns_
;
60 vector
<Grob
*> pending_elements_
;
64 TRANSLATOR_DECLARATIONS (Dynamic_engraver
);
65 DECLARE_ACKNOWLEDGER (accidental
);
66 DECLARE_ACKNOWLEDGER (script
);
67 DECLARE_ACKNOWLEDGER (stem_tremolo
);
68 DECLARE_ACKNOWLEDGER (note_column
);
69 DECLARE_ACKNOWLEDGER (slur
);
70 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic
);
71 DECLARE_TRANSLATOR_LISTENER (span_dynamic
);
74 virtual void finalize ();
75 void stop_translation_timestep ();
76 void process_music ();
79 Dynamic_engraver::Dynamic_engraver ()
84 finished_line_spanner_
= 0;
85 current_cresc_ev_
= 0;
89 accepted_spanevents_drul_
[START
] = 0;
90 accepted_spanevents_drul_
[STOP
] = 0;
93 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver
, absolute_dynamic
);
95 Dynamic_engraver::listen_absolute_dynamic (Stream_event
*ev
)
98 TODO: probably broken.
100 ASSIGN_EVENT_ONCE (script_ev_
, ev
);
103 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver
, span_dynamic
);
105 Dynamic_engraver::listen_span_dynamic (Stream_event
*ev
)
107 Direction d
= to_dir (ev
->get_property ("span-direction"));
110 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_
[START
], ev
);
112 /* Cancel any ongoing crescendo, either explicitly by \! or
113 implicitly by a new crescendo. Also avoid warning if cresc is
114 cancelled both implicitly and explicitly. */
115 if ((d
== STOP
|| current_cresc_ev_
) && !accepted_spanevents_drul_
[STOP
])
116 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_
[STOP
], ev
);
120 Dynamic_engraver::process_music ()
122 if (accepted_spanevents_drul_
[START
] || accepted_spanevents_drul_
[STOP
] || script_ev_
)
126 Stream_event
*rq
= accepted_spanevents_drul_
[START
];
127 line_spanner_
= make_spanner ("DynamicLineSpanner", rq
? rq
->self_scm () : SCM_EOL
);
134 During a (de)crescendo, pending event will not be cleared,
135 and a line-spanner will always be created, as \< \! are already
138 Note: line-spanner must always have at least same duration
139 as (de)crecsendo, b.o. line-breaking.
143 maybe we should leave dynamic texts to the text-engraver and
144 simply acknowledge them?
148 script_
= make_item ("DynamicText", script_ev_
->self_scm ());
149 script_
->set_property ("text",
150 script_ev_
->get_property ("text"));
152 if (Direction d
= to_dir (script_ev_
->get_property ("direction")))
153 set_grob_direction (line_spanner_
, d
);
154 else if (Direction d
= to_dir (line_spanner_
->get_property ("direction")))
155 set_grob_direction (script_
, d
);
157 Axis_group_interface::add_element (line_spanner_
, script_
);
160 Stream_event
*stop_ev
= accepted_spanevents_drul_
[STOP
]
161 ? accepted_spanevents_drul_
[STOP
] : script_ev_
;
163 if (accepted_spanevents_drul_
[STOP
] || script_ev_
)
166 finish side position alignment if the (de)cresc ends here, and
167 there are no new dynamics.
172 assert (!finished_cresc_
&& cresc_
);
176 cresc_
->set_bound (RIGHT
, script_
);
177 add_bound_item (line_spanner_
, script_
);
180 finished_cresc_
= cresc_
;
182 current_cresc_ev_
= 0;
184 else if (accepted_spanevents_drul_
[STOP
])
186 accepted_spanevents_drul_
[STOP
]->origin ()->warning (_ ("can't find start of (de)crescendo"));
191 if (accepted_spanevents_drul_
[START
])
193 if (current_cresc_ev_
)
195 string msg
= _ ("already have a decrescendo");
196 if (current_cresc_ev_
->in_event_class ("crescendo-event"))
197 msg
= _ ("already have a crescendo");
199 accepted_spanevents_drul_
[START
]->origin ()->warning (msg
);
200 current_cresc_ev_
->origin ()->warning (_ ("cresc starts here"));
204 current_cresc_ev_
= accepted_spanevents_drul_
[START
];
206 if (Direction d
= to_dir (current_cresc_ev_
->get_property ("direction")))
207 set_grob_direction (line_spanner_
, d
);
213 SCM start_sym
= current_cresc_ev_
->get_property ("class");
216 if (start_sym
== ly_symbol2scm ("decrescendo-event"))
217 start_type
= "decrescendo";
218 else if (start_sym
== ly_symbol2scm ("crescendo-event"))
219 start_type
= "crescendo";
222 programming_error ("unknown dynamic spanner type");
227 UGH. TODO: should read from original event, so appearance
228 may be altered with \tweak.
230 SCM s
= get_property ((start_type
+ "Spanner").c_str ());
231 if (!scm_is_symbol (s
) || s
== ly_symbol2scm ("hairpin"))
233 cresc_
= make_spanner ("Hairpin", accepted_spanevents_drul_
[START
]->self_scm ());
236 Pointer_group_interface::add_grob (finished_cresc_
,
237 ly_symbol2scm ("adjacent-hairpins"),
240 Pointer_group_interface::add_grob (cresc_
,
241 ly_symbol2scm ("adjacent-hairpins"),
247 This is a convenient (and legacy) interface to TextSpanners
248 for use in (de)crescendi.
253 cresc_
= make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_
[START
]->self_scm ());
254 cresc_
->set_property ("style", s
);
255 context ()->set_property ((start_type
256 + "Spanner").c_str (), SCM_EOL
);
257 s
= get_property ((start_type
+ "Text").c_str ());
259 FIXME: use get_markup () to check type.
261 if (scm_is_string (s
) || scm_is_pair (s
))
263 cresc_
->set_property ("edge-text",
264 scm_cons (s
, scm_makfrom0str ("")));
265 context ()->set_property ((start_type
+ "Text").c_str (),
272 cresc_
->set_bound (LEFT
, script_
);
273 add_bound_item (line_spanner_
, cresc_
->get_bound (LEFT
));
276 Axis_group_interface::add_element (line_spanner_
, cresc_
);
282 Dynamic_engraver::stop_translation_timestep ()
284 if (!current_cresc_ev_
&& line_spanner_
)
286 assert (!finished_line_spanner_
);
287 finished_line_spanner_
= line_spanner_
;
293 if (cresc_
&& !cresc_
->get_bound (LEFT
))
295 cresc_
->set_bound (LEFT
, unsmob_grob (get_property ("currentMusicalColumn")));
296 add_bound_item (line_spanner_
, cresc_
->get_bound (LEFT
));
300 accepted_spanevents_drul_
[START
] = 0;
301 accepted_spanevents_drul_
[STOP
] = 0;
305 Dynamic_engraver::finalize ()
310 && !line_spanner_
->is_live ())
314 finished_line_spanner_
= line_spanner_
;
319 && !cresc_
->is_live ())
323 current_cresc_ev_
->origin ()->warning (_ ("unterminated (de)crescendo"));
330 Dynamic_engraver::typeset_all ()
334 bool use_bar
= to_boolean (get_property ("hairpinToBarline"))
335 && scm_is_string (get_property ("whichBar"))
339 if (!finished_cresc_
->get_bound (RIGHT
)
342 Grob
*column_bound
= unsmob_grob (use_bar
343 ? get_property ("currentCommandColumn")
344 : get_property ("currentMusicalColumn"));
346 finished_cresc_
->set_bound (RIGHT
, script_
350 if (finished_line_spanner_
)
351 add_bound_item (finished_line_spanner_
,
352 finished_cresc_
->get_bound (RIGHT
));
358 if (finished_line_spanner_
)
363 extend-spanner-over-elements (finished_line_spanner_);
365 but this is rather kludgy, since finished_line_spanner_
366 typically has a staff-symbol field set , extending it over the
371 Grob
*l
= finished_line_spanner_
->get_bound (LEFT
);
372 Grob
*r
= finished_line_spanner_
->get_bound (RIGHT
);
374 finished_line_spanner_
->set_bound (RIGHT
, l
);
376 finished_line_spanner_
->set_bound (LEFT
, r
);
380 This is a isolated dynamic apparently, and does not even have
381 any interesting support item.
383 Grob
*cc
= unsmob_grob (get_property ("currentMusicalColumn"));
384 Item
*ci
= dynamic_cast<Item
*> (cc
);
385 finished_line_spanner_
->set_bound (RIGHT
, ci
);
386 finished_line_spanner_
->set_bound (LEFT
, ci
);
389 finished_line_spanner_
= 0;
395 Dynamic_engraver::acknowledge_accidental (Grob_info info
)
398 Side_position_interface::add_support (line_spanner_
, info
.grob ());
403 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info
)
406 Side_position_interface::add_support (line_spanner_
, info
.grob ());
411 Dynamic_engraver::acknowledge_slur (Grob_info info
)
414 Side_position_interface::add_support (line_spanner_
, info
.grob ());
419 Dynamic_engraver::acknowledge_note_column (Grob_info info
)
425 /* Don't refill killed spanner */
426 && line_spanner_
->is_live ())
428 Side_position_interface::add_support (line_spanner_
, info
.grob ());
429 add_bound_item (line_spanner_
, dynamic_cast<Item
*> (info
.grob ()));
432 if (script_
&& !script_
->get_parent (X_AXIS
))
434 extract_grob_set (info
.grob (), "note-heads", heads
);
437 Grob
*head
= heads
[0];
438 script_
->set_parent (head
, X_AXIS
);
439 Self_alignment_interface::set_center_parent (script_
, X_AXIS
);
445 if (!cresc_
->get_bound (LEFT
))
447 cresc_
->set_bound (LEFT
, info
.grob ());
448 add_bound_item (line_spanner_
, cresc_
->get_bound (LEFT
));
452 if (finished_cresc_
&& !finished_cresc_
->get_bound (RIGHT
))
453 finished_cresc_
->set_bound (RIGHT
, info
.grob ());
457 Dynamic_engraver::acknowledge_script (Grob_info info
)
459 if (!line_spanner_
|| !script_
)
462 SCM p
= info
.grob ()->get_property ("script-priority");
467 DynamicText doesn't really have a script-priority field.
469 if (scm_is_number (p
)
471 < scm_to_int (script_
->get_property ("script-priority")))
472 Side_position_interface::add_support (line_spanner_
, info
.grob ());
475 ADD_ACKNOWLEDGER (Dynamic_engraver
, accidental
);
476 ADD_ACKNOWLEDGER (Dynamic_engraver
, script
);
477 ADD_ACKNOWLEDGER (Dynamic_engraver
, note_column
);
478 ADD_ACKNOWLEDGER (Dynamic_engraver
, slur
);
479 ADD_ACKNOWLEDGER (Dynamic_engraver
, stem_tremolo
);
481 ADD_TRANSLATOR (Dynamic_engraver
,
483 "This engraver creates hairpins, dynamic texts, and their vertical\n"
484 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
485 "which takes care of vertical positioning. ",
487 /* create */ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
488 /* accept */ "absolute-dynamic-event crescendo-event decrescendo-event",