]> git.donarmstrong.com Git - lilypond.git/blob - lily/piano-pedal-engraver.cc
*** empty log message ***
[lilypond.git] / lily / piano-pedal-engraver.cc
1 /*
2   piano-pedal-engraver.cc -- implement Piano_pedal_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2006 Jan Nieuwenhuizen <janneke@gnu.org>,
7                  Erik Sandberg <mandolaerik@gmail.com>
8
9   Chris Jackson <chris@fluffhouse.org.uk> - extended to support
10   bracketed pedals.
11 */
12
13 #include "engraver.hh"
14
15 #include "axis-group-interface.hh"
16 #include "context.hh"
17 #include "directional-element-interface.hh"
18 #include "international.hh"
19 #include "lily-guile.hh"
20 #include "note-column.hh"
21 #include "side-position-interface.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "stream-event.hh"
24 #include "string-convert.hh"
25 #include "warn.hh"
26
27 #include "translator.icc"
28
29 /*
30   Urgh. This engraver is too complex. rewrite. --hwn
31 */
32
33 /* Ugh: This declaration is duplicated in piano-pedal-performer */
34 typedef enum Pedal_type {SOSTENUTO, SUSTAIN, UNA_CORDA, NUM_PEDAL_TYPES};
35
36 /*
37   Static precalculated data (symbols and strings) for the different
38   pedal types
39 */
40 struct Pedal_type_info
41 {
42   string base_name_;
43   SCM event_class_sym_;
44   SCM style_sym_;
45   SCM strings_sym_;
46   const char *pedal_line_spanner_c_str_;
47   const char *pedal_c_str_;
48 };
49
50 struct Pedal_info
51 {
52   const Pedal_type_info *type_;
53
54   /*
55     Event for currently running pedal.
56   */
57   Stream_event *current_bracket_ev_;
58
59   /*
60     Event for currently starting pedal, (necessary?
61
62     distinct from current_bracket_ev_, since current_bracket_ev_ only
63     necessary for brackets, not for text style.
64   */
65   Stream_event *start_ev_;
66
67   /*
68     Events that were found in this timestep.
69   */
70   Drul_array<Stream_event *> event_drul_;
71   Item *item_;
72   Spanner *bracket_; // A single portion of a pedal bracket
73   Spanner *finished_bracket_;
74
75   /*
76     This grob contains all the pedals of the same type on the same staff
77   */
78   Spanner *line_spanner_;
79   Spanner *finished_line_spanner_;
80 };
81
82 static Pedal_type_info pedal_types_[NUM_PEDAL_TYPES];
83
84 class Piano_pedal_engraver : public Engraver
85 {
86 public:
87   TRANSLATOR_DECLARATIONS (Piano_pedal_engraver);
88   ~Piano_pedal_engraver ();
89 protected:
90   virtual void initialize ();
91   virtual void finalize ();
92   DECLARE_TRANSLATOR_LISTENER (sustain);
93   DECLARE_TRANSLATOR_LISTENER (una_corda);
94   DECLARE_TRANSLATOR_LISTENER (sostenuto);
95   void stop_translation_timestep ();
96   DECLARE_ACKNOWLEDGER (note_column);
97   void process_music ();
98
99 private:
100   Pedal_info info_list_[NUM_PEDAL_TYPES + 1];
101
102   /*
103     Record a stack of the current pedal spanners, so if more than one pedal
104     occurs simultaneously then extra space can be added between them.
105   */
106
107   vector<Spanner*> previous_;
108   void del_linespanner (Spanner *);
109
110   void create_text_grobs (Pedal_info *p, bool);
111   void create_bracket_grobs (Pedal_info *p, bool);
112   void typeset_all (Pedal_info *p);
113 };
114
115 static void
116 init_pedal_types ()
117 {
118   const char *names [NUM_PEDAL_TYPES];
119   names[SOSTENUTO] = "Sostenuto";
120   names[SUSTAIN] = "Sustain";
121   names[UNA_CORDA] = "UnaCorda";
122
123   for (int i = 0; i < NUM_PEDAL_TYPES; i++)
124     {
125       const char *name = names[i];
126       /* FooBar */
127       string base_name = name;
128       /* foo-bar */
129       string base_ident = "";
130       int prev_pos=0;
131       int cur_pos;
132       for (cur_pos = 1; name[cur_pos]; cur_pos++)
133         if (isupper (name[cur_pos]))
134           {
135             base_ident = base_ident + String_convert::to_lower (string (name, prev_pos, cur_pos - prev_pos)) + "-";
136             prev_pos = cur_pos;
137           }
138       base_ident += String_convert::to_lower (string (name, prev_pos, cur_pos - prev_pos));
139
140       Pedal_type_info *tbl = &pedal_types_[i];
141       tbl->base_name_ = name;
142       /* These symbols are static and need to be protected */
143       tbl->event_class_sym_ = scm_gc_protect_object (scm_str2symbol ((base_ident + "-event").c_str ()));
144       tbl->pedal_line_spanner_c_str_ = strdup ((base_name + "PedalLineSpanner").c_str ());
145       tbl->style_sym_ = scm_gc_protect_object (scm_str2symbol (("pedal" + base_name + "Style").c_str ()));
146       tbl->strings_sym_ = scm_gc_protect_object (scm_str2symbol (("pedal" + base_name + "Strings").c_str ()));
147       tbl->pedal_c_str_ = strdup ((base_name + "Pedal").c_str ());
148     }
149 }
150 ADD_SCM_INIT_FUNC (Piano_pedal_engraver_init_pedal_types_, init_pedal_types);
151
152 Piano_pedal_engraver::Piano_pedal_engraver ()
153 {
154 }
155
156 void
157 Piano_pedal_engraver::initialize ()
158 {
159   for (int i = 0; i < NUM_PEDAL_TYPES; i++)
160     {
161       Pedal_type_info *s = &pedal_types_[i];
162       Pedal_info *info = &info_list_[i];
163
164       info->type_ = s;
165       info->item_ = 0;
166       info->bracket_ = 0;
167       info->finished_bracket_ = 0;
168       info->line_spanner_ = 0;
169       info->finished_line_spanner_ = 0;
170       info->current_bracket_ev_ = 0;
171       info->event_drul_[START] = 0;
172       info->event_drul_[STOP] = 0;
173       info->start_ev_ = 0;
174     }
175   info_list_[NUM_PEDAL_TYPES].type_ = 0;
176 }
177
178 Piano_pedal_engraver::~Piano_pedal_engraver ()
179 {
180 }
181
182 /*
183   Urg: Code dup
184   I'm a script
185 */
186 void
187 Piano_pedal_engraver::acknowledge_note_column (Grob_info info)
188 {
189   for (Pedal_info *p = info_list_; p->type_; p++)
190     {
191       if (p->line_spanner_)
192         {
193           Side_position_interface::add_support (p->line_spanner_, info.grob ());
194           add_bound_item (p->line_spanner_, info.grob ());
195         }
196       if (p->bracket_)
197         add_bound_item (p->bracket_, info.grob ());
198       if (p->finished_bracket_)
199         add_bound_item (p->finished_bracket_, info.grob ());
200     }
201 }
202
203 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, sostenuto);
204 void
205 Piano_pedal_engraver::listen_sostenuto (Stream_event *r)
206 {
207   Direction d = to_dir (r->get_property ("span-direction"));
208   info_list_[SOSTENUTO].event_drul_[d] = r;
209 }
210
211 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, sustain);
212 void
213 Piano_pedal_engraver::listen_sustain (Stream_event *r)
214 {
215   Direction d = to_dir (r->get_property ("span-direction"));
216   info_list_[SUSTAIN].event_drul_[d] = r;
217 }
218
219 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_engraver, una_corda);
220 void
221 Piano_pedal_engraver::listen_una_corda (Stream_event *r)
222 {
223   Direction d = to_dir (r->get_property ("span-direction"));
224   info_list_[UNA_CORDA].event_drul_[d] = r;
225 }
226
227 void
228 Piano_pedal_engraver::process_music ()
229 {
230   for (Pedal_info *p = info_list_; p->type_; p++)
231     {
232       if (p->event_drul_[STOP] || p->event_drul_[START])
233         {
234           if (!p->line_spanner_)
235             {
236               const char *name = p->type_->pedal_line_spanner_c_str_;
237               Stream_event *rq = (p->event_drul_[START] ? p->event_drul_[START] : p->event_drul_[STOP]);
238               p->line_spanner_ = make_spanner (name, rq->self_scm ());
239             }
240
241           /* Choose the appropriate grobs to add to the line spanner
242              These can be text items or text-spanners
243           */
244
245           /*
246             ugh, code dup, should read grob to create from other
247             property.
248
249             bracket: |_________/\____|
250             text:    Ped.     *Ped.  *
251             mixed:   Ped. _____/\____|
252           */
253
254           SCM style = internal_get_property (p->type_->style_sym_);
255
256           bool mixed = style == ly_symbol2scm ("mixed");
257           bool bracket = (mixed
258                           || style == ly_symbol2scm ("bracket"));
259           bool text = (style == ly_symbol2scm ("text")
260                        || mixed);
261
262           if (text && !p->item_)
263             create_text_grobs (p, mixed);
264           if (bracket)
265             create_bracket_grobs (p, mixed);
266         }
267     }
268 }
269
270 void
271 Piano_pedal_engraver::create_text_grobs (Pedal_info *p, bool mixed)
272 {
273   SCM s = SCM_EOL;
274   SCM strings = internal_get_property (p->type_->strings_sym_);
275
276   if (scm_ilength (strings) < 3)
277     {
278       Stream_event *m = p->event_drul_[START];
279       if (!m) m = p->event_drul_ [STOP];
280
281       string msg = _f ("expect 3 strings for piano pedals, found: %ld",
282                        scm_ilength (strings));
283       if (m)
284         m->origin ()->warning (msg);
285       else
286         warning (msg);
287
288       return;
289     }
290
291   if (p->event_drul_[STOP] && p->event_drul_[START])
292     {
293       if (!mixed)
294         {
295           if (!p->start_ev_)
296             p->event_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
297           else
298             s = scm_cadr (strings);
299           p->start_ev_ = p->event_drul_[START];
300         }
301     }
302   else if (p->event_drul_[STOP])
303     {
304       if (!mixed)
305         {
306           if (!p->start_ev_)
307             p->event_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
308           else
309             s = scm_caddr (strings);
310           p->start_ev_ = 0;
311         }
312     }
313   else if (p->event_drul_[START])
314     {
315       p->start_ev_ = p->event_drul_[START];
316       s = scm_car (strings);
317       if (!mixed)
318         {
319           /*
320             Code dup?! see below.
321           */
322           if (previous_.size ())
323             // add extra space below the previous already-occuring pedal
324             Side_position_interface::add_support (p->line_spanner_,
325                                                   previous_.back ());
326           previous_.push_back (p->line_spanner_);
327         }
328     }
329
330   if (scm_is_string (s))
331     {
332       const char *propname = p->type_->pedal_c_str_;
333
334       p->item_ = make_item (propname, (p->event_drul_[START]
335                                        ? p->event_drul_[START]
336                                        : p->event_drul_[STOP])->self_scm ());
337
338       p->item_->set_property ("text", s);
339       Axis_group_interface::add_element (p->line_spanner_, p->item_);
340     }
341
342   if (!mixed)
343     {
344       p->event_drul_[START] = 0;
345       p->event_drul_[STOP] = 0;
346     }
347 }
348
349 void
350 Piano_pedal_engraver::create_bracket_grobs (Pedal_info *p, bool mixed)
351 {
352   if (!p->bracket_ && p->event_drul_[STOP])
353     {
354       string msg = _f ("can't find start of piano pedal bracket: `%s'", p->type_->base_name_.c_str ());
355       p->event_drul_[STOP]->origin ()->warning (msg);
356       p->event_drul_[STOP] = 0;
357     }
358
359   if (p->event_drul_[STOP])
360     {
361       assert (!p->finished_bracket_);
362
363       Grob *cmc = unsmob_grob (get_property ("currentMusicalColumn"));
364
365       if (!p->bracket_->get_bound (RIGHT))
366         p->bracket_->set_bound (RIGHT, cmc);
367
368       /*
369         Set properties so that the stencil-creating function will
370         know whether the right edge should be flared ___/
371       */
372
373       if (!p->event_drul_[START])
374         {
375           SCM flare = p->bracket_->get_property ("bracket-flare");
376           p->bracket_->set_property ("bracket-flare", scm_cons (scm_car (flare),
377                                                                 scm_from_double (0)));
378         }
379
380       p->finished_bracket_ = p->bracket_;
381       p->bracket_ = 0;
382       p->current_bracket_ev_ = 0;
383     }
384
385   if (p->event_drul_[START])
386     {
387       p->start_ev_ = p->event_drul_[START];
388       p->current_bracket_ev_ = p->event_drul_[START];
389
390       p->bracket_ = make_spanner ("PianoPedalBracket", p->event_drul_[START]->self_scm ());
391
392       /*
393         Set properties so that the stencil-creating function will
394         know whether the left edge should be flared \___
395       */
396
397       if (!p->finished_bracket_)
398         {
399           SCM flare = p->bracket_->get_property ("bracket-flare");
400           p->bracket_->set_property ("bracket-flare", scm_cons (scm_from_double (0), scm_cdr (flare)));
401         }
402
403       /* Set this property for 'mixed style' pedals,    Ped._______/\ ,
404          so the stencil function will shorten the ____ line by the length of the Ped. text.
405       */
406
407       if (mixed)
408         {
409           /*
410             Mixed style: Store a pointer to the preceding text for use in
411             calculating the length of the line
412
413
414             TODO:
415
416             WTF is pedal-text not the bound of the object? --hwn
417           */
418           if (p->item_)
419             p->bracket_->set_object ("pedal-text", p->item_->self_scm ());
420         }
421
422       /*
423         We do not use currentMusicalColumn for the left span-point.
424         If the column as accidentals (eg on a different stave), the
425         currentMusicalColumn is too wide, making the bracket too big.
426
427         TODO:
428
429         Hmm. What do we do when there are no notes when the spanner starts?
430
431         TODO:
432
433         what about the right span point?
434
435       */
436       Axis_group_interface::add_element (p->line_spanner_, p->bracket_);
437
438       if (!p->event_drul_[STOP])
439         {
440
441           /*
442             code dup. --hwn.
443
444             // position new pedal spanner below the current one
445             */
446           if (previous_.size ())
447             Side_position_interface::add_support (p->line_spanner_, previous_.back ());
448
449           previous_.push_back (p->line_spanner_);
450         }
451     }
452
453   p->event_drul_[START] = 0;
454   p->event_drul_[STOP] = 0;
455 }
456
457 void
458 Piano_pedal_engraver::finalize ()
459 {
460   for (Pedal_info *p = info_list_; p->type_; p++)
461     {
462       /*
463         suicide?
464       */
465       if (p->line_spanner_
466           && !p->line_spanner_->is_live ())
467         p->line_spanner_ = 0;
468
469       if (p->bracket_
470           && !p->bracket_->is_live ())
471         p->bracket_ = 0;
472
473       if (p->bracket_)
474         {
475           SCM cc = get_property ("currentCommandColumn");
476           Item *c = unsmob_item (cc);
477           if (p->line_spanner_)
478             p->line_spanner_->set_bound (RIGHT, c);
479           p->bracket_->set_bound (RIGHT, c);
480
481           p->finished_bracket_ = p->bracket_;
482           p->bracket_ = 0;
483           p->finished_line_spanner_ = p->line_spanner_;
484           p->line_spanner_ = 0;
485           typeset_all (p);
486         }
487
488       if (p->line_spanner_)
489         {
490           p->finished_line_spanner_ = p->line_spanner_;
491           typeset_all (p);
492         }
493     }
494 }
495
496 void
497 Piano_pedal_engraver::del_linespanner (Spanner *g)
498 {
499   vsize idx = find (previous_, g) - previous_.begin ();
500   if (idx != VPOS && idx < previous_.size ())
501     previous_.erase (previous_.begin () + idx);
502 }
503
504 void
505 Piano_pedal_engraver::stop_translation_timestep ()
506 {
507   for (Pedal_info *p = info_list_; p->type_; p++)
508     {
509       if (!p->bracket_)
510         {
511           p->finished_line_spanner_ = p->line_spanner_;
512           p->line_spanner_ = 0;
513           del_linespanner (p->finished_line_spanner_);
514         }
515
516       typeset_all (p);
517     }
518
519   for (Pedal_info *p = info_list_; p->type_; p++)
520     {
521       p->event_drul_[STOP] = 0;
522       p->event_drul_[START] = 0;
523     }
524 }
525
526 void
527 Piano_pedal_engraver::typeset_all (Pedal_info *p)
528 {
529   /*
530     Handle suicide.
531   */
532   if (p->finished_line_spanner_
533       && !p->finished_line_spanner_->is_live ())
534     p->finished_line_spanner_ = 0;
535   if (p->finished_bracket_
536       && !p->finished_bracket_->is_live ())
537     p->finished_bracket_ = 0;
538
539   if (p->item_)
540     p->item_ = 0;
541
542   if (p->finished_bracket_)
543     {
544       Grob *r = p->finished_bracket_->get_bound (RIGHT);
545       if (!r)
546         p->finished_bracket_->set_bound (RIGHT, unsmob_grob (get_property ("currentMusicalColumn")));
547
548       p->finished_bracket_ = 0;
549     }
550
551   if (p->finished_line_spanner_)
552     {
553       Grob *l = p->finished_line_spanner_->get_bound (LEFT);
554       Grob *r = p->finished_line_spanner_->get_bound (RIGHT);
555       if (!r && l)
556         p->finished_line_spanner_->set_bound (RIGHT, l);
557       else if (!l && r)
558         p->finished_line_spanner_->set_bound (LEFT, r);
559       else if (!r && !l)
560         {
561           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
562           Item *ci = dynamic_cast<Item *> (cc);
563           p->finished_line_spanner_->set_bound (RIGHT, ci);
564           p->finished_line_spanner_->set_bound (LEFT, ci);
565         }
566
567       p->finished_line_spanner_ = 0;
568     }
569 }
570
571 ADD_ACKNOWLEDGER (Piano_pedal_engraver, note_column);
572
573 ADD_TRANSLATOR (Piano_pedal_engraver,
574
575                 /* doc */
576                 "Engrave piano pedal symbols and brackets.",
577
578                 /* create */
579                 "SostenutoPedal "
580                 "SostenutoPedalLineSpanner "
581                 "SustainPedal "
582                 "SustainPedalLineSpanner "
583                 "UnaCordaPedal "
584                 "UnaCordaPedalLineSpanner ",
585
586                 /* accept */
587                 "pedal-event",
588
589                 /* read */
590                 "currentCommandColumn "
591                 "pedalSostenutoStrings "
592                 "pedalSostenutoStyle "
593                 "pedalSustainStrings "
594                 "pedalSustainStyle "
595                 "pedalUnaCordaStrings "
596                 "pedalUnaCordaStyle",
597                 /* write */ "");