+ if (!open_span_.dynamic_)
+ programming_error ("no open dynamic span");
+ else
+ {
+ DynamicQueue &dq
+ = (state_ == STATE_RETURN) ? return_queue_ : depart_queue_;
+
+ // Changing equalizer settings in the course of the performance does not
+ // seem very likely. This is a fig leaf: Equalize these limit volumes
+ // now as the required context properties are current. Note that only
+ // the limits at the end of the last span in the queue are kept.
+
+ // Resist diminishing to silence. (Idea: Look up "ppppp"
+ // with dynamicAbsoluteVolumeFunction, however that would yield 0.25.)
+ const Real min_target = equalize_volume (0.1);
+ const Real max_target
+ = equalize_volume (Audio_span_dynamic::MAXIMUM_VOLUME);
+
+ open_span_.dynamic_->set_end_moment (now_mom ());
+ dq.push_back (open_span_, min_target, max_target);
+ }
+
+ open_span_ = UnfinishedSpan ();
+}
+
+// Set the starting and target volume for each span in the queue. The gain
+// (loss) of any (de)crescendo is proportional to its share of the total time
+// spent changing.
+void
+Dynamic_performer::DynamicQueue::set_volume (Real start_vol,
+ Real target_vol)
+{
+ const Real gain = target_vol - start_vol;
+ Real dur = 0; // duration of (de)crescendi processed so far
+ Real vol = start_vol;
+ for (vector<UnfinishedSpan>::iterator it = spans_.begin ();
+ it != spans_.end (); ++it)
+ {
+ const Real prev_vol = vol;
+ if (it->grow_dir_ != CENTER)
+ {
+ // grant this (de)crescendo its portion of the gain
+ dur += it->dynamic_->get_duration ();
+ vol = start_vol + gain * (dur / change_duration_);
+ }
+ it->dynamic_->set_volume (prev_vol, vol);
+ }
+}
+
+// Return a volume which is reasonably distant from the given start and end
+// volumes in the given direction, for use as a peak volume in a passage with a
+// crescendo followed by a decrescendo (or vice versa). If the given volumes
+// are equal, the returned volume is a also reasonable target volume for a
+// single (de)crescendo.
+//
+// The given minimum and maximum volumes are the allowable dynamic range.
+Real
+Dynamic_performer::compute_departure_volume (Direction depart_dir,
+ Real start_vol,
+ Real end_vol,
+ Real min_vol,
+ Real max_vol)
+{
+ if (depart_dir == CENTER)
+ return start_vol;
+
+ // Try to find a volume that is a minimum distance from the starting and
+ // ending volumes. If the endpoint volumes differ, the nearer one is padded
+ // less than the farther one.
+ //
+ // Example: mf < ... > p. The legacy behavior was to use a 25% of the
+ // dynamic range for a (de)crescendo to an unspecified target, and this tries
+ // to preserve that, but is not possible to use a 25% change for both the
+ // crescendo and the decrescendo and meet the constraints of this example.
+ // The decrescendo is a greater change than the crescendo. Believing that
+ // 25% is already more than enough for either, pad using 25% for the greater
+ // change and 7% for the lesser change.
+ //
+ // Idea: Use a context property or callback, e.g. the difference between two
+ // dynamics in dynamicAbsoluteVolumeFunction. 0.25 is the default difference
+ // between "p" and "ff". (Isn't that rather wide for this purpose?) 0.07 is
+ // the default difference between "mp" and "mf".
+ const Real far_padding = 0.25;
+ const Real near_padding = 0.07;
+
+ // If for some reason one of the endpoints is already below the supposed
+ // minimum or maximum, just accept it.
+ min_vol = min (min (min_vol, start_vol), end_vol);
+ max_vol = max (max (max_vol, start_vol), end_vol);
+
+ const Real vol_range = max_vol - min_vol;
+
+ const Real near_vol = minmax (depart_dir, start_vol, end_vol)
+ + depart_dir * near_padding * vol_range;
+ const Real far_vol = minmax (-depart_dir, start_vol, end_vol)
+ + depart_dir * far_padding * vol_range;
+ const Real depart_vol = minmax (depart_dir, near_vol, far_vol);
+ return max (min (depart_vol, max_vol), min_vol);
+}
+
+Real
+Dynamic_performer::finish_queued_spans (Real next_vol)
+{
+ if (depart_queue_.spans_.empty ())
+ {
+ programming_error ("no dynamic span to finish");
+ return next_vol;
+ }
+
+ const Real start_vol = depart_queue_.spans_.front ().dynamic_->get_start_volume ();
+
+ if (return_queue_.spans_.empty ())