]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
0107e7f436c6a9baefd7368615666d9e970684cf
[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   Stream_event *stop_ev = accepted_spanevents_drul_ [STOP]
171                           ? accepted_spanevents_drul_[STOP] : script_ev_;
172
173   if (accepted_spanevents_drul_[STOP] || script_ev_)
174     {
175       /*
176         finish side position alignment if the (de)cresc ends here, and
177         there are no new dynamics.
178       */
179
180       if (cresc_)
181         {
182           assert (!finished_cresc_ && cresc_);
183
184           if (script_)
185             {
186               cresc_->set_bound (RIGHT, script_);
187               add_bound_item (line_spanner_, script_);
188             }
189
190           finished_cresc_ = cresc_;
191           announce_end_grob (finished_cresc_, SCM_EOL);
192           cresc_ = 0;
193           current_cresc_ev_ = 0;
194         }
195       else if (accepted_spanevents_drul_[STOP])
196         {
197           accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("cannot find start of (de)crescendo"));
198           stop_ev = 0;
199         }
200     }
201
202   if (accepted_spanevents_drul_[START])
203     {
204       if (current_cresc_ev_)
205         {
206           string msg = _ ("already have a decrescendo");
207           if (current_cresc_ev_->in_event_class ("crescendo-event"))
208             msg = _ ("already have a crescendo");
209
210           accepted_spanevents_drul_[START]->origin ()->warning (msg);
211           current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
212         }
213       else
214         {
215           current_cresc_ev_ = accepted_spanevents_drul_[START];
216
217           if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
218             set_grob_direction (line_spanner_, d);
219
220           /*
221             TODO: Use symbols.
222           */
223
224           SCM start_sym = current_cresc_ev_->get_property ("class");
225           string start_type;
226
227           if (start_sym == ly_symbol2scm ("decrescendo-event"))
228             start_type = "decrescendo";
229           else if (start_sym == ly_symbol2scm ("crescendo-event"))
230             start_type = "crescendo";
231           else
232             {
233               programming_error ("unknown dynamic spanner type");
234               return;
235             }
236
237           /*
238             UGH. TODO: should read from original event, so appearance
239             may be altered with \tweak.
240            */
241           SCM s = get_property ((start_type + "Spanner").c_str ());
242           if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
243             {
244               cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
245               if (finished_cresc_)
246                 {
247                   Pointer_group_interface::add_grob (finished_cresc_,
248                                                      ly_symbol2scm ("adjacent-hairpins"),
249                                                      cresc_);
250
251                   Pointer_group_interface::add_grob (cresc_,
252                                                      ly_symbol2scm ("adjacent-hairpins"),
253                                                      finished_cresc_);
254                 }
255             }
256
257           /*
258             This is a convenient (and legacy) interface to TextSpanners
259             for use in (de)crescendi.
260             Hmm.
261           */
262           else
263             {
264               cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
265               cresc_->set_property ("style", s);
266               context ()->set_property ((start_type
267                                          + "Spanner").c_str (), SCM_EOL);
268               s = get_property ((start_type + "Text").c_str ());
269               if (Text_interface::is_markup (s))
270                 {
271                   cresc_->set_property ("text", s);
272                   context ()->set_property ((start_type + "Text").c_str (),
273                                             SCM_EOL);
274                 }
275
276               if (script_)
277                 {
278                   set_nested_property (cresc_,
279                                        scm_list_3 (ly_symbol2scm ("bound-details"),
280                                                    ly_symbol2scm ("left"),
281                                                    ly_symbol2scm ("attach-dir")
282                                                   ),
283                                        scm_from_int (RIGHT));
284                 }
285             }
286
287           if (script_)
288             {
289               cresc_->set_bound (LEFT, script_);
290               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
291             }
292           Axis_group_interface::add_element (line_spanner_, cresc_);
293         }
294     }
295 }
296
297 void
298 Dynamic_engraver::stop_translation_timestep ()
299 {
300   if (!current_cresc_ev_ && line_spanner_)
301     {
302       assert (!finished_line_spanner_);
303       finished_line_spanner_ = line_spanner_;
304       line_spanner_ = 0;
305     }
306
307   typeset_all ();
308
309   if (cresc_ && !cresc_->get_bound (LEFT))
310     {
311       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
312       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
313     }
314
315   script_ev_ = 0;
316   accepted_spanevents_drul_[START] = 0;
317   accepted_spanevents_drul_[STOP] = 0;
318 }
319
320 void
321 Dynamic_engraver::finalize ()
322 {
323   typeset_all ();
324
325   if (line_spanner_
326       && !line_spanner_->is_live ())
327     line_spanner_ = 0;
328   if (line_spanner_)
329     {
330       finished_line_spanner_ = line_spanner_;
331       typeset_all ();
332     }
333
334   if (cresc_
335       && !cresc_->is_live ())
336     cresc_ = 0;
337   if (cresc_)
338     {
339       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
340       cresc_->suicide ();
341       cresc_ = 0;
342     }
343 }
344
345 void
346 Dynamic_engraver::typeset_all ()
347 {
348   if (finished_cresc_)
349     {
350       if (!finished_cresc_->get_bound (RIGHT))
351         {
352
353           Grob *column_bound = unsmob_grob (get_property ("currentMusicalColumn"));
354
355           finished_cresc_->set_bound (RIGHT, script_
356                                       ? script_
357                                       : column_bound);
358
359           if (finished_line_spanner_)
360             add_bound_item (finished_line_spanner_,
361                             finished_cresc_->get_bound (RIGHT));
362         }
363       finished_cresc_ = 0;
364     }
365
366   script_ = 0;
367   if (finished_line_spanner_)
368     {
369       /*
370         We used to have
371
372         extend-spanner-over-elements (finished_line_spanner_);
373
374         but this is rather kludgy, since finished_line_spanner_
375         typically has a staff-symbol field set , extending it over the
376         entire staff.
377
378       */
379
380       Grob *l = finished_line_spanner_->get_bound (LEFT);
381       Grob *r = finished_line_spanner_->get_bound (RIGHT);
382       if (!r && l)
383         finished_line_spanner_->set_bound (RIGHT, l);
384       else if (!l && r)
385         finished_line_spanner_->set_bound (LEFT, r);
386       else if (!r && !l)
387         {
388           /*
389             This is a isolated dynamic apparently, and does not even have
390             any interesting support item.
391           */
392           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
393           Item *ci = dynamic_cast<Item *> (cc);
394           finished_line_spanner_->set_bound (RIGHT, ci);
395           finished_line_spanner_->set_bound (LEFT, ci);
396         }
397       finished_line_spanner_ = 0;
398     }
399 }
400
401 void
402 Dynamic_engraver::acknowledge_note_column (Grob_info info)
403 {
404   if (!line_spanner_)
405     return;
406
407   if (line_spanner_
408       /* Don't refill killed spanner */
409       && line_spanner_->is_live ())
410     {
411       Side_position_interface::add_support (line_spanner_, info.grob ());
412       add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
413     }
414
415   if (script_ && !script_->get_parent (X_AXIS))
416     {
417       extract_grob_set (info.grob (), "note-heads", heads);
418       if (heads.size ())
419         {
420           Grob *head = heads[0];
421           script_->set_parent (head, X_AXIS);
422           Self_alignment_interface::set_center_parent (script_, X_AXIS);
423         }
424     }
425
426   if (cresc_)
427     {
428       if (!cresc_->get_bound (LEFT))
429         {
430           cresc_->set_bound (LEFT, info.grob ());
431           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
432         }
433     }
434
435   if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
436     finished_cresc_->set_bound (RIGHT, info.grob ());
437 }
438
439 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
440
441 ADD_TRANSLATOR (Dynamic_engraver,
442                 /* doc */
443                 "Create hairpins, dynamic texts, and their vertical"
444                 " alignments.  The symbols are collected onto a"
445                 " @code{DynamicLineSpanner} grob which takes care of vertical"
446                 " positioning.",
447
448                 /* create */
449                 "DynamicLineSpanner "
450                 "DynamicTextSpanner "
451                 "DynamicText "
452                 "Hairpin ",
453
454                 /* read */
455                 "",
456
457                 /* write */
458                 ""
459                );