2
Copyright (C) 2006 Paul Davis
4
This program is free software; you can redistribute it and/or modify it
5
under the terms of the GNU General Public License as published by the Free
6
Software Foundation; either version 2 of the License, or (at your option)
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
You should have received a copy of the GNU General Public License along
15
with this program; if not, write to the Free Software Foundation, Inc.,
16
675 Mass Ave, Cambridge, MA 02139, USA.
24
#include "evoral/Curve.hpp"
26
#include "ardour/amp.h"
27
#include "ardour/audio_buffer.h"
28
#include "ardour/buffer_set.h"
29
#include "ardour/midi_buffer.h"
30
#include "ardour/session.h"
34
using namespace ARDOUR;
38
/* gain range of -inf to +6dB, default 0dB */
39
const float Amp::max_gain_coefficient = 1.99526231f;
44
, _apply_gain_automation(false)
46
, _gain_automation_buffer(0)
48
Evoral::Parameter p (GainAutomation);
49
p.set_range (0, max_gain_coefficient, 1, false);
50
boost::shared_ptr<AutomationList> gl (new AutomationList (p));
51
_gain_control = boost::shared_ptr<GainControl> (new GainControl (X_("gaincontrol"), s, this, p, gl));
52
_gain_control->set_flags (Controllable::GainLike);
54
add_control(_gain_control);
58
Amp::display_name() const
64
Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out)
71
Amp::configure_io (ChanCount in, ChanCount out)
73
if (out != in) { // always 1:1
77
return Processor::configure_io (in, out);
81
Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
83
if (!_active && !_pending_active) {
89
if (_apply_gain_automation) {
91
gain_t* gab = _gain_automation_buffer;
94
for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
95
Sample* const sp = i->data();
96
for (pframes_t nx = 0; nx < nframes; ++nx) {
101
_current_gain = gab[nframes-1];
103
} else { /* manual (scalar) gain */
105
gain_t const dg = _gain_control->user_double();
107
if (_current_gain != dg) {
109
Amp::apply_gain (bufs, nframes, _current_gain, dg);
112
} else if (_current_gain != 1.0f) {
114
/* gain has not changed, but its non-unity
117
for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
121
for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
122
Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
123
if (ev.is_note_on()) {
124
ev.scale_velocity (_current_gain);
129
for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
130
apply_gain_to_buffer (i->data(), nframes, _current_gain);
136
_active = _pending_active;
140
Amp::apply_gain (BufferSet& bufs, framecnt_t nframes, gain_t initial, gain_t target)
142
/** Apply a (potentially) declicked gain to the buffers of @a bufs
145
if (nframes == 0 || bufs.count().n_total() == 0) {
149
// if we don't need to declick, defer to apply_simple_gain
150
if (initial == target) {
151
apply_simple_gain (bufs, nframes, target);
155
const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
157
double fractional_shift = -1.0/declick;
158
double fractional_pos;
160
if (target < initial) {
161
/* fade out: remove more and more of delta from initial */
162
delta = -(initial - target);
164
/* fade in: add more and more of delta from initial */
165
delta = target - initial;
170
for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
174
for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
175
Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
177
if (ev.is_note_on()) {
178
const gain_t scale = delta * (ev.time()/(double) nframes);
179
ev.scale_velocity (initial+scale);
186
for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
187
Sample* const buffer = i->data();
189
fractional_pos = 1.0;
191
for (pframes_t nx = 0; nx < declick; ++nx) {
192
buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
193
fractional_pos += fractional_shift;
196
/* now ensure the rest of the buffer has the target value applied, if necessary. */
198
if (declick != nframes) {
201
memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
202
} else if (target != 1.0) {
203
apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
210
Amp::declick (BufferSet& bufs, framecnt_t nframes, int dir)
212
/* Almost exactly like ::apply_gain() but skips MIDI buffers and has fixed initial+target
216
if (nframes == 0 || bufs.count().n_total() == 0) {
220
const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
221
gain_t delta, initial, target;
222
double fractional_shift = -1.0/(declick-1);
223
double fractional_pos;
226
/* fade out: remove more and more of delta from initial */
231
/* fade in: add more and more of delta from initial */
239
for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
240
Sample* const buffer = i->data();
242
fractional_pos = 1.0;
244
for (pframes_t nx = 0; nx < declick; ++nx) {
245
buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
246
fractional_pos += fractional_shift;
249
/* now ensure the rest of the buffer has the target value applied, if necessary. */
251
if (declick != nframes) {
254
memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
255
} else if (target != 1.0) {
256
apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
263
Amp::apply_gain (AudioBuffer& buf, framecnt_t nframes, gain_t initial, gain_t target)
265
/** Apply a (potentially) declicked gain to the contents of @a buf
272
// if we don't need to declick, defer to apply_simple_gain
273
if (initial == target) {
274
apply_simple_gain (buf, nframes, target);
278
const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
280
double fractional_shift = -1.0/declick;
281
double fractional_pos;
283
if (target < initial) {
284
/* fade out: remove more and more of delta from initial */
285
delta = -(initial - target);
287
/* fade in: add more and more of delta from initial */
288
delta = target - initial;
292
Sample* const buffer = buf.data();
294
fractional_pos = 1.0;
296
for (pframes_t nx = 0; nx < declick; ++nx) {
297
buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
298
fractional_pos += fractional_shift;
301
/* now ensure the rest of the buffer has the target value applied, if necessary. */
303
if (declick != nframes) {
306
memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
307
} else if (target != 1.0) {
308
apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
314
Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target)
318
for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
321
for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
322
Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
323
if (ev.is_note_on()) {
329
for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
330
memset (i->data(), 0, sizeof (Sample) * nframes);
333
} else if (target != 1.0) {
335
for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
338
for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
339
Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
340
if (ev.is_note_on()) {
341
ev.scale_velocity (target);
346
for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
347
apply_gain_to_buffer (i->data(), nframes, target);
353
Amp::apply_simple_gain (AudioBuffer& buf, framecnt_t nframes, gain_t target)
356
memset (buf.data(), 0, sizeof (Sample) * nframes);
357
} else if (target != 1.0) {
358
apply_gain_to_buffer (buf.data(), nframes, target);
363
Amp::inc_gain (gain_t factor, void *src)
365
float desired_gain = _gain_control->user_double();
367
if (desired_gain == 0.0f) {
368
set_gain (0.000001f + (0.000001f * factor), src);
370
set_gain (desired_gain + (desired_gain * factor), src);
375
Amp::set_gain (gain_t val, void *src)
377
val = min (val, max_gain_coefficient);
379
if (src != _gain_control.get()) {
380
_gain_control->set_value (val);
381
// bit twisty, this will come back and call us again
382
// (this keeps control in sync with reality)
386
_gain_control->set_double (val);
387
_session.set_dirty();
391
Amp::state (bool full_state)
393
XMLNode& node (Processor::state (full_state));
394
node.add_property("type", "amp");
395
node.add_child_nocopy (_gain_control->get_state());
401
Amp::set_state (const XMLNode& node, int version)
405
Processor::set_state (node, version);
407
if ((gain_node = node.child (Controllable::xml_node_name.c_str())) != 0) {
408
_gain_control->set_state (*gain_node, version);
415
Amp::GainControl::set_value (double val)
417
if (val > max_gain_coefficient) {
418
val = max_gain_coefficient;
421
_amp->set_gain (val, this);
423
AutomationControl::set_value(val);
427
Amp::GainControl::internal_to_interface (double v) const
429
return gain_to_slider_position (v);
433
Amp::GainControl::interface_to_internal (double v) const
435
return slider_position_to_gain (v);
439
Amp::GainControl::internal_to_user (double v) const
441
return accurate_coefficient_to_dB (v);
444
/** Write gain automation for this cycle into the buffer previously passed in to
445
* set_gain_automation_buffer (if we are in automation playback mode and the
446
* transport is rolling).
449
Amp::setup_gain_automation (framepos_t start_frame, framepos_t end_frame, framecnt_t nframes)
451
Glib::Threads::Mutex::Lock am (control_lock(), Glib::Threads::TRY_LOCK);
453
if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
454
assert (_gain_automation_buffer);
455
_apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
456
start_frame, end_frame, _gain_automation_buffer, nframes);
458
_apply_gain_automation = false;
469
Amp::value_as_string (boost::shared_ptr<AutomationControl> ac) const
471
if (ac == _gain_control) {
473
snprintf (buffer, sizeof (buffer), "%.2fdB", ac->internal_to_user (ac->get_value ()));
477
return Automatable::value_as_string (ac);
480
/** Sets up the buffer that setup_gain_automation and ::run will use for
481
* gain automationc curves. Must be called before setup_gain_automation,
482
* and must be called with process lock held.
486
Amp::set_gain_automation_buffer (gain_t* g)
488
_gain_automation_buffer = g;