]> git.donarmstrong.com Git - lilypond.git/blob - lily/piano-pedal-engraver.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / piano-pedal-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2011 Jan Nieuwenhuizen <janneke@gnu.org>,
5                  Erik Sandberg <mandolaerik@gmail.com>
6
7   Chris Jackson <chris@fluffhouse.org.uk> - extended to support
8   bracketed pedals.
9
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.
14
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.
19
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/>.
22 */
23
24 #include "engraver.hh"
25
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"
39
40 #include "translator.icc"
41
42 #include <string.h>
43
44 /*
45   TODO:
46
47   * Junk hardcoded sustain/sostenuto/una_corda distinction;
48     Softcode using (list (sustain-event SustainPedal PianoPedalBracket) ... )
49
50   * Try to use same engraver for dynamics.
51 */
52
53 /* Ugh: This declaration is duplicated in piano-pedal-performer */
54 enum Pedal_type
55 {
56   SOSTENUTO,
57   SUSTAIN,
58   UNA_CORDA,
59   NUM_PEDAL_TYPES
60 };
61
62 /*
63   Static precalculated data (symbols and strings) for the different
64   pedal types
65 */
66 struct Pedal_type_info
67 {
68   string base_name_;
69   SCM event_class_sym_;
70   SCM style_sym_;
71   SCM strings_sym_;
72
73   const char *pedal_c_str_;
74
75   Pedal_type_info ()
76   {
77     event_class_sym_ = SCM_EOL;
78     style_sym_ = SCM_EOL;
79     strings_sym_ = SCM_EOL;
80     pedal_c_str_ = 0;
81   }
82   void protect ()
83   {
84     scm_gc_protect_object (event_class_sym_);
85     scm_gc_protect_object (style_sym_);
86     scm_gc_protect_object (strings_sym_);
87   }
88 };
89
90 struct Pedal_info
91 {
92   const Pedal_type_info *type_;
93
94   /*
95     Event for currently running pedal.
96   */
97   Stream_event *current_bracket_ev_;
98
99   /*
100     Event for currently starting pedal, (necessary?
101
102     distinct from current_bracket_ev_, since current_bracket_ev_ only
103     necessary for brackets, not for text style.
104   */
105   Stream_event *start_ev_;
106
107   /*
108     Events that were found in this timestep.
109   */
110   Drul_array<Stream_event *> event_drul_;
111   Item *item_;
112   Spanner *bracket_; // A single portion of a pedal bracket
113   Spanner *finished_bracket_;
114 };
115
116 static Pedal_type_info pedal_types_[NUM_PEDAL_TYPES];
117
118 class Piano_pedal_engraver : public Engraver
119 {
120 public:
121   TRANSLATOR_DECLARATIONS (Piano_pedal_engraver);
122
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 ();
132
133 private:
134   Pedal_info info_list_[NUM_PEDAL_TYPES + 1];
135
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);
139 };
140
141 static void
142 init_pedal_types ()
143 {
144   const char *names [NUM_PEDAL_TYPES];
145   names[SOSTENUTO] = "Sostenuto";
146   names[SUSTAIN] = "Sustain";
147   names[UNA_CORDA] = "UnaCorda";
148
149   for (int i = 0; i < NUM_PEDAL_TYPES; i++)
150     {
151       const char *name = names[i];
152       /* FooBar */
153       string base_name = name;
154       /* foo-bar */
155       string base_ident = "";
156       int prev_pos = 0;
157       int cur_pos;
158       for (cur_pos = 1; name[cur_pos]; cur_pos++)
159         if (isupper (name[cur_pos]))
160           {
161             base_ident = base_ident + String_convert::to_lower (string (name, prev_pos, cur_pos - prev_pos)) + "-";
162             prev_pos = cur_pos;
163           }
164       base_ident += String_convert::to_lower (string (name, prev_pos, cur_pos - prev_pos));
165
166       /*
167         be careful, as we don't want to loose references to the _sym_ members.
168        */
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 ());
173
174       info.base_name_ = name;
175       info.pedal_c_str_ = strdup ((base_name + "Pedal").c_str ());
176
177       info.protect ();
178
179       pedal_types_[i] = info;
180     }
181 }
182
183 ADD_SCM_INIT_FUNC (Piano_pedal_engraver_init_pedal_types_, init_pedal_types);
184
185 Piano_pedal_engraver::Piano_pedal_engraver ()
186 {
187 }
188
189 void
190 Piano_pedal_engraver::initialize ()
191 {
192   for (int i = 0; i < NUM_PEDAL_TYPES; i++)
193     {
194       Pedal_type_info *s = &pedal_types_[i];
195       Pedal_info *info = &info_list_[i];
196
197       info->type_ = s;
198       info->item_ = 0;
199       info->bracket_ = 0;
200       info->finished_bracket_ = 0;
201       info->current_bracket_ev_ = 0;
202       info->event_drul_[START] = 0;
203       info->event_drul_[STOP] = 0;
204       info->start_ev_ = 0;
205     }
206   info_list_[NUM_PEDAL_TYPES].type_ = 0;
207 }
208
209 /*
210   Urg: Code dup
211   I'm a script
212 */
213 void
214 Piano_pedal_engraver::acknowledge_note_column (Grob_info info)
215 {
216   for (Pedal_info *p = info_list_; p->type_; p++)
217     {
218       if (p->bracket_)
219         add_bound_item (p->bracket_, info.grob ());
220       if (p->finished_bracket_)
221         add_bound_item (p->finished_bracket_, info.grob ());
222     }
223 }
224
225 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, sostenuto);
226 void
227 Piano_pedal_engraver::listen_sostenuto (Stream_event *ev)
228 {
229   Direction d = to_dir (ev->get_property ("span-direction"));
230   ASSIGN_EVENT_ONCE (info_list_[SOSTENUTO].event_drul_[d], ev);
231 }
232
233 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, sustain);
234 void
235 Piano_pedal_engraver::listen_sustain (Stream_event *ev)
236 {
237   Direction d = to_dir (ev->get_property ("span-direction"));
238   ASSIGN_EVENT_ONCE (info_list_[SUSTAIN].event_drul_[d], ev);
239 }
240
241 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, una_corda);
242 void
243 Piano_pedal_engraver::listen_una_corda (Stream_event *ev)
244 {
245   Direction d = to_dir (ev->get_property ("span-direction"));
246   ASSIGN_EVENT_ONCE (info_list_[UNA_CORDA].event_drul_[d], ev);
247 }
248
249 void
250 Piano_pedal_engraver::process_music ()
251 {
252   for (Pedal_info *p = info_list_; p->type_; p++)
253     {
254       if (p->event_drul_[STOP] || p->event_drul_[START])
255         {
256           /* Choose the appropriate grobs to add to the line spanner
257              These can be text items or text-spanners
258           */
259
260           /*
261             ugh, code dup, should read grob to create from other
262             property.
263
264             bracket: |_________/\____|
265             text:    Ped.     *Ped.  *
266             mixed:   Ped. _____/\____|
267           */
268
269           SCM style = internal_get_property (p->type_->style_sym_);
270
271           bool mixed = style == ly_symbol2scm ("mixed");
272           bool bracket = (mixed
273                           || style == ly_symbol2scm ("bracket"));
274           bool text = (style == ly_symbol2scm ("text")
275                        || mixed);
276
277           if (text && !p->item_)
278             create_text_grobs (p, mixed);
279           if (bracket)
280             create_bracket_grobs (p, mixed);
281         }
282     }
283 }
284
285 void
286 Piano_pedal_engraver::create_text_grobs (Pedal_info *p, bool mixed)
287 {
288   SCM s = SCM_EOL;
289   SCM strings = internal_get_property (p->type_->strings_sym_);
290
291   if (scm_ilength (strings) < 3)
292     {
293       Stream_event *m = p->event_drul_[START];
294       if (!m) m = p->event_drul_ [STOP];
295
296       string msg = _f ("expect 3 strings for piano pedals, found: %ld",
297                        scm_ilength (strings));
298       if (m)
299         m->origin ()->warning (msg);
300       else
301         warning (msg);
302
303       return;
304     }
305
306   if (p->event_drul_[STOP] && p->event_drul_[START])
307     {
308       if (!mixed)
309         {
310           if (!p->start_ev_)
311             p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
312           else
313             s = scm_cadr (strings);
314           p->start_ev_ = p->event_drul_[START];
315         }
316     }
317   else if (p->event_drul_[STOP])
318     {
319       if (!mixed)
320         {
321           if (!p->start_ev_)
322             p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
323           else
324             s = scm_caddr (strings);
325           p->start_ev_ = 0;
326         }
327     }
328   else if (p->event_drul_[START])
329     {
330       p->start_ev_ = p->event_drul_[START];
331       s = scm_car (strings);
332     }
333
334   if (scm_is_string (s))
335     {
336       const char *propname = p->type_->pedal_c_str_;
337
338       p->item_ = make_item (propname, (p->event_drul_[START]
339                                        ? p->event_drul_[START]
340                                        : p->event_drul_[STOP])->self_scm ());
341
342       p->item_->set_property ("text", s);
343     }
344
345   if (!mixed)
346     {
347       p->event_drul_[START] = 0;
348       p->event_drul_[STOP] = 0;
349     }
350 }
351
352 void
353 Piano_pedal_engraver::create_bracket_grobs (Pedal_info *p, bool mixed)
354 {
355   if (!p->bracket_ && p->event_drul_[STOP])
356     {
357       string msg = _f ("cannot find start of piano pedal bracket: `%s'", p->type_->base_name_.c_str ());
358       p->event_drul_[STOP]->origin ()->warning (msg);
359       p->event_drul_[STOP] = 0;
360     }
361
362   if (p->event_drul_[STOP])
363     {
364       assert (!p->finished_bracket_);
365
366       Grob *cmc = unsmob_grob (get_property ("currentMusicalColumn"));
367
368       if (!p->bracket_->get_bound (RIGHT))
369         p->bracket_->set_bound (RIGHT, cmc);
370
371       /*
372         Set properties so that the stencil-creating function will
373         know whether the right edge should be flared ___/
374       */
375
376       if (!p->event_drul_[START])
377         {
378           SCM flare = p->bracket_->get_property ("bracket-flare");
379           if (scm_is_pair (flare))
380             p->bracket_->set_property ("bracket-flare", scm_cons (scm_car (flare),
381                                                                   scm_from_double (0)));
382         }
383
384       p->finished_bracket_ = p->bracket_;
385       p->bracket_ = 0;
386
387       announce_end_grob (p->finished_bracket_, p->event_drul_[STOP]->self_scm ());
388
389       p->current_bracket_ev_ = 0;
390     }
391
392   if (p->event_drul_[START])
393     {
394       p->start_ev_ = p->event_drul_[START];
395       p->current_bracket_ev_ = p->event_drul_[START];
396
397       p->bracket_ = make_spanner ("PianoPedalBracket", p->event_drul_[START]->self_scm ());
398
399       /*
400         Set properties so that the stencil-creating function will
401         know whether the left edge should be flared \___
402       */
403
404       if (!p->finished_bracket_)
405         {
406           SCM flare = p->bracket_->get_property ("bracket-flare");
407           p->bracket_->set_property ("bracket-flare", scm_cons (scm_from_double (0), scm_cdr (flare)));
408         }
409
410       /* Set this property for 'mixed style' pedals,    Ped._______/\ ,
411          so the stencil function will shorten the ____ line by the length of the Ped. text.
412       */
413
414       if (mixed)
415         {
416           /*
417             Mixed style: Store a pointer to the preceding text for use in
418             calculating the length of the line
419
420
421             TODO:
422
423             WTF is pedal-text not the bound of the object? --hwn
424           */
425           if (p->item_)
426             p->bracket_->set_object ("pedal-text", p->item_->self_scm ());
427         }
428     }
429
430   p->event_drul_[START] = 0;
431   p->event_drul_[STOP] = 0;
432 }
433
434 void
435 Piano_pedal_engraver::finalize ()
436 {
437   for (Pedal_info *p = info_list_; p->type_; p++)
438     {
439       if (p->bracket_
440           && !p->bracket_->is_live ())
441         p->bracket_ = 0;
442
443       if (p->bracket_)
444         {
445           SCM cc = get_property ("currentCommandColumn");
446           Item *c = unsmob_item (cc);
447           p->bracket_->set_bound (RIGHT, c);
448
449           p->finished_bracket_ = p->bracket_;
450           p->bracket_ = 0;
451           typeset_all (p);
452         }
453
454     }
455 }
456
457 void
458 Piano_pedal_engraver::stop_translation_timestep ()
459 {
460   for (Pedal_info *p = info_list_; p->type_; p++)
461     {
462
463       typeset_all (p);
464       if (p->bracket_ && !p->bracket_->get_bound (LEFT))
465         {
466           Grob *cmc = unsmob_grob (get_property ("currentMusicalColumn"));
467
468           if (!p->bracket_->get_bound (LEFT))
469             p->bracket_->set_bound (LEFT, cmc);
470         }
471     }
472
473   for (Pedal_info *p = info_list_; p->type_; p++)
474     {
475       p->event_drul_[STOP] = 0;
476       p->event_drul_[START] = 0;
477     }
478 }
479
480 void
481 Piano_pedal_engraver::typeset_all (Pedal_info *p)
482 {
483   /*
484     Handle suicide.
485   */
486   if (p->finished_bracket_
487       && !p->finished_bracket_->is_live ())
488     p->finished_bracket_ = 0;
489
490   if (p->item_)
491     p->item_ = 0;
492
493   if (p->finished_bracket_)
494     {
495       Grob *r = p->finished_bracket_->get_bound (RIGHT);
496       if (!r)
497         p->finished_bracket_->set_bound (RIGHT, unsmob_grob (get_property ("currentMusicalColumn")));
498
499       p->finished_bracket_ = 0;
500     }
501 }
502
503 ADD_ACKNOWLEDGER (Piano_pedal_engraver, note_column);
504
505 ADD_TRANSLATOR (Piano_pedal_engraver,
506                 /* doc */
507                 "Engrave piano pedal symbols and brackets.",
508
509                 /* create */
510                 "PianoPedalBracket "
511                 "SostenutoPedal "
512                 "SustainPedal "
513                 "UnaCordaPedal ",
514
515                 /* read */
516                 "currentCommandColumn "
517                 "pedalSostenutoStrings "
518                 "pedalSostenutoStyle "
519                 "pedalSustainStrings "
520                 "pedalSustainStyle "
521                 "pedalUnaCordaStrings "
522                 "pedalUnaCordaStyle ",
523
524                 /* write */
525                 ""
526                );