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/configuration.h"
30 #include "ardour/io.h"
31 #include "ardour/midi_buffer.h"
32 #include "ardour/session.h"
36 using namespace ARDOUR
;
42 , _apply_gain_automation(false)
45 boost::shared_ptr
<AutomationList
> gl(new AutomationList(Evoral::Parameter(GainAutomation
)));
46 _gain_control
= boost::shared_ptr
<GainControl
>( new GainControl(X_("gaincontrol"), s
, this, Evoral::Parameter(GainAutomation
), gl
));
47 add_control(_gain_control
);
51 Amp::display_name() const
57 Amp::can_support_io_configuration (const ChanCount
& in
, ChanCount
& out
) const
64 Amp::configure_io (ChanCount in
, ChanCount out
)
66 if (out
!= in
) { // always 1:1
70 return Processor::configure_io (in
, out
);
74 Amp::run (BufferSet
& bufs
, framepos_t
/*start_frame*/, framepos_t
/*end_frame*/, pframes_t nframes
, bool)
76 if (!_active
&& !_pending_active
) {
82 if (_apply_gain_automation
) {
84 gain_t
* gab
= _session
.gain_automation_buffer ();
86 for (BufferSet::audio_iterator i
= bufs
.audio_begin(); i
!= bufs
.audio_end(); ++i
) {
87 Sample
* const sp
= i
->data();
88 for (pframes_t nx
= 0; nx
< nframes
; ++nx
) {
93 _current_gain
= gab
[nframes
-1];
95 } else { /* manual (scalar) gain */
97 gain_t
const dg
= _gain_control
->user_double();
99 if (_current_gain
!= dg
) {
101 Amp::apply_gain (bufs
, nframes
, _current_gain
, dg
);
104 } else if (_current_gain
!= 1.0f
) {
106 /* gain has not changed, but its non-unity
109 for (BufferSet::midi_iterator i
= bufs
.midi_begin(); i
!= bufs
.midi_end(); ++i
) {
113 for (MidiBuffer::iterator m
= mb
.begin(); m
!= mb
.end(); ++m
) {
114 Evoral::MIDIEvent
<MidiBuffer::TimeType
> ev
= *m
;
115 if (ev
.is_note_on()) {
116 ev
.scale_velocity (_current_gain
);
121 for (BufferSet::audio_iterator i
= bufs
.audio_begin(); i
!= bufs
.audio_end(); ++i
) {
122 apply_gain_to_buffer (i
->data(), nframes
, _current_gain
);
128 _active
= _pending_active
;
132 Amp::apply_gain (BufferSet
& bufs
, framecnt_t nframes
, gain_t initial
, gain_t target
)
134 /** Apply a (potentially) declicked gain to the buffers of @a bufs
137 if (nframes
== 0 || bufs
.count().n_total() == 0) {
141 // if we don't need to declick, defer to apply_simple_gain
142 if (initial
== target
) {
143 apply_simple_gain (bufs
, nframes
, target
);
147 const framecnt_t declick
= std::min ((framecnt_t
) 128, nframes
);
149 double fractional_shift
= -1.0/declick
;
150 double fractional_pos
;
152 if (target
< initial
) {
153 /* fade out: remove more and more of delta from initial */
154 delta
= -(initial
- target
);
156 /* fade in: add more and more of delta from initial */
157 delta
= target
- initial
;
162 for (BufferSet::midi_iterator i
= bufs
.midi_begin(); i
!= bufs
.midi_end(); ++i
) {
166 for (MidiBuffer::iterator m
= mb
.begin(); m
!= mb
.end(); ++m
) {
167 Evoral::MIDIEvent
<MidiBuffer::TimeType
> ev
= *m
;
169 if (ev
.is_note_on()) {
170 gain_t scale
= delta
* (ev
.time()/(double) nframes
);
171 ev
.scale_velocity (initial
+scale
);
178 for (BufferSet::audio_iterator i
= bufs
.audio_begin(); i
!= bufs
.audio_end(); ++i
) {
179 Sample
* const buffer
= i
->data();
181 fractional_pos
= 1.0;
183 for (pframes_t nx
= 0; nx
< declick
; ++nx
) {
184 buffer
[nx
] *= (initial
+ (delta
* (0.5 + 0.5 * cos (M_PI
* fractional_pos
))));
185 fractional_pos
+= fractional_shift
;
188 /* now ensure the rest of the buffer has the target value applied, if necessary. */
190 if (declick
!= nframes
) {
193 memset (&buffer
[declick
], 0, sizeof (Sample
) * (nframes
- declick
));
194 } else if (target
!= 1.0) {
195 apply_gain_to_buffer (&buffer
[declick
], nframes
- declick
, target
);
202 Amp::declick (BufferSet
& bufs
, framecnt_t nframes
, int dir
)
204 /* Almost exactly like ::apply_gain() but skips MIDI buffers and has fixed initial+target
208 if (nframes
== 0 || bufs
.count().n_total() == 0) {
212 const framecnt_t declick
= std::min ((framecnt_t
) 128, nframes
);
213 gain_t delta
, initial
, target
;
214 double fractional_shift
= -1.0/(declick
-1);
215 double fractional_pos
;
218 /* fade out: remove more and more of delta from initial */
223 /* fade in: add more and more of delta from initial */
231 for (BufferSet::audio_iterator i
= bufs
.audio_begin(); i
!= bufs
.audio_end(); ++i
) {
232 Sample
* const buffer
= i
->data();
234 fractional_pos
= 1.0;
236 for (pframes_t nx
= 0; nx
< declick
; ++nx
) {
237 buffer
[nx
] *= (initial
+ (delta
* (0.5 + 0.5 * cos (M_PI
* fractional_pos
))));
238 fractional_pos
+= fractional_shift
;
241 /* now ensure the rest of the buffer has the target value applied, if necessary. */
243 if (declick
!= nframes
) {
246 memset (&buffer
[declick
], 0, sizeof (Sample
) * (nframes
- declick
));
247 } else if (target
!= 1.0) {
248 apply_gain_to_buffer (&buffer
[declick
], nframes
- declick
, target
);
255 Amp::apply_gain (AudioBuffer
& buf
, framecnt_t nframes
, gain_t initial
, gain_t target
)
257 /** Apply a (potentially) declicked gain to the contents of @a buf
264 // if we don't need to declick, defer to apply_simple_gain
265 if (initial
== target
) {
266 apply_simple_gain (buf
, nframes
, target
);
270 const framecnt_t declick
= std::min ((framecnt_t
) 128, nframes
);
272 double fractional_shift
= -1.0/declick
;
273 double fractional_pos
;
275 if (target
< initial
) {
276 /* fade out: remove more and more of delta from initial */
277 delta
= -(initial
- target
);
279 /* fade in: add more and more of delta from initial */
280 delta
= target
- initial
;
284 Sample
* const buffer
= buf
.data();
286 fractional_pos
= 1.0;
288 for (pframes_t nx
= 0; nx
< declick
; ++nx
) {
289 buffer
[nx
] *= (initial
+ (delta
* (0.5 + 0.5 * cos (M_PI
* fractional_pos
))));
290 fractional_pos
+= fractional_shift
;
293 /* now ensure the rest of the buffer has the target value applied, if necessary. */
295 if (declick
!= nframes
) {
298 memset (&buffer
[declick
], 0, sizeof (Sample
) * (nframes
- declick
));
299 } else if (target
!= 1.0) {
300 apply_gain_to_buffer (&buffer
[declick
], nframes
- declick
, target
);
306 Amp::apply_simple_gain (BufferSet
& bufs
, framecnt_t nframes
, gain_t target
)
310 for (BufferSet::midi_iterator i
= bufs
.midi_begin(); i
!= bufs
.midi_end(); ++i
) {
313 for (MidiBuffer::iterator m
= mb
.begin(); m
!= mb
.end(); ++m
) {
314 Evoral::MIDIEvent
<MidiBuffer::TimeType
> ev
= *m
;
315 if (ev
.is_note_on()) {
321 for (BufferSet::audio_iterator i
= bufs
.audio_begin(); i
!= bufs
.audio_end(); ++i
) {
322 memset (i
->data(), 0, sizeof (Sample
) * nframes
);
325 } else if (target
!= 1.0) {
327 for (BufferSet::midi_iterator i
= bufs
.midi_begin(); i
!= bufs
.midi_end(); ++i
) {
330 for (MidiBuffer::iterator m
= mb
.begin(); m
!= mb
.end(); ++m
) {
331 Evoral::MIDIEvent
<MidiBuffer::TimeType
> ev
= *m
;
332 if (ev
.is_note_on()) {
333 ev
.scale_velocity (target
);
338 for (BufferSet::audio_iterator i
= bufs
.audio_begin(); i
!= bufs
.audio_end(); ++i
) {
339 apply_gain_to_buffer (i
->data(), nframes
, target
);
345 Amp::apply_simple_gain (AudioBuffer
& buf
, framecnt_t nframes
, gain_t target
)
348 memset (buf
.data(), 0, sizeof (Sample
) * nframes
);
349 } else if (target
!= 1.0) {
350 apply_gain_to_buffer (buf
.data(), nframes
, target
);
355 Amp::inc_gain (gain_t factor
, void *src
)
357 float desired_gain
= _gain_control
->user_double();
359 if (desired_gain
== 0.0f
) {
360 set_gain (0.000001f
+ (0.000001f
* factor
), src
);
362 set_gain (desired_gain
+ (desired_gain
* factor
), src
);
367 Amp::set_gain (gain_t val
, void *src
)
369 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
370 if (val
> 1.99526231f
) {
374 //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
376 if (src
!= _gain_control
.get()) {
377 _gain_control
->set_value (val
);
378 // bit twisty, this will come back and call us again
379 // (this keeps control in sync with reality)
383 _gain_control
->set_double(val
, false);
384 _session
.set_dirty();
388 Amp::state (bool full_state
)
390 XMLNode
& node (Processor::state (full_state
));
391 node
.add_property("type", "amp");
392 node
.add_child_nocopy (_gain_control
->get_state());
398 Amp::set_state (const XMLNode
& node
, int version
)
402 Processor::set_state (node
, version
);
404 if ((gain_node
= node
.child (Controllable::xml_node_name
.c_str())) != 0) {
405 _gain_control
->set_state (*gain_node
, version
);
412 Amp::GainControl::set_value (double val
)
414 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
415 if (val
> 1.99526231f
) {
419 _amp
->set_gain (val
, this);
421 AutomationControl::set_value(val
);
425 Amp::GainControl::get_value (void) const
427 return AutomationControl::get_value();
431 Amp::setup_gain_automation (framepos_t start_frame
, framepos_t end_frame
, framecnt_t nframes
)
433 Glib::Mutex::Lock
am (control_lock(), Glib::TRY_LOCK
);
435 if (am
.locked() && _session
.transport_rolling() && _gain_control
->automation_playback()) {
436 _apply_gain_automation
= _gain_control
->list()->curve().rt_safe_get_vector (
437 start_frame
, end_frame
, _session
.gain_automation_buffer(), nframes
);
439 _apply_gain_automation
= false;