]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-dynamic-engraver.cc
Imported Upstream version 2.14.2
[lilypond.git] / lily / new-dynamic-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2008--2011 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 class New_dynamic_engraver : public Engraver
34 {
35   TRANSLATOR_DECLARATIONS (New_dynamic_engraver);
36   DECLARE_ACKNOWLEDGER (note_column);
37   DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
38   DECLARE_TRANSLATOR_LISTENER (span_dynamic);
39
40 protected:
41   virtual void process_music ();
42   virtual void stop_translation_timestep ();
43   virtual void finalize ();
44
45 private:
46   SCM get_property_setting (Stream_event *evt, char const *evprop,
47                             char const *ctxprop);
48   string get_spanner_type (Stream_event *ev);
49
50   Drul_array<Stream_event *> accepted_spanevents_drul_;
51   Spanner *current_spanner_;
52   Spanner *finished_spanner_;
53
54   Item *script_;
55   Stream_event *script_event_;
56   Stream_event *current_span_event_;
57 };
58
59 New_dynamic_engraver::New_dynamic_engraver ()
60 {
61   script_event_ = 0;
62   current_span_event_ = 0;
63   script_ = 0;
64   finished_spanner_ = 0;
65   current_spanner_ = 0;
66   accepted_spanevents_drul_.set (0, 0);
67 }
68
69 IMPLEMENT_TRANSLATOR_LISTENER (New_dynamic_engraver, absolute_dynamic);
70 void
71 New_dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
72 {
73   ASSIGN_EVENT_ONCE (script_event_, ev);
74 }
75
76 IMPLEMENT_TRANSLATOR_LISTENER (New_dynamic_engraver, span_dynamic);
77 void
78 New_dynamic_engraver::listen_span_dynamic (Stream_event *ev)
79 {
80   Direction d = to_dir (ev->get_property ("span-direction"));
81
82   ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[d], ev);
83 }
84
85 SCM
86 New_dynamic_engraver::get_property_setting (Stream_event *evt,
87                                             char const *evprop,
88                                             char const *ctxprop)
89 {
90   SCM spanner_type = evt->get_property (evprop);
91   if (spanner_type == SCM_EOL)
92     spanner_type = get_property (ctxprop);
93   return spanner_type;
94 }
95
96 void
97 New_dynamic_engraver::process_music ()
98 {
99   if (current_spanner_
100       && (accepted_spanevents_drul_[STOP]
101           || script_event_
102           || accepted_spanevents_drul_[START]))
103     {
104       Stream_event *ender = accepted_spanevents_drul_[STOP];
105       if (!ender)
106         ender = script_event_;
107
108       if (!ender)
109         ender = accepted_spanevents_drul_[START];
110
111       finished_spanner_ = current_spanner_;
112       announce_end_grob (finished_spanner_, ender->self_scm ());
113       current_spanner_ = 0;
114       current_span_event_ = 0;
115     }
116
117   if (accepted_spanevents_drul_[START])
118     {
119       current_span_event_ = accepted_spanevents_drul_[START];
120
121       string start_type = get_spanner_type (current_span_event_);
122       SCM cresc_type = get_property_setting (current_span_event_, "span-type",
123                                              (start_type + "Spanner").c_str ());
124
125       if (cresc_type == ly_symbol2scm ("text"))
126         {
127           current_spanner_
128             = make_spanner ("DynamicTextSpanner",
129                             accepted_spanevents_drul_[START]->self_scm ());
130
131           SCM text = get_property_setting (current_span_event_, "span-text",
132                                            (start_type + "Text").c_str ());
133           if (Text_interface::is_markup (text))
134             current_spanner_->set_property ("text", text);
135         }
136       else
137         {
138           if (cresc_type != ly_symbol2scm ("hairpin"))
139             {
140               string as_string = ly_scm_write_string (cresc_type);
141               current_span_event_
142                 ->origin()->warning (_f ("unknown crescendo style: %s\ndefaulting to hairpin.", as_string.c_str()));
143             }
144           current_spanner_ = make_spanner ("Hairpin",
145                                            current_span_event_->self_scm ());
146         }
147       if (finished_spanner_)
148         {
149           if (Hairpin::has_interface (finished_spanner_))
150             Pointer_group_interface::add_grob (finished_spanner_,
151                                                ly_symbol2scm ("adjacent-spanners"),
152                                                current_spanner_);
153           if (Hairpin::has_interface (current_spanner_))
154             Pointer_group_interface::add_grob (current_spanner_,
155                                                ly_symbol2scm ("adjacent-spanners"),
156                                                finished_spanner_);
157         }
158     }
159
160   if (script_event_)
161     {
162       script_ = make_item ("DynamicText", script_event_->self_scm ());
163       script_->set_property ("text",
164                              script_event_->get_property ("text"));
165
166       if (finished_spanner_)
167         finished_spanner_->set_bound (RIGHT, script_);
168       if (current_spanner_)
169         current_spanner_->set_bound (LEFT, script_);
170     }
171 }
172
173 void
174 New_dynamic_engraver::stop_translation_timestep ()
175 {
176   if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
177     finished_spanner_
178       ->set_bound (RIGHT,
179                    unsmob_grob (get_property ("currentMusicalColumn")));
180
181   if (current_spanner_ && !current_spanner_->get_bound (LEFT))
182     current_spanner_
183       ->set_bound (LEFT,
184                    unsmob_grob (get_property ("currentMusicalColumn")));
185   script_ = 0;
186   script_event_ = 0;
187   accepted_spanevents_drul_.set (0, 0);
188   finished_spanner_ = 0;
189 }
190
191 void
192 New_dynamic_engraver::finalize ()
193 {
194   if (current_spanner_
195       && !current_spanner_->is_live ())
196     current_spanner_ = 0;
197   if (current_spanner_)
198     {
199       current_span_event_
200         ->origin ()->warning (_f ("unterminated %s",
201                                   get_spanner_type (current_span_event_)
202                                   .c_str ()));
203       current_spanner_->suicide ();
204       current_spanner_ = 0;
205     }
206 }
207
208 string
209 New_dynamic_engraver::get_spanner_type (Stream_event *ev)
210 {
211   string type;
212   SCM start_sym = ev->get_property ("class");
213
214   if (start_sym == ly_symbol2scm ("decrescendo-event"))
215     type = "decrescendo";
216   else if (start_sym == ly_symbol2scm ("crescendo-event"))
217     type = "crescendo";
218   else
219     programming_error ("unknown dynamic spanner type");
220
221   return type;
222 }
223
224 void
225 New_dynamic_engraver::acknowledge_note_column (Grob_info info)
226 {
227   if (script_ && !script_->get_parent (X_AXIS))
228     {
229       extract_grob_set (info.grob (), "note-heads", heads);
230       /*
231         Spacing constraints may require dynamics to be aligned on rests,
232         so check for a rest if this note column has no note heads.
233       */
234       Grob *x_parent = (heads.size ()
235                         ? heads[0]
236                         : unsmob_grob (info.grob ()->get_object ("rest")));
237       if (x_parent)
238         {
239           script_->set_parent (x_parent, X_AXIS);
240           Self_alignment_interface::set_center_parent (script_, X_AXIS);
241         }
242     }
243
244   if (current_spanner_ && !current_spanner_->get_bound (LEFT))
245     current_spanner_->set_bound (LEFT, info.grob ());
246   if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
247     finished_spanner_->set_bound (RIGHT, info.grob ());
248 }
249
250 ADD_ACKNOWLEDGER (New_dynamic_engraver, note_column);
251 ADD_TRANSLATOR (New_dynamic_engraver,
252                 /* doc */
253                 "Create hairpins, dynamic texts and dynamic text spanners.",
254
255                 /* create */
256                 "DynamicTextSpanner "
257                 "DynamicText "
258                 "Hairpin ",
259
260                 /* read */
261                 "crescendoSpanner "
262                 "crescendoText "
263                 "currentMusicalColumn "
264                 "decrescendoSpanner "
265                 "decrescendoText ",
266
267                 /* write */
268                 ""
269                 );