]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
Clean up g++ 4.6.1 compiler warnings (-Wunused-but-set-variable).
[lilypond.git] / lily / dynamic-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "axis-group-interface.hh"
21 #include "context.hh"
22 #include "engraver.hh"
23 #include "dimensions.hh"
24 #include "directional-element-interface.hh"
25 #include "engraver.hh"
26 #include "hairpin.hh"
27 #include "international.hh"
28 #include "interval.hh"
29 #include "note-column.hh"
30 #include "paper-column.hh"
31 #include "pointer-group-interface.hh"
32 #include "script-interface.hh"
33 #include "self-alignment-interface.hh"
34 #include "side-position-interface.hh"
35 #include "staff-symbol-referencer.hh"
36 #include "stream-event.hh"
37 #include "warn.hh"
38 #include "spanner.hh"
39 #include "text-interface.hh"
40
41 #include "translator.icc"
42
43 /*
44   TODO:
45
46   * direction of text-dynamic-event if not equal to direction of
47   line-spanner
48
49   - TODO: this engraver is too complicated. We should split it into
50   the handling of the basic grobs and the linespanner
51
52   - TODO: the line-spanner is not killed after the (de)crescs are
53   finished.
54 */
55
56 /**
57    print text & hairpin dynamics.
58 */
59 class Dynamic_engraver : public Engraver
60 {
61   Item *script_;
62   Spanner *line_spanner_;
63   Spanner *cresc_;
64
65   Spanner *finished_line_spanner_;
66   Spanner *finished_cresc_;
67
68   Stream_event *script_ev_;
69   Stream_event *current_cresc_ev_;
70
71   Drul_array<Stream_event *> accepted_spanevents_drul_;
72
73   vector<Note_column *> pending_columns_;
74   vector<Grob *> pending_elements_;
75
76   void typeset_all ();
77
78   TRANSLATOR_DECLARATIONS (Dynamic_engraver);
79   DECLARE_ACKNOWLEDGER (note_column);
80   DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
81   DECLARE_TRANSLATOR_LISTENER (span_dynamic);
82
83 protected:
84   virtual void finalize ();
85   void stop_translation_timestep ();
86   void process_music ();
87 };
88
89 Dynamic_engraver::Dynamic_engraver ()
90 {
91   script_ = 0;
92   finished_cresc_ = 0;
93   line_spanner_ = 0;
94   finished_line_spanner_ = 0;
95   current_cresc_ev_ = 0;
96   cresc_ = 0;
97
98   script_ev_ = 0;
99   accepted_spanevents_drul_[START] = 0;
100   accepted_spanevents_drul_[STOP] = 0;
101 }
102
103 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
104 void
105 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
106 {
107   /*
108     TODO: probably broken.
109   */
110   ASSIGN_EVENT_ONCE (script_ev_, ev);
111 }
112
113 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
114 void
115 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
116 {
117   Direction d = to_dir (ev->get_property ("span-direction"));
118
119   if (d == START)
120     ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[START], ev);
121
122   /* Cancel any ongoing crescendo, either explicitly by \! or
123      implicitly by a new crescendo. Also avoid warning if cresc is
124      cancelled both implicitly and explicitly. */
125   if ((d == STOP || current_cresc_ev_) && !accepted_spanevents_drul_[STOP])
126     ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[STOP], ev);
127 }
128
129 void
130 Dynamic_engraver::process_music ()
131 {
132   if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
133     {
134       if (!line_spanner_)
135         {
136           Stream_event *rq = accepted_spanevents_drul_[START];
137           line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
138           if (script_ev_)
139             rq = script_ev_;
140         }
141     }
142
143   /*
144     During a (de)crescendo, pending event will not be cleared,
145     and a line-spanner will always be created, as \< \! are already
146     two events.
147
148     Note: line-spanner must always have at least same duration
149     as (de)crecsendo, b.o. line-breaking.
150   */
151
152   /*
153     maybe we should leave dynamic texts to the text-engraver and
154     simply acknowledge them?
155   */
156   if (script_ev_)
157     {
158       script_ = make_item ("DynamicText", script_ev_->self_scm ());
159       script_->set_property ("text",
160                              script_ev_->get_property ("text"));
161
162       if (Direction d = to_dir (script_ev_->get_property ("direction")))
163         set_grob_direction (line_spanner_, d);
164       else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
165         set_grob_direction (script_, d);
166
167       Axis_group_interface::add_element (line_spanner_, script_);
168     }
169
170   if (accepted_spanevents_drul_[STOP] || script_ev_)
171     {
172       /*
173         finish side position alignment if the (de)cresc ends here, and
174         there are no new dynamics.
175       */
176
177       if (cresc_)
178         {
179           assert (!finished_cresc_ && cresc_);
180
181           if (script_)
182             {
183               cresc_->set_bound (RIGHT, script_);
184               add_bound_item (line_spanner_, script_);
185             }
186
187           finished_cresc_ = cresc_;
188           announce_end_grob (finished_cresc_, SCM_EOL);
189           cresc_ = 0;
190           current_cresc_ev_ = 0;
191         }
192       else if (accepted_spanevents_drul_[STOP])
193         accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("cannot find start of (de)crescendo"));
194     }
195
196   if (accepted_spanevents_drul_[START])
197     {
198       if (current_cresc_ev_)
199         {
200           string msg = _ ("already have a decrescendo");
201           if (current_cresc_ev_->in_event_class ("crescendo-event"))
202             msg = _ ("already have a crescendo");
203
204           accepted_spanevents_drul_[START]->origin ()->warning (msg);
205           current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
206         }
207       else
208         {
209           current_cresc_ev_ = accepted_spanevents_drul_[START];
210
211           if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
212             set_grob_direction (line_spanner_, d);
213
214           /*
215             TODO: Use symbols.
216           */
217
218           SCM start_sym = current_cresc_ev_->get_property ("class");
219           string start_type;
220
221           if (start_sym == ly_symbol2scm ("decrescendo-event"))
222             start_type = "decrescendo";
223           else if (start_sym == ly_symbol2scm ("crescendo-event"))
224             start_type = "crescendo";
225           else
226             {
227               programming_error ("unknown dynamic spanner type");
228               return;
229             }
230
231           /*
232             UGH. TODO: should read from original event, so appearance
233             may be altered with \tweak.
234            */
235           SCM s = get_property ((start_type + "Spanner").c_str ());
236           if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
237             {
238               cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
239               if (finished_cresc_)
240                 {
241                   Pointer_group_interface::add_grob (finished_cresc_,
242                                                      ly_symbol2scm ("adjacent-hairpins"),
243                                                      cresc_);
244
245                   Pointer_group_interface::add_grob (cresc_,
246                                                      ly_symbol2scm ("adjacent-hairpins"),
247                                                      finished_cresc_);
248                 }
249             }
250
251           /*
252             This is a convenient (and legacy) interface to TextSpanners
253             for use in (de)crescendi.
254             Hmm.
255           */
256           else
257             {
258               cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
259               cresc_->set_property ("style", s);
260               context ()->set_property ((start_type
261                                          + "Spanner").c_str (), SCM_EOL);
262               s = get_property ((start_type + "Text").c_str ());
263               if (Text_interface::is_markup (s))
264                 {
265                   cresc_->set_property ("text", s);
266                   context ()->set_property ((start_type + "Text").c_str (),
267                                             SCM_EOL);
268                 }
269
270               if (script_)
271                 {
272                   set_nested_property (cresc_,
273                                        scm_list_3 (ly_symbol2scm ("bound-details"),
274                                                    ly_symbol2scm ("left"),
275                                                    ly_symbol2scm ("attach-dir")
276                                                   ),
277                                        scm_from_int (RIGHT));
278                 }
279             }
280
281           if (script_)
282             {
283               cresc_->set_bound (LEFT, script_);
284               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
285             }
286           Axis_group_interface::add_element (line_spanner_, cresc_);
287         }
288     }
289 }
290
291 void
292 Dynamic_engraver::stop_translation_timestep ()
293 {
294   if (!current_cresc_ev_ && line_spanner_)
295     {
296       assert (!finished_line_spanner_);
297       finished_line_spanner_ = line_spanner_;
298       line_spanner_ = 0;
299     }
300
301   typeset_all ();
302
303   if (cresc_ && !cresc_->get_bound (LEFT))
304     {
305       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
306       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
307     }
308
309   script_ev_ = 0;
310   accepted_spanevents_drul_[START] = 0;
311   accepted_spanevents_drul_[STOP] = 0;
312 }
313
314 void
315 Dynamic_engraver::finalize ()
316 {
317   typeset_all ();
318
319   if (line_spanner_
320       && !line_spanner_->is_live ())
321     line_spanner_ = 0;
322   if (line_spanner_)
323     {
324       finished_line_spanner_ = line_spanner_;
325       typeset_all ();
326     }
327
328   if (cresc_
329       && !cresc_->is_live ())
330     cresc_ = 0;
331   if (cresc_)
332     {
333       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
334       cresc_->suicide ();
335       cresc_ = 0;
336     }
337 }
338
339 void
340 Dynamic_engraver::typeset_all ()
341 {
342   if (finished_cresc_)
343     {
344       if (!finished_cresc_->get_bound (RIGHT))
345         {
346
347           Grob *column_bound = unsmob_grob (get_property ("currentMusicalColumn"));
348
349           finished_cresc_->set_bound (RIGHT, script_
350                                       ? script_
351                                       : column_bound);
352
353           if (finished_line_spanner_)
354             add_bound_item (finished_line_spanner_,
355                             finished_cresc_->get_bound (RIGHT));
356         }
357       finished_cresc_ = 0;
358     }
359
360   script_ = 0;
361   if (finished_line_spanner_)
362     {
363       /*
364         We used to have
365
366         extend-spanner-over-elements (finished_line_spanner_);
367
368         but this is rather kludgy, since finished_line_spanner_
369         typically has a staff-symbol field set , extending it over the
370         entire staff.
371
372       */
373
374       Grob *l = finished_line_spanner_->get_bound (LEFT);
375       Grob *r = finished_line_spanner_->get_bound (RIGHT);
376       if (!r && l)
377         finished_line_spanner_->set_bound (RIGHT, l);
378       else if (!l && r)
379         finished_line_spanner_->set_bound (LEFT, r);
380       else if (!r && !l)
381         {
382           /*
383             This is a isolated dynamic apparently, and does not even have
384             any interesting support item.
385           */
386           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
387           Item *ci = dynamic_cast<Item *> (cc);
388           finished_line_spanner_->set_bound (RIGHT, ci);
389           finished_line_spanner_->set_bound (LEFT, ci);
390         }
391       finished_line_spanner_ = 0;
392     }
393 }
394
395 void
396 Dynamic_engraver::acknowledge_note_column (Grob_info info)
397 {
398   if (!line_spanner_)
399     return;
400
401   if (line_spanner_
402       /* Don't refill killed spanner */
403       && line_spanner_->is_live ())
404     {
405       Side_position_interface::add_support (line_spanner_, info.grob ());
406       add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
407     }
408
409   if (script_ && !script_->get_parent (X_AXIS))
410     {
411       extract_grob_set (info.grob (), "note-heads", heads);
412       if (heads.size ())
413         {
414           Grob *head = heads[0];
415           script_->set_parent (head, X_AXIS);
416           Self_alignment_interface::set_center_parent (script_, X_AXIS);
417         }
418     }
419
420   if (cresc_)
421     {
422       if (!cresc_->get_bound (LEFT))
423         {
424           cresc_->set_bound (LEFT, info.grob ());
425           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
426         }
427     }
428
429   if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
430     finished_cresc_->set_bound (RIGHT, info.grob ());
431 }
432
433 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
434
435 ADD_TRANSLATOR (Dynamic_engraver,
436                 /* doc */
437                 "Create hairpins, dynamic texts, and their vertical"
438                 " alignments.  The symbols are collected onto a"
439                 " @code{DynamicLineSpanner} grob which takes care of vertical"
440                 " positioning.",
441
442                 /* create */
443                 "DynamicLineSpanner "
444                 "DynamicTextSpanner "
445                 "DynamicText "
446                 "Hairpin ",
447
448                 /* read */
449                 "",
450
451                 /* write */
452                 ""
453                );