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