make sure that fade in and fade out curves reach their target (1.0 and 0.0) rather...
[ardour2.git] / libs / ardour / amp.cc
blob5f305182f8c8472a1fbd7160e943b4b93ec7d739
1 /*
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)
7 any later version.
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
12 for more details.
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.
19 #include <iostream>
20 #include <cstring>
21 #include <cmath>
22 #include <algorithm>
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"
34 #include "i18n.h"
36 using namespace ARDOUR;
37 using namespace PBD;
39 Amp::Amp (Session& s)
40 : Processor(s, "Amp")
41 , _apply_gain(true)
42 , _apply_gain_automation(false)
43 , _current_gain(1.0)
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);
50 std::string
51 Amp::display_name() const
53 return _("Fader");
56 bool
57 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
59 out = in;
60 return true;
63 bool
64 Amp::configure_io (ChanCount in, ChanCount out)
66 if (out != in) { // always 1:1
67 return false;
70 return Processor::configure_io (in, out);
73 void
74 Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
76 if (!_active && !_pending_active) {
77 return;
80 if (_apply_gain) {
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) {
89 sp[nx] *= gab[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);
102 _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) {
111 MidiBuffer& mb (*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;
131 void
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) {
138 return;
141 // if we don't need to declick, defer to apply_simple_gain
142 if (initial == target) {
143 apply_simple_gain (bufs, nframes, target);
144 return;
147 const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
148 gain_t delta;
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);
155 } else {
156 /* fade in: add more and more of delta from initial */
157 delta = target - initial;
160 /* MIDI Gain */
162 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
164 MidiBuffer& mb (*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);
176 /* Audio Gain */
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) {
192 if (target == 0.0) {
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);
201 void
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
205 values.
208 if (nframes == 0 || bufs.count().n_total() == 0) {
209 return;
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;
217 if (dir < 0) {
218 /* fade out: remove more and more of delta from initial */
219 delta = -1.0;
220 initial = 1.0;
221 target = 0.0;
222 } else {
223 /* fade in: add more and more of delta from initial */
224 delta = 1.0;
225 initial = 0.0;
226 target = 1.0;
229 /* Audio Gain */
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) {
245 if (target == 0.0) {
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);
254 void
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
260 if (nframes == 0) {
261 return;
264 // if we don't need to declick, defer to apply_simple_gain
265 if (initial == target) {
266 apply_simple_gain (buf, nframes, target);
267 return;
270 const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
271 gain_t delta;
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);
278 } else {
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) {
297 if (target == 0.0) {
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);
305 void
306 Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target)
308 if (target == 0.0) {
310 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
311 MidiBuffer& mb (*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()) {
316 ev.set_velocity (0);
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) {
328 MidiBuffer& mb (*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);
344 void
345 Amp::apply_simple_gain (AudioBuffer& buf, framecnt_t nframes, gain_t target)
347 if (target == 0.0) {
348 memset (buf.data(), 0, sizeof (Sample) * nframes);
349 } else if (target != 1.0) {
350 apply_gain_to_buffer (buf.data(), nframes, target);
354 void
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);
361 } else {
362 set_gain (desired_gain + (desired_gain * factor), src);
366 void
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) {
371 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)
380 return;
383 _gain_control->set_double(val, false);
384 _session.set_dirty();
387 XMLNode&
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());
394 return node;
398 Amp::set_state (const XMLNode& node, int version)
400 XMLNode* gain_node;
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);
408 return 0;
411 void
412 Amp::GainControl::set_value (double val)
414 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
415 if (val > 1.99526231f) {
416 val = 1.99526231f;
419 _amp->set_gain (val, this);
421 AutomationControl::set_value(val);
424 double
425 Amp::GainControl::get_value (void) const
427 return AutomationControl::get_value();
430 void
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);
438 } else {
439 _apply_gain_automation = false;
443 bool
444 Amp::visible() const
446 return true;