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"
16 TODO: fold this into 1 engraver: \< and \> should also stop when
17 absdyn is encountered.
19 struct Audio_dynamic_tuple
21 Audio_dynamic *audio_;
28 class Span_dynamic_performer : public Performer
31 TRANSLATOR_DECLARATIONS (Span_dynamic_performer);
34 virtual bool try_music (Music *);
35 virtual void acknowledge_audio_element (Audio_element_info);
36 void process_music ();
37 void stop_translation_timestep ();
40 Audio_dynamic *audio_;
42 Music *span_start_event_;
43 Drul_array<Music *> span_events_;
44 vector<Audio_dynamic_tuple> dynamic_tuples_;
45 vector<Audio_dynamic_tuple> finished_dynamic_tuples_;
47 Direction finished_dir_;
50 Span_dynamic_performer::Span_dynamic_performer ()
52 span_events_[START] = 0;
53 span_events_[STOP] = 0;
54 span_start_event_ = 0;
60 Span_dynamic_performer::acknowledge_audio_element (Audio_element_info i)
62 if (Audio_dynamic *d = dynamic_cast<Audio_dynamic *> (i.elem_))
63 last_volume_ = d->volume_;
67 Span_dynamic_performer::process_music ()
69 if (span_start_event_ || span_events_[START])
71 audio_ = new Audio_dynamic (0);
72 Audio_element_info info (audio_, span_events_[START]
74 : span_events_[STOP]);
75 announce_element (info);
76 Audio_dynamic_tuple a = { audio_, now_mom () };
77 dynamic_tuples_.push_back (a);
80 if (span_events_[STOP])
82 if (!span_start_event_)
84 span_events_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
85 span_events_[STOP] = 0;
90 finished_dynamic_tuples_ = dynamic_tuples_;
92 dynamic_tuples_.clear ();
93 span_start_event_ = 0;
96 if (span_events_[START])
98 dir_ = (span_events_[START]->is_mus_type ("crescendo-event"))
100 span_start_event_ = span_events_[START];
102 dynamic_tuples_.clear ();
103 Audio_dynamic_tuple a = { audio_, now_mom () };
104 dynamic_tuples_.push_back (a);
107 if (span_events_[STOP])
108 finished_dynamic_tuples_.back ().audio_->volume_ = last_volume_;
110 if (span_events_[START])
111 dynamic_tuples_[0].audio_->volume_ = last_volume_;
113 span_events_[START] = 0;
114 span_events_[STOP] = 0;
118 Span_dynamic_performer::stop_translation_timestep ()
120 if (finished_dynamic_tuples_.size () > 1)
122 Real start_volume = finished_dynamic_tuples_[0].audio_->volume_;
123 Real dv = finished_dynamic_tuples_.back ().audio_->volume_
127 Catch and fix the case of:
131 f cresc. -- -- -- -- -- pp
133 Actually, we should provide a non-displayed dynamic/volume setting,
134 to set volume to 'ff' just before the pp.
136 if (!dv || sign (dv) != finished_dir_)
138 // urg. 20%: about two volume steps
139 dv = (Real)finished_dir_ * 0.2;
141 start_volume = finished_dynamic_tuples_.back ().audio_->volume_
144 Moment start_mom = finished_dynamic_tuples_[0].mom_;
145 Moment dt = finished_dynamic_tuples_.back ().mom_ - start_mom;
146 for (vsize i = 0; i < finished_dynamic_tuples_.size (); i++)
148 Audio_dynamic_tuple *a = &finished_dynamic_tuples_[i];
149 Real volume = start_volume + dv * (Real) (a->mom_ - start_mom).main_part_
150 / (Real)dt.main_part_;
151 a->audio_->volume_ = volume;
153 finished_dynamic_tuples_.clear ();
158 play_element (audio_);
162 span_events_[STOP] = 0;
163 span_events_[START] = 0;
167 Span_dynamic_performer::try_music (Music *r)
169 if (r->is_mus_type ("crescendo-event")
170 || r->is_mus_type ("decrescendo-event"))
172 Direction d = to_dir (r->get_property ("span-direction"));
178 #include "translator.icc"
180 ADD_TRANSLATOR (Span_dynamic_performer,
182 "crescendo-event decrescendo-event",