+Amp::apply_gain (AudioBuffer& buf, framecnt_t nframes, gain_t initial, gain_t target)
+{
+ /** Apply a (potentially) declicked gain to the contents of @a buf
+ */
+
+ if (nframes == 0) {
+ return;
+ }
+
+ // if we don't need to declick, defer to apply_simple_gain
+ if (initial == target) {
+ apply_simple_gain (buf, nframes, target);
+ return;
+ }
+
+ const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
+ gain_t delta;
+ double fractional_shift = -1.0/declick;
+ double fractional_pos;
+
+ if (target < initial) {
+ /* fade out: remove more and more of delta from initial */
+ delta = -(initial - target);
+ } else {
+ /* fade in: add more and more of delta from initial */
+ delta = target - initial;
+ }
+
+
+ Sample* const buffer = buf.data();
+
+ fractional_pos = 1.0;
+
+ for (pframes_t nx = 0; nx < declick; ++nx) {
+ buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
+ fractional_pos += fractional_shift;
+ }
+
+ /* now ensure the rest of the buffer has the target value applied, if necessary. */
+
+ if (declick != nframes) {
+
+ if (target == 0.0) {
+ memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
+ } else if (target != 1.0) {
+ apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
+ }
+ }
+}
+
+void
+Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target)
+{
+ if (target == 0.0) {
+
+ for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
+ MidiBuffer& mb (*i);
+
+ for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
+ Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
+ if (ev.is_note_on()) {
+ ev.set_velocity (0);
+ }
+ }
+ }
+
+ for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
+ memset (i->data(), 0, sizeof (Sample) * nframes);
+ }
+
+ } else if (target != 1.0) {
+
+ for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
+ MidiBuffer& mb (*i);
+
+ for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
+ Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
+ if (ev.is_note_on()) {
+ ev.scale_velocity (target);
+ }
+ }
+ }
+
+ for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
+ apply_gain_to_buffer (i->data(), nframes, target);
+ }
+ }
+}
+
+void
+Amp::apply_simple_gain (AudioBuffer& buf, framecnt_t nframes, gain_t target)
+{
+ if (target == 0.0) {
+ memset (buf.data(), 0, sizeof (Sample) * nframes);
+ } else if (target != 1.0) {
+ apply_gain_to_buffer (buf.data(), nframes, target);
+ }
+}
+
+void
+Amp::inc_gain (gain_t factor, void *src)
+{
+ float desired_gain = _gain_control->user_double();
+
+ if (desired_gain == 0.0f) {
+ set_gain (0.000001f + (0.000001f * factor), src);
+ } else {
+ set_gain (desired_gain + (desired_gain * factor), src);
+ }
+}
+
+void
+Amp::set_gain (gain_t val, void *)