]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / dynamic-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2008--2015 Han-Wen Nienhuys <hanwen@lilypond.org>
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 "engraver.hh"
21 #include "hairpin.hh"
22 #include "international.hh"
23 #include "item.hh"
24 #include "note-column.hh"
25 #include "pointer-group-interface.hh"
26 #include "self-alignment-interface.hh"
27 #include "spanner.hh"
28 #include "stream-event.hh"
29 #include "text-interface.hh"
30
31 #include "translator.icc"
32
33 using std::string;
34
35 class Dynamic_engraver : public Engraver
36 {
37   TRANSLATOR_DECLARATIONS (Dynamic_engraver);
38   DECLARE_ACKNOWLEDGER (note_column);
39   DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
40   DECLARE_TRANSLATOR_LISTENER (span_dynamic);
41   DECLARE_TRANSLATOR_LISTENER (break_span);
42
43 protected:
44   virtual void process_music ();
45   virtual void stop_translation_timestep ();
46   virtual void finalize ();
47
48 private:
49   SCM get_property_setting (Stream_event *evt, char const *evprop,
50                             char const *ctxprop);
51   string get_spanner_type (Stream_event *ev);
52
53   Drul_array<Stream_event *> accepted_spanevents_drul_;
54   Spanner *current_spanner_;
55   Spanner *finished_spanner_;
56
57   Item *script_;
58   Stream_event *script_event_;
59   Stream_event *current_span_event_;
60   bool end_new_spanner_;
61 };
62
63 Dynamic_engraver::Dynamic_engraver ()
64 {
65   script_event_ = 0;
66   current_span_event_ = 0;
67   script_ = 0;
68   finished_spanner_ = 0;
69   current_spanner_ = 0;
70   accepted_spanevents_drul_.set (0, 0);
71   end_new_spanner_ = false;
72 }
73
74 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
75 void
76 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
77 {
78   ASSIGN_EVENT_ONCE (script_event_, ev);
79 }
80
81 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
82 void
83 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
84 {
85   Direction d = to_dir (ev->get_property ("span-direction"));
86
87   ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[d], ev);
88 }
89
90 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, break_span);
91 void
92 Dynamic_engraver::listen_break_span (Stream_event *event)
93 {
94   if (event->in_event_class ("break-dynamic-span-event"))
95     {
96       // Case 1: Already have a start dynamic event -> break applies to new
97       //         spanner (created later) -> set a flag
98       // Case 2: no new spanner, but spanner already active -> break it now
99       if (accepted_spanevents_drul_[START])
100         end_new_spanner_ = true;
101       else if (current_spanner_)
102         current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
103     }
104 }
105
106 SCM
107 Dynamic_engraver::get_property_setting (Stream_event *evt,
108                                         char const *evprop,
109                                         char const *ctxprop)
110 {
111   SCM spanner_type = evt->get_property (evprop);
112   if (scm_is_null (spanner_type))
113     spanner_type = get_property (ctxprop);
114   return spanner_type;
115 }
116
117 void
118 Dynamic_engraver::process_music ()
119 {
120   if (current_spanner_
121       && (accepted_spanevents_drul_[STOP]
122           || script_event_
123           || accepted_spanevents_drul_[START]))
124     {
125       Stream_event *ender = accepted_spanevents_drul_[STOP];
126       if (!ender)
127         ender = script_event_;
128
129       if (!ender)
130         ender = accepted_spanevents_drul_[START];
131
132       finished_spanner_ = current_spanner_;
133       announce_end_grob (finished_spanner_, ender->self_scm ());
134       current_spanner_ = 0;
135       current_span_event_ = 0;
136     }
137
138   if (accepted_spanevents_drul_[START])
139     {
140       current_span_event_ = accepted_spanevents_drul_[START];
141
142       string start_type = get_spanner_type (current_span_event_);
143       SCM cresc_type = get_property_setting (current_span_event_, "span-type",
144                                              (start_type + "Spanner").c_str ());
145
146       if (scm_is_eq (cresc_type, ly_symbol2scm ("text")))
147         {
148           current_spanner_
149             = make_spanner ("DynamicTextSpanner",
150                             accepted_spanevents_drul_[START]->self_scm ());
151
152           SCM text = get_property_setting (current_span_event_, "span-text",
153                                            (start_type + "Text").c_str ());
154           if (Text_interface::is_markup (text))
155             current_spanner_->set_property ("text", text);
156           /*
157             If the line of a text spanner is hidden, end the alignment spanner
158             early: this allows dynamics to be spaced individually instead of
159             being linked together.
160           */
161           if (scm_is_eq (current_spanner_->get_property ("style"),
162                          ly_symbol2scm ("none")))
163             current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
164         }
165       else
166         {
167           if (!scm_is_eq (cresc_type, ly_symbol2scm ("hairpin")))
168             {
169               string as_string = ly_scm_write_string (cresc_type);
170               current_span_event_
171               ->origin ()->warning (_f ("unknown crescendo style: %s\ndefaulting to hairpin.", as_string.c_str ()));
172             }
173           current_spanner_ = make_spanner ("Hairpin",
174                                            current_span_event_->self_scm ());
175         }
176       // if we have a break-dynamic-span event right after the start dynamic, break the new spanner immediately
177       if (end_new_spanner_)
178         {
179           current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
180           end_new_spanner_ = false;
181         }
182       if (finished_spanner_)
183         {
184           if (has_interface<Hairpin> (finished_spanner_))
185             Pointer_group_interface::add_grob (finished_spanner_,
186                                                ly_symbol2scm ("adjacent-spanners"),
187                                                current_spanner_);
188           if (has_interface<Hairpin> (current_spanner_))
189             Pointer_group_interface::add_grob (current_spanner_,
190                                                ly_symbol2scm ("adjacent-spanners"),
191                                                finished_spanner_);
192         }
193     }
194
195   if (script_event_)
196     {
197       script_ = make_item ("DynamicText", script_event_->self_scm ());
198       script_->set_property ("text",
199                              script_event_->get_property ("text"));
200
201       if (finished_spanner_)
202         finished_spanner_->set_bound (RIGHT, script_);
203       if (current_spanner_)
204         current_spanner_->set_bound (LEFT, script_);
205     }
206 }
207
208 void
209 Dynamic_engraver::stop_translation_timestep ()
210 {
211   if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
212     finished_spanner_
213     ->set_bound (RIGHT,
214                  unsmob<Grob> (get_property ("currentMusicalColumn")));
215
216   if (current_spanner_ && !current_spanner_->get_bound (LEFT))
217     current_spanner_
218     ->set_bound (LEFT,
219                  unsmob<Grob> (get_property ("currentMusicalColumn")));
220   script_ = 0;
221   script_event_ = 0;
222   accepted_spanevents_drul_.set (0, 0);
223   finished_spanner_ = 0;
224   end_new_spanner_ = false;
225 }
226
227 void
228 Dynamic_engraver::finalize ()
229 {
230   if (current_spanner_
231       && !current_spanner_->is_live ())
232     current_spanner_ = 0;
233   if (current_spanner_)
234     {
235       current_span_event_
236       ->origin ()->warning (_f ("unterminated %s",
237                                 get_spanner_type (current_span_event_)
238                                 .c_str ()));
239       current_spanner_->suicide ();
240       current_spanner_ = 0;
241     }
242 }
243
244 string
245 Dynamic_engraver::get_spanner_type (Stream_event *ev)
246 {
247   string type;
248   SCM start_sym = scm_car (ev->get_property ("class"));
249
250   if (scm_is_eq (start_sym, ly_symbol2scm ("decrescendo-event")))
251     type = "decrescendo";
252   else if (scm_is_eq (start_sym, ly_symbol2scm ("crescendo-event")))
253     type = "crescendo";
254   else
255     programming_error ("unknown dynamic spanner type");
256
257   return type;
258 }
259
260 void
261 Dynamic_engraver::acknowledge_note_column (Grob_info info)
262 {
263   if (script_ && !script_->get_parent (X_AXIS))
264     {
265       extract_grob_set (info.grob (), "note-heads", heads);
266       /*
267         Spacing constraints may require dynamics to be attached to rests,
268         so check for a rest if this note column has no note heads.
269       */
270       Grob *x_parent = (heads.size ()
271                         ? info.grob ()
272                         : unsmob<Grob> (info.grob ()->get_object ("rest")));
273       if (x_parent)
274         script_->set_parent (x_parent, X_AXIS);
275     }
276
277   if (current_spanner_ && !current_spanner_->get_bound (LEFT))
278     current_spanner_->set_bound (LEFT, info.grob ());
279   if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
280     finished_spanner_->set_bound (RIGHT, info.grob ());
281 }
282
283 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
284 ADD_TRANSLATOR (Dynamic_engraver,
285                 /* doc */
286                 "Create hairpins, dynamic texts and dynamic text spanners.",
287
288                 /* create */
289                 "DynamicTextSpanner "
290                 "DynamicText "
291                 "Hairpin ",
292
293                 /* read */
294                 "crescendoSpanner "
295                 "crescendoText "
296                 "currentMusicalColumn "
297                 "decrescendoSpanner "
298                 "decrescendoText ",
299
300                 /* write */
301                 ""
302                );