2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2000--2012 Jan Nieuwenhuizen <janneke@gnu.org>,
5 Erik Sandberg <mandolaerik@gmail.com>
7 Chris Jackson <chris@fluffhouse.org.uk> - extended to support
10 LilyPond is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 LilyPond is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
24 #include "engraver.hh"
26 #include "axis-group-interface.hh"
28 #include "directional-element-interface.hh"
29 #include "international.hh"
30 #include "lily-guile.hh"
31 #include "note-column.hh"
32 #include "side-position-interface.hh"
33 #include "staff-symbol-referencer.hh"
34 #include "stream-event.hh"
35 #include "string-convert.hh"
40 #include "translator.icc"
48 * Junk hardcoded sustain/sostenuto/una_corda distinction;
49 Softcode using (list (sustain-event SustainPedal PianoPedalBracket) ... )
51 * Try to use same engraver for dynamics.
54 /* Ugh: This declaration is duplicated in piano-pedal-performer */
64 Static precalculated data (symbols and strings) for the different
67 struct Pedal_type_info
74 const char *pedal_c_str_;
78 event_class_sym_ = SCM_EOL;
80 strings_sym_ = SCM_EOL;
85 scm_gc_protect_object (event_class_sym_);
86 scm_gc_protect_object (style_sym_);
87 scm_gc_protect_object (strings_sym_);
93 const Pedal_type_info *type_;
96 Event for currently running pedal.
98 Stream_event *current_bracket_ev_;
101 Event for currently starting pedal, (necessary?
103 distinct from current_bracket_ev_, since current_bracket_ev_ only
104 necessary for brackets, not for text style.
106 Stream_event *start_ev_;
109 Events that were found in this timestep.
111 Drul_array<Stream_event *> event_drul_;
113 Spanner *bracket_; // A single portion of a pedal bracket
114 Spanner *finished_bracket_;
117 static Pedal_type_info pedal_types_[NUM_PEDAL_TYPES];
119 class Piano_pedal_engraver : public Engraver
122 TRANSLATOR_DECLARATIONS (Piano_pedal_engraver);
125 virtual void initialize ();
126 virtual void finalize ();
127 DECLARE_TRANSLATOR_LISTENER (sustain);
128 DECLARE_TRANSLATOR_LISTENER (una_corda);
129 DECLARE_TRANSLATOR_LISTENER (sostenuto);
130 void stop_translation_timestep ();
131 void process_music ();
134 Pedal_info info_list_[NUM_PEDAL_TYPES + 1];
136 void create_text_grobs (Pedal_info *p, bool);
137 void create_bracket_grobs (Pedal_info *p, bool);
138 void typeset_all (Pedal_info *p);
144 const char *names [NUM_PEDAL_TYPES];
145 names[SOSTENUTO] = "Sostenuto";
146 names[SUSTAIN] = "Sustain";
147 names[UNA_CORDA] = "UnaCorda";
149 for (int i = 0; i < NUM_PEDAL_TYPES; i++)
151 const char *name = names[i];
153 string base_name = name;
155 string base_ident = "";
158 for (cur_pos = 1; name[cur_pos]; cur_pos++)
159 if (isupper (name[cur_pos]))
161 base_ident = base_ident + String_convert::to_lower (string (name, prev_pos, cur_pos - prev_pos)) + "-";
164 base_ident += String_convert::to_lower (string (name, prev_pos, cur_pos - prev_pos));
167 be careful, as we don't want to loose references to the _sym_ members.
169 Pedal_type_info info;
170 info.event_class_sym_ = scm_from_locale_symbol ((base_ident + "-event").c_str ());
171 info.style_sym_ = scm_from_locale_symbol (("pedal" + base_name + "Style").c_str ());
172 info.strings_sym_ = scm_from_locale_symbol (("pedal" + base_name + "Strings").c_str ());
174 info.base_name_ = name;
175 info.pedal_c_str_ = strdup ((base_name + "Pedal").c_str ());
179 pedal_types_[i] = info;
183 ADD_SCM_INIT_FUNC (Piano_pedal_engraver_init_pedal_types_, init_pedal_types);
185 Piano_pedal_engraver::Piano_pedal_engraver ()
190 Piano_pedal_engraver::initialize ()
192 for (int i = 0; i < NUM_PEDAL_TYPES; i++)
194 Pedal_type_info *s = &pedal_types_[i];
195 Pedal_info *info = &info_list_[i];
200 info->finished_bracket_ = 0;
201 info->current_bracket_ev_ = 0;
202 info->event_drul_[START] = 0;
203 info->event_drul_[STOP] = 0;
206 info_list_[NUM_PEDAL_TYPES].type_ = 0;
209 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, sostenuto);
211 Piano_pedal_engraver::listen_sostenuto (Stream_event *ev)
213 Direction d = to_dir (ev->get_property ("span-direction"));
214 ASSIGN_EVENT_ONCE (info_list_[SOSTENUTO].event_drul_[d], ev);
217 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, sustain);
219 Piano_pedal_engraver::listen_sustain (Stream_event *ev)
221 Direction d = to_dir (ev->get_property ("span-direction"));
222 ASSIGN_EVENT_ONCE (info_list_[SUSTAIN].event_drul_[d], ev);
225 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, una_corda);
227 Piano_pedal_engraver::listen_una_corda (Stream_event *ev)
229 Direction d = to_dir (ev->get_property ("span-direction"));
230 ASSIGN_EVENT_ONCE (info_list_[UNA_CORDA].event_drul_[d], ev);
234 Piano_pedal_engraver::process_music ()
236 for (Pedal_info *p = info_list_; p->type_; p++)
238 if (p->event_drul_[STOP] || p->event_drul_[START])
240 /* Choose the appropriate grobs to add to the line spanner
241 These can be text items or text-spanners
245 ugh, code dup, should read grob to create from other
248 bracket: |_________/\____|
250 mixed: Ped. _____/\____|
253 SCM style = internal_get_property (p->type_->style_sym_);
255 bool mixed = style == ly_symbol2scm ("mixed");
256 bool bracket = (mixed
257 || style == ly_symbol2scm ("bracket"));
258 bool text = (style == ly_symbol2scm ("text")
261 if (text && !p->item_)
262 create_text_grobs (p, mixed);
264 create_bracket_grobs (p, mixed);
270 Piano_pedal_engraver::create_text_grobs (Pedal_info *p, bool mixed)
273 SCM strings = internal_get_property (p->type_->strings_sym_);
275 if (scm_ilength (strings) < 3)
277 Stream_event *m = p->event_drul_[START];
278 if (!m) m = p->event_drul_ [STOP];
280 string msg = _f ("expect 3 strings for piano pedals, found: %ld",
281 scm_ilength (strings));
283 m->origin ()->warning (msg);
290 if (p->event_drul_[STOP] && p->event_drul_[START])
295 p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
297 s = scm_cadr (strings);
298 p->start_ev_ = p->event_drul_[START];
301 else if (p->event_drul_[STOP])
306 p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
308 s = scm_caddr (strings);
312 else if (p->event_drul_[START])
314 p->start_ev_ = p->event_drul_[START];
315 s = scm_car (strings);
318 if (scm_is_string (s))
320 const char *propname = p->type_->pedal_c_str_;
322 p->item_ = make_item (propname, (p->event_drul_[START]
323 ? p->event_drul_[START]
324 : p->event_drul_[STOP])->self_scm ());
326 p->item_->set_property ("text", s);
331 p->event_drul_[START] = 0;
332 p->event_drul_[STOP] = 0;
337 Piano_pedal_engraver::create_bracket_grobs (Pedal_info *p, bool mixed)
339 if (!p->bracket_ && p->event_drul_[STOP])
341 string msg = _f ("cannot find start of piano pedal bracket: `%s'", p->type_->base_name_.c_str ());
342 p->event_drul_[STOP]->origin ()->warning (msg);
343 p->event_drul_[STOP] = 0;
346 if (p->event_drul_[STOP])
348 assert (!p->finished_bracket_);
350 Grob *cmc = unsmob_grob (get_property ("currentMusicalColumn"));
351 p->bracket_->set_bound (RIGHT, cmc);
354 Set properties so that the stencil-creating function will
355 know whether the right edge should be flared ___/
358 if (!p->event_drul_[START])
360 SCM flare = p->bracket_->get_property ("bracket-flare");
361 if (scm_is_pair (flare))
362 p->bracket_->set_property ("bracket-flare", scm_cons (scm_car (flare),
363 scm_from_double (0)));
366 p->finished_bracket_ = p->bracket_;
369 announce_end_grob (p->finished_bracket_, p->event_drul_[STOP]->self_scm ());
371 p->current_bracket_ev_ = 0;
374 if (p->event_drul_[START])
376 p->start_ev_ = p->event_drul_[START];
377 p->current_bracket_ev_ = p->event_drul_[START];
379 p->bracket_ = make_spanner ("PianoPedalBracket", p->event_drul_[START]->self_scm ());
382 Set properties so that the stencil-creating function will
383 know whether the left edge should be flared \___
386 if (!p->finished_bracket_)
388 SCM flare = p->bracket_->get_property ("bracket-flare");
389 p->bracket_->set_property ("bracket-flare", scm_cons (scm_from_double (0), scm_cdr (flare)));
392 /* Set this property for 'mixed style' pedals, Ped._______/\ ,
393 so the stencil function will shorten the ____ line by the length of the Ped. text.
399 Mixed style: Store a pointer to the preceding text for use in
400 calculating the length of the line
405 WTF is pedal-text not the bound of the object? --hwn
408 p->bracket_->set_object ("pedal-text", p->item_->self_scm ());
412 p->event_drul_[START] = 0;
413 p->event_drul_[STOP] = 0;
417 Piano_pedal_engraver::finalize ()
419 for (Pedal_info *p = info_list_; p->type_; p++)
422 && !p->bracket_->is_live ())
427 SCM cc = get_property ("currentCommandColumn");
428 Item *c = unsmob_item (cc);
429 p->bracket_->set_bound (RIGHT, c);
431 p->finished_bracket_ = p->bracket_;
440 Piano_pedal_engraver::stop_translation_timestep ()
442 for (Pedal_info *p = info_list_; p->type_; p++)
446 if (p->bracket_ && !p->bracket_->get_bound (LEFT))
448 Grob *cmc = unsmob_grob (get_property ("currentMusicalColumn"));
449 p->bracket_->set_bound (LEFT, cmc);
453 for (Pedal_info *p = info_list_; p->type_; p++)
455 p->event_drul_[STOP] = 0;
456 p->event_drul_[START] = 0;
461 Piano_pedal_engraver::typeset_all (Pedal_info *p)
466 if (p->finished_bracket_
467 && !p->finished_bracket_->is_live ())
468 p->finished_bracket_ = 0;
473 if (p->finished_bracket_)
475 Grob *r = p->finished_bracket_->get_bound (RIGHT);
477 p->finished_bracket_->set_bound (RIGHT, unsmob_grob (get_property ("currentMusicalColumn")));
479 p->finished_bracket_ = 0;
483 ADD_TRANSLATOR (Piano_pedal_engraver,
485 "Engrave piano pedal symbols and brackets.",
494 "currentCommandColumn "
495 "pedalSostenutoStrings "
496 "pedalSostenutoStyle "
497 "pedalSustainStrings "
499 "pedalUnaCordaStrings "
500 "pedalUnaCordaStyle ",