second (and hopefully) final part of changes to respond to header format changes...
[ArdourMidi.git] / libs / ardour / amp.cc
bloba739b59088c160c74ce320640f265cb04dd7cdbd
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;
38 Amp::Amp (Session& s)
39 : Processor(s, "Amp")
40 , _apply_gain(true)
41 , _apply_gain_automation(false)
42 , _current_gain(1.0)
44 boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(GainAutomation)));
45 _gain_control = boost::shared_ptr<GainControl>( new GainControl(X_("gaincontrol"), s, this, Evoral::Parameter(GainAutomation), gl ));
46 add_control(_gain_control);
49 std::string
50 Amp::display_name() const
52 return _("Fader");
55 bool
56 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
58 out = in;
59 return true;
62 bool
63 Amp::configure_io (ChanCount in, ChanCount out)
65 if (out != in) { // always 1:1
66 return false;
69 return Processor::configure_io (in, out);
72 void
73 Amp::run (BufferSet& bufs, sframes_t /*start_frame*/, sframes_t /*end_frame*/, nframes_t nframes, bool)
75 if (!_active && !_pending_active) {
76 return;
79 if (_apply_gain) {
81 if (_apply_gain_automation) {
83 gain_t* gab = _session.gain_automation_buffer ();
85 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
86 Sample* const sp = i->data();
87 for (nframes_t nx = 0; nx < nframes; ++nx) {
88 sp[nx] *= gab[nx];
92 _current_gain = gab[nframes-1];
94 } else { /* manual (scalar) gain */
96 gain_t const dg = _gain_control->user_float();
98 if (_current_gain != dg) {
100 Amp::apply_gain (bufs, nframes, _current_gain, dg);
101 _current_gain = dg;
103 } else if (_current_gain != 1.0f) {
105 /* gain has not changed, but its non-unity
108 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
110 MidiBuffer& mb (*i);
112 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
113 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
114 if (ev.is_note_on()) {
115 ev.scale_velocity (_current_gain);
120 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
121 apply_gain_to_buffer (i->data(), nframes, _current_gain);
127 _active = _pending_active;
130 void
131 Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target)
133 /** Apply a (potentially) declicked gain to the buffers of @a bufs
136 if (nframes == 0 || bufs.count().n_total() == 0) {
137 return;
140 // if we don't need to declick, defer to apply_simple_gain
141 if (initial == target) {
142 apply_simple_gain (bufs, nframes, target);
143 return;
146 const nframes_t declick = std::min ((nframes_t)128, nframes);
147 gain_t delta;
148 double fractional_shift = -1.0/declick;
149 double fractional_pos;
151 if (target < initial) {
152 /* fade out: remove more and more of delta from initial */
153 delta = -(initial - target);
154 } else {
155 /* fade in: add more and more of delta from initial */
156 delta = target - initial;
159 /* MIDI Gain */
161 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 (nframes_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::apply_gain (AudioBuffer& buf, nframes_t nframes, gain_t initial, gain_t target)
204 /** Apply a (potentially) declicked gain to the contents of @a buf
207 if (nframes == 0) {
208 return;
211 // if we don't need to declick, defer to apply_simple_gain
212 if (initial == target) {
213 apply_simple_gain (buf, nframes, target);
214 return;
217 const nframes_t declick = std::min ((nframes_t)128, nframes);
218 gain_t delta;
219 double fractional_shift = -1.0/declick;
220 double fractional_pos;
222 if (target < initial) {
223 /* fade out: remove more and more of delta from initial */
224 delta = -(initial - target);
225 } else {
226 /* fade in: add more and more of delta from initial */
227 delta = target - initial;
231 Sample* const buffer = buf.data();
233 fractional_pos = 1.0;
235 for (nframes_t nx = 0; nx < declick; ++nx) {
236 buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
237 fractional_pos += fractional_shift;
240 /* now ensure the rest of the buffer has the target value applied, if necessary. */
242 if (declick != nframes) {
244 if (target == 0.0) {
245 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
246 } else if (target != 1.0) {
247 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
252 void
253 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
255 if (target == 0.0) {
257 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
258 MidiBuffer& mb (*i);
260 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
261 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
262 if (ev.is_note_on()) {
263 ev.set_velocity (0);
268 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
269 memset (i->data(), 0, sizeof (Sample) * nframes);
272 } else if (target != 1.0) {
274 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
275 MidiBuffer& mb (*i);
277 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
278 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
279 if (ev.is_note_on()) {
280 ev.scale_velocity (target);
285 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
286 apply_gain_to_buffer (i->data(), nframes, target);
291 void
292 Amp::apply_simple_gain (AudioBuffer& buf, nframes_t nframes, gain_t target)
294 if (target == 0.0) {
295 memset (buf.data(), 0, sizeof (Sample) * nframes);
296 } else if (target != 1.0) {
297 apply_gain_to_buffer (buf.data(), nframes, target);
301 void
302 Amp::inc_gain (gain_t factor, void *src)
304 float desired_gain = _gain_control->user_float();
306 if (desired_gain == 0.0f) {
307 set_gain (0.000001f + (0.000001f * factor), src);
308 } else {
309 set_gain (desired_gain + (desired_gain * factor), src);
313 void
314 Amp::set_gain (gain_t val, void *src)
316 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
317 if (val > 1.99526231f) {
318 val = 1.99526231f;
321 //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
323 if (src != _gain_control.get()) {
324 _gain_control->set_value (val);
325 // bit twisty, this will come back and call us again
326 // (this keeps control in sync with reality)
327 return;
330 _gain_control->set_float(val, false);
331 _session.set_dirty();
334 XMLNode&
335 Amp::state (bool full_state)
337 XMLNode& node (Processor::state (full_state));
338 node.add_property("type", "amp");
340 char buf[32];
341 snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
342 node.add_property("gain", buf);
344 return node;
348 Amp::set_state (const XMLNode& node, int version)
350 const XMLProperty* prop;
352 Processor::set_state (node, version);
353 prop = node.property ("gain");
355 if (prop) {
356 gain_t val;
358 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
359 _gain_control->set_value (val);
363 return 0;
366 void
367 Amp::GainControl::set_value (float val)
369 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
370 if (val > 1.99526231f)
371 val = 1.99526231f;
373 _amp->set_gain (val, this);
375 AutomationControl::set_value(val);
378 float
379 Amp::GainControl::get_value (void) const
381 return AutomationControl::get_value();
384 void
385 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
387 Glib::Mutex::Lock am (control_lock(), Glib::TRY_LOCK);
389 if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
390 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
391 start_frame, end_frame, _session.gain_automation_buffer(), nframes);
392 } else {
393 _apply_gain_automation = false;
397 bool
398 Amp::visible() const
400 return true;