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