lilypond-1.5.9
[lilypond.git] / lily / span-dynamic-performer.cc
blobf76f7a88764ec641e8a718ea78ab28122cd44b71
1 /*
2 span-dynamic-performer.cc -- implement Span_dynamic_performer
4 source file of the GNU LilyPond music typesetter
6 (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
9 #include "performer.hh"
10 #include "command-request.hh"
11 #include "musical-request.hh"
12 #include "audio-item.hh"
14 struct Audio_dynamic_tuple
16 Audio_dynamic* audio_l_;
17 Moment mom_;
20 /**
21 perform span-dynamics
23 class Span_dynamic_performer : public Performer
25 public:
26 VIRTUAL_COPY_CONS (Translator);
28 Span_dynamic_performer ();
30 protected:
31 virtual bool try_music (Music*);
32 virtual void acknowledge_audio_element (Audio_element_info);
33 virtual void process_music ();
34 virtual void stop_translation_timestep ();
35 virtual void start_translation_timestep ();
37 private:
38 Audio_dynamic* audio_p_;
39 Real last_volume_;
40 Span_req* span_start_req_l_;
41 Drul_array<Span_req*> span_req_l_drul_;
42 Array<Audio_dynamic_tuple> dynamic_tuple_arr_;
43 Array<Audio_dynamic_tuple> finished_dynamic_tuple_arr_;
44 Direction dir_;
45 Direction finished_dir_;
48 ADD_THIS_TRANSLATOR (Span_dynamic_performer);
50 Span_dynamic_performer::Span_dynamic_performer ()
52 span_req_l_drul_[START] = 0;
53 span_req_l_drul_[STOP] = 0;
54 span_start_req_l_ = 0;
55 audio_p_ = 0;
56 last_volume_ = 0;
59 void
60 Span_dynamic_performer::acknowledge_audio_element (Audio_element_info i)
62 if (Audio_dynamic * d = dynamic_cast <Audio_dynamic*> (i.elem_l_))
64 last_volume_ = d->volume_;
68 void
69 Span_dynamic_performer::process_music ()
71 if (span_start_req_l_ || span_req_l_drul_[START])
73 audio_p_ = new Audio_dynamic (0);
74 Audio_element_info info (audio_p_, span_req_l_drul_[START]
75 ? span_req_l_drul_[START]
76 : span_req_l_drul_[STOP]);
77 announce_element (info);
78 Audio_dynamic_tuple a = { audio_p_, now_mom () };
79 dynamic_tuple_arr_.push (a);
82 if (span_req_l_drul_[STOP])
84 if (!span_start_req_l_)
86 span_req_l_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
87 span_req_l_drul_[STOP] = 0;
89 else
91 finished_dir_ = dir_;
92 finished_dynamic_tuple_arr_ = dynamic_tuple_arr_;
94 dynamic_tuple_arr_.clear ();
95 span_start_req_l_ = 0;
98 if (span_req_l_drul_[START])
100 String t = ly_scm2string (span_req_l_drul_[START]->get_mus_property ("span-type"));
101 dir_ = (t == "crescendo") ? RIGHT : LEFT;
102 span_start_req_l_ = span_req_l_drul_[START];
104 dynamic_tuple_arr_.clear ();
105 Audio_dynamic_tuple a = { audio_p_, now_mom () };
106 dynamic_tuple_arr_.push (a);
110 if (span_req_l_drul_[STOP])
112 finished_dynamic_tuple_arr_.top ().audio_l_->volume_ = last_volume_;
115 if (span_req_l_drul_[START])
117 dynamic_tuple_arr_[0].audio_l_->volume_ = last_volume_;
119 span_start_req_l_ = 0;
120 span_req_l_drul_[START] = 0;
121 span_req_l_drul_[STOP] = 0;
124 void
125 Span_dynamic_performer::stop_translation_timestep ()
127 if (finished_dynamic_tuple_arr_.size () > 1)
129 Real start_volume = finished_dynamic_tuple_arr_[0].audio_l_->volume_;
130 Real dv = finished_dynamic_tuple_arr_.top ().audio_l_->volume_
131 - start_volume;
133 urg.
134 Catch and fix the case of:
137 x| x|
138 f cresc. -- -- -- -- -- pp
140 Actually, we should provide a non-displayed dynamic/volume setting,
141 to set volume to 'ff' just before the pp.
143 if (!dv || sign (dv) != finished_dir_)
145 // urg. 20%: about two volume steps
146 dv = (Real)finished_dir_ * 0.2;
147 if (!start_volume)
148 start_volume = finished_dynamic_tuple_arr_.top
149 ().audio_l_->volume_ - dv;
151 Moment start_mom = finished_dynamic_tuple_arr_[0].mom_;
152 Moment dt = finished_dynamic_tuple_arr_.top ().mom_ - start_mom;
153 for (int i=0; i < finished_dynamic_tuple_arr_.size (); i++)
155 Audio_dynamic_tuple* a = &finished_dynamic_tuple_arr_[i];
156 Real volume = start_volume + dv * (Real) (a->mom_ - start_mom).main_part_
157 / (Real)dt.main_part_;
158 a->audio_l_->volume_ = volume;
160 finished_dynamic_tuple_arr_.clear ();
163 if (audio_p_)
165 play_element (audio_p_);
166 audio_p_ = 0;
170 void
171 Span_dynamic_performer::start_translation_timestep ()
173 span_req_l_drul_[STOP] = 0;
174 span_req_l_drul_[START] = 0;
177 bool
178 Span_dynamic_performer::try_music (Music* r)
180 if (Span_req * s = dynamic_cast<Span_req*> (r))
182 String t = ly_scm2string (s->get_mus_property ("span-type"));
183 if (t == "crescendo" || t == "decrescendo")
185 span_req_l_drul_[s->get_span_dir ()] = s;
186 return true;
189 return false;