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