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