1 /*
2 * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include "ardour/amp.h"
20 #include "ardour/audio_buffer.h"
21 #include "ardour/phase_control.h"
22 #include "ardour/polarity_processor.h"
23 #include "ardour/session.h"
24
25 #include "pbd/i18n.h"
26
27 using namespace ARDOUR;
28 using namespace PBD;
29
PolarityProcessor(Session & s,boost::shared_ptr<PhaseControl> control)30 PolarityProcessor::PolarityProcessor (Session& s, boost::shared_ptr<PhaseControl> control)
31 : Processor(s, "Polarity")
32 , _control (control)
33 {
34 }
35
36 bool
can_support_io_configuration(const ChanCount & in,ChanCount & out)37 PolarityProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out)
38 {
39 out = in;
40 return true;
41 }
42
43 bool
configure_io(ChanCount in,ChanCount out)44 PolarityProcessor::configure_io (ChanCount in, ChanCount out)
45 {
46 if (out != in) { // always 1:1
47 return false;
48 }
49
50 _control->resize (in.n_audio ());
51 _current_gain.resize (in.n_audio (), 1.f);
52
53 return Processor::configure_io (in, out);
54 }
55
56 void
run(BufferSet & bufs,samplepos_t,samplepos_t,double,pframes_t nframes,bool)57 PolarityProcessor::run (BufferSet& bufs, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, double /*speed*/, pframes_t nframes, bool)
58 {
59 size_t chn = 0;
60 assert (bufs.count().n_audio () <= _current_gain.size());
61 if (!_active && !_pending_active) {
62 /* fade all to unity */
63 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
64 _current_gain[chn] = Amp::apply_gain (*i, _session.nominal_sample_rate(), nframes, _current_gain[chn], 1.0);
65 }
66 return;
67 }
68 _active = _pending_active;
69
70 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
71 _current_gain[chn] = Amp::apply_gain (*i, _session.nominal_sample_rate(), nframes, _current_gain[chn], _control->inverted (chn) ? -1.f : 1.f);
72 }
73 }
74
75 XMLNode&
state()76 PolarityProcessor::state ()
77 {
78 XMLNode& node (Processor::state ());
79 node.set_property("type", "polarity");
80 return node;
81 }
82