2 span-dynamic-performer.cc -- implement Span_dynamic_performer
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2005 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "performer.hh"
10 #include "audio-item.hh"
14 TODO: fold this into 1 engraver: \< and \> should also stop when
15 absdyn is encountered.
17 struct Audio_dynamic_tuple
19 Audio_dynamic *audio_;
26 class Span_dynamic_performer : public Performer
29 TRANSLATOR_DECLARATIONS (Span_dynamic_performer);
32 virtual bool try_music (Music *);
33 virtual void acknowledge_audio_element (Audio_element_info);
34 void process_music ();
35 void stop_translation_timestep ();
38 Audio_dynamic *audio_;
40 Music *span_start_event_;
41 Drul_array<Music *> span_events_;
42 Array<Audio_dynamic_tuple> dynamic_tuples_;
43 Array<Audio_dynamic_tuple> finished_dynamic_tuples_;
45 Direction finished_dir_;
48 Span_dynamic_performer::Span_dynamic_performer ()
50 span_events_[START] = 0;
51 span_events_[STOP] = 0;
52 span_start_event_ = 0;
58 Span_dynamic_performer::acknowledge_audio_element (Audio_element_info i)
60 if (Audio_dynamic *d = dynamic_cast<Audio_dynamic *> (i.elem_))
61 last_volume_ = d->volume_;
65 Span_dynamic_performer::process_music ()
67 if (span_start_event_ || span_events_[START])
69 audio_ = new Audio_dynamic (0);
70 Audio_element_info info (audio_, span_events_[START]
72 : span_events_[STOP]);
73 announce_element (info);
74 Audio_dynamic_tuple a = { audio_, now_mom () };
75 dynamic_tuples_.push (a);
78 if (span_events_[STOP])
80 if (!span_start_event_)
82 span_events_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
83 span_events_[STOP] = 0;
88 finished_dynamic_tuples_ = dynamic_tuples_;
90 dynamic_tuples_.clear ();
91 span_start_event_ = 0;
94 if (span_events_[START])
96 dir_ = (span_events_[START]->is_mus_type ("crescendo-event"))
98 span_start_event_ = span_events_[START];
100 dynamic_tuples_.clear ();
101 Audio_dynamic_tuple a = { audio_, now_mom () };
102 dynamic_tuples_.push (a);
105 if (span_events_[STOP])
106 finished_dynamic_tuples_.top ().audio_->volume_ = last_volume_;
108 if (span_events_[START])
109 dynamic_tuples_[0].audio_->volume_ = last_volume_;
111 span_events_[START] = 0;
112 span_events_[STOP] = 0;
116 Span_dynamic_performer::stop_translation_timestep ()
118 if (finished_dynamic_tuples_.size () > 1)
120 Real start_volume = finished_dynamic_tuples_[0].audio_->volume_;
121 Real dv = finished_dynamic_tuples_.top ().audio_->volume_
125 Catch and fix the case of:
129 f cresc. -- -- -- -- -- pp
131 Actually, we should provide a non-displayed dynamic/volume setting,
132 to set volume to 'ff' just before the pp.
134 if (!dv || sign (dv) != finished_dir_)
136 // urg. 20%: about two volume steps
137 dv = (Real)finished_dir_ * 0.2;
139 start_volume = finished_dynamic_tuples_.top
140 ().audio_->volume_ - dv;
142 Moment start_mom = finished_dynamic_tuples_[0].mom_;
143 Moment dt = finished_dynamic_tuples_.top ().mom_ - start_mom;
144 for (int i = 0; i < finished_dynamic_tuples_.size (); i++)
146 Audio_dynamic_tuple *a = &finished_dynamic_tuples_[i];
147 Real volume = start_volume + dv * (Real) (a->mom_ - start_mom).main_part_
148 / (Real)dt.main_part_;
149 a->audio_->volume_ = volume;
151 finished_dynamic_tuples_.clear ();
156 play_element (audio_);
160 span_events_[STOP] = 0;
161 span_events_[START] = 0;
165 Span_dynamic_performer::try_music (Music *r)
167 if (r->is_mus_type ("crescendo-event")
168 || r->is_mus_type ("decrescendo-event"))
170 Direction d = to_dir (r->get_property ("span-direction"));
176 #include "translator.icc"
178 ADD_TRANSLATOR (Span_dynamic_performer,
180 "crescendo-event decrescendo-event",