2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2000--2015 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"
49 * Junk hardcoded sustain/sostenuto/una_corda distinction;
50 Softcode using (list (sustain-event SustainPedal PianoPedalBracket) ... )
52 * Try to use same engraver for dynamics.
55 /* Ugh: This declaration is duplicated in piano-pedal-performer */
65 Static precalculated data (symbols and strings) for the different
68 struct Pedal_type_info
75 const char *pedal_c_str_;
79 event_class_sym_ = SCM_EOL;
81 strings_sym_ = SCM_EOL;
86 scm_gc_protect_object (event_class_sym_);
87 scm_gc_protect_object (style_sym_);
88 scm_gc_protect_object (strings_sym_);
94 const Pedal_type_info *type_;
97 Event for currently running pedal.
99 Stream_event *current_bracket_ev_;
102 Event for currently starting pedal, (necessary?
104 distinct from current_bracket_ev_, since current_bracket_ev_ only
105 necessary for brackets, not for text style.
107 Stream_event *start_ev_;
110 Events that were found in this timestep.
112 Drul_array<Stream_event *> event_drul_;
114 Spanner *bracket_; // A single portion of a pedal bracket
115 Spanner *finished_bracket_;
118 static Pedal_type_info pedal_types_[NUM_PEDAL_TYPES];
120 class Piano_pedal_engraver : public Engraver
123 TRANSLATOR_DECLARATIONS (Piano_pedal_engraver);
126 virtual void initialize ();
127 virtual void finalize ();
128 DECLARE_TRANSLATOR_LISTENER (sustain);
129 DECLARE_TRANSLATOR_LISTENER (una_corda);
130 DECLARE_TRANSLATOR_LISTENER (sostenuto);
131 void stop_translation_timestep ();
132 void process_music ();
135 Pedal_info info_list_[NUM_PEDAL_TYPES + 1];
137 void create_text_grobs (Pedal_info *p, bool);
138 void create_bracket_grobs (Pedal_info *p, bool);
139 void typeset_all (Pedal_info *p);
145 const char *names [NUM_PEDAL_TYPES];
146 names[SOSTENUTO] = "Sostenuto";
147 names[SUSTAIN] = "Sustain";
148 names[UNA_CORDA] = "UnaCorda";
150 for (int i = 0; i < NUM_PEDAL_TYPES; i++)
152 const char *name = names[i];
154 string base_name = name;
156 string base_ident = "";
159 for (cur_pos = 1; name[cur_pos]; cur_pos++)
160 if (isupper (name[cur_pos]))
162 base_ident = base_ident + String_convert::to_lower (string (name, prev_pos, cur_pos - prev_pos)) + "-";
165 base_ident += String_convert::to_lower (string (name, prev_pos, cur_pos - prev_pos));
168 be careful, as we don't want to loose references to the _sym_ members.
170 Pedal_type_info info;
171 info.event_class_sym_ = scm_from_ascii_symbol ((base_ident + "-event").c_str ());
172 info.style_sym_ = scm_from_ascii_symbol (("pedal" + base_name + "Style").c_str ());
173 info.strings_sym_ = scm_from_ascii_symbol (("pedal" + base_name + "Strings").c_str ());
175 info.base_name_ = name;
176 info.pedal_c_str_ = strdup ((base_name + "Pedal").c_str ());
180 pedal_types_[i] = info;
184 ADD_SCM_INIT_FUNC (Piano_pedal_engraver_init_pedal_types_, init_pedal_types);
186 Piano_pedal_engraver::Piano_pedal_engraver ()
191 Piano_pedal_engraver::initialize ()
193 for (int i = 0; i < NUM_PEDAL_TYPES; i++)
195 Pedal_type_info *s = &pedal_types_[i];
196 Pedal_info *info = &info_list_[i];
201 info->finished_bracket_ = 0;
202 info->current_bracket_ev_ = 0;
203 info->event_drul_[START] = 0;
204 info->event_drul_[STOP] = 0;
207 info_list_[NUM_PEDAL_TYPES].type_ = 0;
210 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, sostenuto);
212 Piano_pedal_engraver::listen_sostenuto (Stream_event *ev)
214 Direction d = to_dir (ev->get_property ("span-direction"));
215 ASSIGN_EVENT_ONCE (info_list_[SOSTENUTO].event_drul_[d], ev);
218 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, sustain);
220 Piano_pedal_engraver::listen_sustain (Stream_event *ev)
222 Direction d = to_dir (ev->get_property ("span-direction"));
223 ASSIGN_EVENT_ONCE (info_list_[SUSTAIN].event_drul_[d], ev);
226 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, una_corda);
228 Piano_pedal_engraver::listen_una_corda (Stream_event *ev)
230 Direction d = to_dir (ev->get_property ("span-direction"));
231 ASSIGN_EVENT_ONCE (info_list_[UNA_CORDA].event_drul_[d], ev);
235 Piano_pedal_engraver::process_music ()
237 for (Pedal_info *p = info_list_; p->type_; p++)
239 if (p->event_drul_[STOP] || p->event_drul_[START])
241 /* Choose the appropriate grobs to add to the line spanner
242 These can be text items or text-spanners
246 ugh, code dup, should read grob to create from other
249 bracket: |_________/\____|
251 mixed: Ped. _____/\____|
254 SCM style = get_property (p->type_->style_sym_);
256 bool mixed = scm_is_eq (style, ly_symbol2scm ("mixed"));
257 bool bracket = (mixed
258 || scm_is_eq (style, ly_symbol2scm ("bracket")));
260 || scm_is_eq (style, ly_symbol2scm ("text")));
262 if (text && !p->item_)
263 create_text_grobs (p, mixed);
265 create_bracket_grobs (p, mixed);
271 Piano_pedal_engraver::create_text_grobs (Pedal_info *p, bool mixed)
274 SCM strings = get_property (p->type_->strings_sym_);
276 if (scm_ilength (strings) < 3)
278 Stream_event *m = p->event_drul_[START];
279 if (!m) m = p->event_drul_ [STOP];
281 string msg = _f ("expect 3 strings for piano pedals, found: %ld",
282 scm_ilength (strings));
284 m->origin ()->warning (msg);
291 if (p->event_drul_[STOP] && p->event_drul_[START])
296 p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
298 s = scm_cadr (strings);
299 p->start_ev_ = p->event_drul_[START];
302 else if (p->event_drul_[STOP])
307 p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
309 s = scm_caddr (strings);
313 else if (p->event_drul_[START])
315 p->start_ev_ = p->event_drul_[START];
316 s = scm_car (strings);
319 if (scm_is_string (s))
321 const char *propname = p->type_->pedal_c_str_;
323 p->item_ = make_item (propname, (p->event_drul_[START]
324 ? p->event_drul_[START]
325 : p->event_drul_[STOP])->self_scm ());
327 p->item_->set_property ("text", s);
332 p->event_drul_[START] = 0;
333 p->event_drul_[STOP] = 0;
338 Piano_pedal_engraver::create_bracket_grobs (Pedal_info *p, bool mixed)
340 if (!p->bracket_ && p->event_drul_[STOP])
342 string msg = _f ("cannot find start of piano pedal bracket: `%s'", p->type_->base_name_.c_str ());
343 p->event_drul_[STOP]->origin ()->warning (msg);
344 p->event_drul_[STOP] = 0;
347 if (p->event_drul_[STOP])
349 assert (!p->finished_bracket_);
351 Grob *cmc = unsmob<Grob> (get_property ("currentMusicalColumn"));
352 p->bracket_->set_bound (RIGHT, cmc);
355 Set properties so that the stencil-creating function will
356 know whether the right edge should be flared ___/
359 if (!p->event_drul_[START])
361 SCM flare = p->bracket_->get_property ("bracket-flare");
362 if (scm_is_pair (flare))
363 p->bracket_->set_property ("bracket-flare", scm_cons (scm_car (flare),
364 scm_from_double (0)));
367 p->finished_bracket_ = p->bracket_;
370 announce_end_grob (p->finished_bracket_, p->event_drul_[STOP]->self_scm ());
372 p->current_bracket_ev_ = 0;
375 if (p->event_drul_[START])
377 p->start_ev_ = p->event_drul_[START];
378 p->current_bracket_ev_ = p->event_drul_[START];
380 p->bracket_ = make_spanner ("PianoPedalBracket", p->event_drul_[START]->self_scm ());
383 Set properties so that the stencil-creating function will
384 know whether the left edge should be flared \___
387 if (!p->finished_bracket_)
389 SCM flare = p->bracket_->get_property ("bracket-flare");
390 p->bracket_->set_property ("bracket-flare", scm_cons (scm_from_double (0), scm_cdr (flare)));
393 /* Set this property for 'mixed style' pedals, Ped._______/\ ,
394 so the stencil function will shorten the ____ line by the length of the Ped. text.
400 Mixed style: Store a pointer to the preceding text for use in
401 calculating the length of the line
406 WTF is pedal-text not the bound of the object? --hwn
409 p->bracket_->set_object ("pedal-text", p->item_->self_scm ());
413 p->event_drul_[START] = 0;
414 p->event_drul_[STOP] = 0;
418 Piano_pedal_engraver::finalize ()
420 for (Pedal_info *p = info_list_; p->type_; p++)
423 && !p->bracket_->is_live ())
428 SCM cc = get_property ("currentCommandColumn");
429 Item *c = unsmob<Item> (cc);
430 p->bracket_->set_bound (RIGHT, c);
432 p->finished_bracket_ = p->bracket_;
441 Piano_pedal_engraver::stop_translation_timestep ()
443 for (Pedal_info *p = info_list_; p->type_; p++)
447 if (p->bracket_ && !p->bracket_->get_bound (LEFT))
449 Grob *cmc = unsmob<Grob> (get_property ("currentMusicalColumn"));
450 p->bracket_->set_bound (LEFT, cmc);
454 for (Pedal_info *p = info_list_; p->type_; p++)
456 p->event_drul_[STOP] = 0;
457 p->event_drul_[START] = 0;
462 Piano_pedal_engraver::typeset_all (Pedal_info *p)
467 if (p->finished_bracket_
468 && !p->finished_bracket_->is_live ())
469 p->finished_bracket_ = 0;
474 if (p->finished_bracket_)
476 Grob *r = p->finished_bracket_->get_bound (RIGHT);
478 p->finished_bracket_->set_bound (RIGHT, unsmob<Grob> (get_property ("currentMusicalColumn")));
480 p->finished_bracket_ = 0;
484 ADD_TRANSLATOR (Piano_pedal_engraver,
486 "Engrave piano pedal symbols and brackets.",
495 "currentCommandColumn "
496 "pedalSostenutoStrings "
497 "pedalSostenutoStyle "
498 "pedalSustainStrings "
500 "pedalUnaCordaStrings "
501 "pedalUnaCordaStyle ",