Merge remote branch 'origin' into release/unstable
[lilypond/patrick.git] / lily / piano-pedal-engraver.cc
blob43666efbdd9346b0f18e3eabed675bbe68867227
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2000--2011 Jan Nieuwenhuizen <janneke@gnu.org>,
5 Erik Sandberg <mandolaerik@gmail.com>
7 Chris Jackson <chris@fluffhouse.org.uk> - extended to support
8 bracketed pedals.
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"
27 #include "context.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"
36 #include "warn.hh"
37 #include "spanner.hh"
38 #include "item.hh"
40 #include "translator.icc"
42 #include <string.h>
45 TODO:
47 * Junk hardcoded sustain/sostenuto/una_corda distinction;
48 Softcode using (list (sustain-event SustainPedal PianoPedalBracket) ... )
50 * Try to use same engraver for dynamics.
54 /* Ugh: This declaration is duplicated in piano-pedal-performer */
55 enum Pedal_type {
56 SOSTENUTO,
57 SUSTAIN,
58 UNA_CORDA,
59 NUM_PEDAL_TYPES
63 Static precalculated data (symbols and strings) for the different
64 pedal types
66 struct Pedal_type_info
68 string base_name_;
69 SCM event_class_sym_;
70 SCM style_sym_;
71 SCM strings_sym_;
73 const char *pedal_c_str_;
75 Pedal_type_info ()
77 event_class_sym_ = SCM_EOL;
78 style_sym_ = SCM_EOL;
79 strings_sym_ = SCM_EOL;
80 pedal_c_str_ = 0;
82 void protect ()
84 scm_gc_protect_object (event_class_sym_);
85 scm_gc_protect_object (style_sym_);
86 scm_gc_protect_object (strings_sym_);
90 struct Pedal_info
92 const Pedal_type_info *type_;
95 Event for currently running pedal.
97 Stream_event *current_bracket_ev_;
100 Event for currently starting pedal, (necessary?
102 distinct from current_bracket_ev_, since current_bracket_ev_ only
103 necessary for brackets, not for text style.
105 Stream_event *start_ev_;
108 Events that were found in this timestep.
110 Drul_array<Stream_event *> event_drul_;
111 Item *item_;
112 Spanner *bracket_; // A single portion of a pedal bracket
113 Spanner *finished_bracket_;
116 static Pedal_type_info pedal_types_[NUM_PEDAL_TYPES];
118 class Piano_pedal_engraver : public Engraver
120 public:
121 TRANSLATOR_DECLARATIONS (Piano_pedal_engraver);
123 protected:
124 virtual void initialize ();
125 virtual void finalize ();
126 DECLARE_TRANSLATOR_LISTENER (sustain);
127 DECLARE_TRANSLATOR_LISTENER (una_corda);
128 DECLARE_TRANSLATOR_LISTENER (sostenuto);
129 DECLARE_ACKNOWLEDGER (note_column);
130 void stop_translation_timestep ();
131 void process_music ();
133 private:
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);
142 static void
143 init_pedal_types ()
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];
153 /* FooBar */
154 string base_name = name;
155 /* foo-bar */
156 string base_ident = "";
157 int prev_pos=0;
158 int cur_pos;
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)) + "-";
163 prev_pos = cur_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_locale_symbol ((base_ident + "-event").c_str ());
172 info.style_sym_ = scm_from_locale_symbol (("pedal" + base_name + "Style").c_str ());
173 info.strings_sym_ = scm_from_locale_symbol (("pedal" + base_name + "Strings").c_str ());
175 info.base_name_ = name;
176 info.pedal_c_str_ = strdup ((base_name + "Pedal").c_str ());
178 info.protect ();
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 ()
190 void
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];
198 info->type_ = s;
199 info->item_ = 0;
200 info->bracket_ = 0;
201 info->finished_bracket_ = 0;
202 info->current_bracket_ev_ = 0;
203 info->event_drul_[START] = 0;
204 info->event_drul_[STOP] = 0;
205 info->start_ev_ = 0;
207 info_list_[NUM_PEDAL_TYPES].type_ = 0;
212 Urg: Code dup
213 I'm a script
215 void
216 Piano_pedal_engraver::acknowledge_note_column (Grob_info info)
218 for (Pedal_info *p = info_list_; p->type_; p++)
220 if (p->bracket_)
221 add_bound_item (p->bracket_, info.grob ());
222 if (p->finished_bracket_)
223 add_bound_item (p->finished_bracket_, info.grob ());
227 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, sostenuto);
228 void
229 Piano_pedal_engraver::listen_sostenuto (Stream_event *ev)
231 Direction d = to_dir (ev->get_property ("span-direction"));
232 ASSIGN_EVENT_ONCE (info_list_[SOSTENUTO].event_drul_[d], ev);
235 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, sustain);
236 void
237 Piano_pedal_engraver::listen_sustain (Stream_event *ev)
239 Direction d = to_dir (ev->get_property ("span-direction"));
240 ASSIGN_EVENT_ONCE (info_list_[SUSTAIN].event_drul_[d], ev);
243 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, una_corda);
244 void
245 Piano_pedal_engraver::listen_una_corda (Stream_event *ev)
247 Direction d = to_dir (ev->get_property ("span-direction"));
248 ASSIGN_EVENT_ONCE (info_list_[UNA_CORDA].event_drul_[d], ev);
251 void
252 Piano_pedal_engraver::process_music ()
254 for (Pedal_info *p = info_list_; p->type_; p++)
256 if (p->event_drul_[STOP] || p->event_drul_[START])
258 /* Choose the appropriate grobs to add to the line spanner
259 These can be text items or text-spanners
263 ugh, code dup, should read grob to create from other
264 property.
266 bracket: |_________/\____|
267 text: Ped. *Ped. *
268 mixed: Ped. _____/\____|
271 SCM style = internal_get_property (p->type_->style_sym_);
273 bool mixed = style == ly_symbol2scm ("mixed");
274 bool bracket = (mixed
275 || style == ly_symbol2scm ("bracket"));
276 bool text = (style == ly_symbol2scm ("text")
277 || mixed);
279 if (text && !p->item_)
280 create_text_grobs (p, mixed);
281 if (bracket)
282 create_bracket_grobs (p, mixed);
287 void
288 Piano_pedal_engraver::create_text_grobs (Pedal_info *p, bool mixed)
290 SCM s = SCM_EOL;
291 SCM strings = internal_get_property (p->type_->strings_sym_);
293 if (scm_ilength (strings) < 3)
295 Stream_event *m = p->event_drul_[START];
296 if (!m) m = p->event_drul_ [STOP];
298 string msg = _f ("expect 3 strings for piano pedals, found: %ld",
299 scm_ilength (strings));
300 if (m)
301 m->origin ()->warning (msg);
302 else
303 warning (msg);
305 return;
308 if (p->event_drul_[STOP] && p->event_drul_[START])
310 if (!mixed)
312 if (!p->start_ev_)
313 p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
314 else
315 s = scm_cadr (strings);
316 p->start_ev_ = p->event_drul_[START];
319 else if (p->event_drul_[STOP])
321 if (!mixed)
323 if (!p->start_ev_)
324 p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
325 else
326 s = scm_caddr (strings);
327 p->start_ev_ = 0;
330 else if (p->event_drul_[START])
332 p->start_ev_ = p->event_drul_[START];
333 s = scm_car (strings);
336 if (scm_is_string (s))
338 const char *propname = p->type_->pedal_c_str_;
340 p->item_ = make_item (propname, (p->event_drul_[START]
341 ? p->event_drul_[START]
342 : p->event_drul_[STOP])->self_scm ());
344 p->item_->set_property ("text", s);
347 if (!mixed)
349 p->event_drul_[START] = 0;
350 p->event_drul_[STOP] = 0;
354 void
355 Piano_pedal_engraver::create_bracket_grobs (Pedal_info *p, bool mixed)
357 if (!p->bracket_ && p->event_drul_[STOP])
359 string msg = _f ("cannot find start of piano pedal bracket: `%s'", p->type_->base_name_.c_str ());
360 p->event_drul_[STOP]->origin ()->warning (msg);
361 p->event_drul_[STOP] = 0;
364 if (p->event_drul_[STOP])
366 assert (!p->finished_bracket_);
368 Grob *cmc = unsmob_grob (get_property ("currentMusicalColumn"));
370 if (!p->bracket_->get_bound (RIGHT))
371 p->bracket_->set_bound (RIGHT, cmc);
374 Set properties so that the stencil-creating function will
375 know whether the right edge should be flared ___/
378 if (!p->event_drul_[START])
380 SCM flare = p->bracket_->get_property ("bracket-flare");
381 if (scm_is_pair (flare))
382 p->bracket_->set_property ("bracket-flare", scm_cons (scm_car (flare),
383 scm_from_double (0)));
386 p->finished_bracket_ = p->bracket_;
387 p->bracket_ = 0;
389 announce_end_grob (p->finished_bracket_, p->event_drul_[STOP]->self_scm ());
391 p->current_bracket_ev_ = 0;
394 if (p->event_drul_[START])
396 p->start_ev_ = p->event_drul_[START];
397 p->current_bracket_ev_ = p->event_drul_[START];
399 p->bracket_ = make_spanner ("PianoPedalBracket", p->event_drul_[START]->self_scm ());
402 Set properties so that the stencil-creating function will
403 know whether the left edge should be flared \___
406 if (!p->finished_bracket_)
408 SCM flare = p->bracket_->get_property ("bracket-flare");
409 p->bracket_->set_property ("bracket-flare", scm_cons (scm_from_double (0), scm_cdr (flare)));
412 /* Set this property for 'mixed style' pedals, Ped._______/\ ,
413 so the stencil function will shorten the ____ line by the length of the Ped. text.
416 if (mixed)
419 Mixed style: Store a pointer to the preceding text for use in
420 calculating the length of the line
423 TODO:
425 WTF is pedal-text not the bound of the object? --hwn
427 if (p->item_)
428 p->bracket_->set_object ("pedal-text", p->item_->self_scm ());
432 p->event_drul_[START] = 0;
433 p->event_drul_[STOP] = 0;
436 void
437 Piano_pedal_engraver::finalize ()
439 for (Pedal_info *p = info_list_; p->type_; p++)
441 if (p->bracket_
442 && !p->bracket_->is_live ())
443 p->bracket_ = 0;
445 if (p->bracket_)
447 SCM cc = get_property ("currentCommandColumn");
448 Item *c = unsmob_item (cc);
449 p->bracket_->set_bound (RIGHT, c);
451 p->finished_bracket_ = p->bracket_;
452 p->bracket_ = 0;
453 typeset_all (p);
459 void
460 Piano_pedal_engraver::stop_translation_timestep ()
462 for (Pedal_info *p = info_list_; p->type_; p++)
465 typeset_all (p);
466 if (p->bracket_ && !p->bracket_->get_bound (LEFT))
468 Grob *cmc = unsmob_grob (get_property ("currentMusicalColumn"));
470 if (!p->bracket_->get_bound (LEFT))
471 p->bracket_->set_bound (LEFT, cmc);
475 for (Pedal_info *p = info_list_; p->type_; p++)
477 p->event_drul_[STOP] = 0;
478 p->event_drul_[START] = 0;
482 void
483 Piano_pedal_engraver::typeset_all (Pedal_info *p)
486 Handle suicide.
488 if (p->finished_bracket_
489 && !p->finished_bracket_->is_live ())
490 p->finished_bracket_ = 0;
492 if (p->item_)
493 p->item_ = 0;
495 if (p->finished_bracket_)
497 Grob *r = p->finished_bracket_->get_bound (RIGHT);
498 if (!r)
499 p->finished_bracket_->set_bound (RIGHT, unsmob_grob (get_property ("currentMusicalColumn")));
501 p->finished_bracket_ = 0;
505 ADD_ACKNOWLEDGER (Piano_pedal_engraver, note_column);
507 ADD_TRANSLATOR (Piano_pedal_engraver,
508 /* doc */
509 "Engrave piano pedal symbols and brackets.",
511 /* create */
512 "PianoPedalBracket "
513 "SostenutoPedal "
514 "SustainPedal "
515 "UnaCordaPedal ",
517 /* read */
518 "currentCommandColumn "
519 "pedalSostenutoStrings "
520 "pedalSostenutoStyle "
521 "pedalSustainStrings "
522 "pedalSustainStyle "
523 "pedalUnaCordaStrings "
524 "pedalUnaCordaStyle ",
526 /* write */