2 span-dynamic-performer.cc -- implement Span_dynamic_performer
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2006 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "performer.hh"
11 #include "audio-item.hh"
12 #include "international.hh"
13 #include "stream-event.hh"
15 #include "translator.icc"
18 TODO: fold this into 1 engraver: \< and \> should also stop when
19 absdyn is encountered.
21 struct Audio_dynamic_tuple
23 Audio_dynamic *audio_;
30 class Span_dynamic_performer : public Performer
33 TRANSLATOR_DECLARATIONS (Span_dynamic_performer);
36 virtual void acknowledge_audio_element (Audio_element_info);
37 void process_music ();
38 void stop_translation_timestep ();
40 DECLARE_TRANSLATOR_LISTENER (decrescendo);
41 DECLARE_TRANSLATOR_LISTENER (crescendo);
43 Audio_dynamic *audio_;
45 Stream_event *span_start_event_;
46 Drul_array<Stream_event *> span_events_;
47 vector<Audio_dynamic_tuple> dynamic_tuples_;
48 vector<Audio_dynamic_tuple> finished_dynamic_tuples_;
50 Direction finished_dir_;
53 Span_dynamic_performer::Span_dynamic_performer ()
55 span_events_[START] = 0;
56 span_events_[STOP] = 0;
57 span_start_event_ = 0;
63 Span_dynamic_performer::acknowledge_audio_element (Audio_element_info i)
65 if (Audio_dynamic *d = dynamic_cast<Audio_dynamic *> (i.elem_))
66 last_volume_ = d->volume_;
70 Span_dynamic_performer::process_music ()
72 if (span_start_event_ || span_events_[START])
74 audio_ = new Audio_dynamic (0);
75 Audio_element_info info (audio_, span_events_[START]
77 : span_events_[STOP]);
78 announce_element (info);
79 Audio_dynamic_tuple a = { audio_, now_mom () };
80 dynamic_tuples_.push_back (a);
83 if (span_events_[STOP])
85 if (!span_start_event_)
87 span_events_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
88 span_events_[STOP] = 0;
93 finished_dynamic_tuples_ = dynamic_tuples_;
95 dynamic_tuples_.clear ();
96 span_start_event_ = 0;
99 if (span_events_[START])
101 dir_ = (span_events_[START]->in_event_class ("crescendo-event"))
103 span_start_event_ = span_events_[START];
105 dynamic_tuples_.clear ();
106 Audio_dynamic_tuple a = { audio_, now_mom () };
107 dynamic_tuples_.push_back (a);
110 if (span_events_[STOP])
111 finished_dynamic_tuples_.back ().audio_->volume_ = last_volume_;
113 if (span_events_[START])
114 dynamic_tuples_[0].audio_->volume_ = last_volume_;
116 span_events_[START] = 0;
117 span_events_[STOP] = 0;
121 Span_dynamic_performer::stop_translation_timestep ()
123 if (finished_dynamic_tuples_.size () > 1)
125 Real start_volume = finished_dynamic_tuples_[0].audio_->volume_;
126 Real dv = finished_dynamic_tuples_.back ().audio_->volume_
130 Catch and fix the case of:
134 f cresc. -- -- -- -- -- pp
136 Actually, we should provide a non-displayed dynamic/volume setting,
137 to set volume to 'ff' just before the pp.
139 if (!dv || sign (dv) != finished_dir_)
141 // urg. 20%: about two volume steps
142 dv = (Real)finished_dir_ * 0.2;
144 start_volume = finished_dynamic_tuples_.back ().audio_->volume_
147 Moment start_mom = finished_dynamic_tuples_[0].mom_;
148 Moment dt = finished_dynamic_tuples_.back ().mom_ - start_mom;
149 for (vsize i = 0; i < finished_dynamic_tuples_.size (); i++)
151 Audio_dynamic_tuple *a = &finished_dynamic_tuples_[i];
152 Real volume = start_volume + dv * (Real) (a->mom_ - start_mom).main_part_
153 / (Real)dt.main_part_;
154 a->audio_->volume_ = volume;
156 finished_dynamic_tuples_.clear ();
164 span_events_[STOP] = 0;
165 span_events_[START] = 0;
168 IMPLEMENT_TRANSLATOR_LISTENER (Span_dynamic_performer, decrescendo);
170 Span_dynamic_performer::listen_decrescendo (Stream_event *r)
172 Direction d = to_dir (r->get_property ("span-direction"));
176 IMPLEMENT_TRANSLATOR_LISTENER (Span_dynamic_performer, crescendo);
178 Span_dynamic_performer::listen_crescendo (Stream_event *r)
180 Direction d = to_dir (r->get_property ("span-direction"));
184 ADD_TRANSLATOR (Span_dynamic_performer,