2 * Copyright (C) 2000-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2007-2012 David Robillard <d@drobilla.net>
4 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "pbd/xml++.h"
26 #include "ardour/audio_port.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/delivery.h"
29 #include "ardour/io.h"
30 #include "ardour/mtdm.h"
31 #include "ardour/port_insert.h"
32 #include "ardour/session.h"
33 #include "ardour/types.h"
38 using namespace ARDOUR;
42 PortInsert::name_and_id_new_insert (Session& s, uint32_t& bitslot)
44 bitslot = s.next_insert_id ();
45 return string_compose (_("insert %1"), bitslot+ 1);
48 PortInsert::PortInsert (Session& s, boost::shared_ptr<Pannable> pannable, boost::shared_ptr<MuteMaster> mm)
49 : IOProcessor (s, true, true, name_and_id_new_insert (s, _bitslot), "", DataType::AUDIO, true)
50 , _out (new Delivery (s, _output, pannable, mm, _name, Delivery::Insert))
53 _latency_detect = false;
54 _latency_flush_samples = 0;
55 _measured_latency = 0;
58 PortInsert::~PortInsert ()
60 _session.unmark_insert_id (_bitslot);
65 PortInsert::set_pre_fader (bool p)
67 Processor::set_pre_fader (p);
68 _out->set_pre_fader (p);
72 PortInsert::start_latency_detection ()
75 _mtdm = new MTDM (_session.sample_rate());
76 _latency_flush_samples = 0;
77 _latency_detect = true;
78 _measured_latency = 0;
82 PortInsert::stop_latency_detection ()
84 _latency_flush_samples = effective_latency() + _session.engine().samples_per_cycle();
85 _latency_detect = false;
89 PortInsert::set_measured_latency (samplecnt_t n)
91 _measured_latency = n;
95 PortInsert::latency() const
97 /* because we deliver and collect within the same cycle,
98 all I/O is necessarily delayed by at least samples_per_cycle().
100 if the return port for insert has its own latency, we
101 need to take that into account too.
104 if (_measured_latency == 0) {
105 return _session.engine().samples_per_cycle() + _input->latency();
107 return _measured_latency;
112 PortInsert::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool)
114 if (_output->n_ports().n_total() == 0) {
118 if (_latency_detect) {
120 if (_input->n_ports().n_audio() != 0) {
122 AudioBuffer& outbuf (_output->ports().nth_audio_port(0)->get_audio_buffer (nframes));
123 Sample* in = _input->ports().nth_audio_port(0)->get_audio_buffer (nframes).data();
124 Sample* out = outbuf.data();
126 _mtdm->process (nframes, in, out);
128 outbuf.set_written (true);
133 } else if (_latency_flush_samples) {
135 /* wait for the entire input buffer to drain before picking up input again so that we can't
136 * hear the remnants of whatever MTDM pumped into the pipeline.
139 silence (nframes, start_sample);
141 if (_latency_flush_samples > nframes) {
142 _latency_flush_samples -= nframes;
144 _latency_flush_samples = 0;
150 if (!_active && !_pending_active) {
151 /* deliver silence */
152 silence (nframes, start_sample);
156 _out->run (bufs, start_sample, end_sample, speed, nframes, true);
157 _input->collect_input (bufs, nframes, ChanCount::ZERO);
160 _active = _pending_active;
166 XMLNode& node = IOProcessor::state ();
167 node.set_property ("type", "port");
168 node.set_property ("bitslot", _bitslot);
169 node.set_property ("latency", _measured_latency);
170 node.set_property ("block-size", _session.get_block_size());
176 PortInsert::set_state (const XMLNode& node, int version)
178 XMLNodeList nlist = node.children();
179 XMLNodeIterator niter;
180 XMLPropertyList plist;
182 const XMLNode* insert_node = &node;
184 // legacy sessions: search for child Redirect node
185 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
186 if ((*niter)->name() == "Redirect") {
187 insert_node = *niter;
192 IOProcessor::set_state (*insert_node, version);
194 std::string type_str;
195 if (!node.get_property ("type", type_str)) {
196 error << _("XML node describing port insert is missing the `type' field") << endmsg;
200 if (type_str != "port") {
201 error << _("non-port insert XML used for port plugin insert") << endmsg;
205 uint32_t blocksize = 0;
206 node.get_property ("block-size", blocksize);
208 //if the jack period is the same as when the value was saved, we can recall our latency..
209 if ( (_session.get_block_size() == blocksize) ) {
210 node.get_property ("latency", _measured_latency);
213 if (!node.property ("ignore-bitslot")) {
215 if (node.get_property ("bitslot", bitslot)) {
216 _session.unmark_insert_id (_bitslot);
218 _session.mark_insert_id (_bitslot);
220 _bitslot = _session.next_insert_id();
228 PortInsert::signal_latency() const
230 /* because we deliver and collect within the same cycle,
231 * all I/O is necessarily delayed by at least samples_per_cycle().
233 * if the return port for insert has its own latency, we
234 * need to take that into account too.
237 if (_measured_latency == 0) {
238 return _session.engine().samples_per_cycle()
239 + _input->connected_latency (false);
240 + _output->connected_latency (true);
242 return _measured_latency;
246 /** Caller must hold process lock */
248 PortInsert::configure_io (ChanCount in, ChanCount out)
250 #ifndef PLATFORM_WINDOWS
251 assert (!AudioEngine::instance()->process_lock().trylock());
254 /* for an insert, processor input corresponds to IO output, and vice versa */
256 if (_input->ensure_io (in, false, this) != 0) {
260 if (_output->ensure_io (out, false, this) != 0) {
264 return Processor::configure_io (in, out);
268 PortInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out)
275 PortInsert::set_name (const std::string& name)
277 bool ret = Processor::set_name (name);
279 ret = (ret && _input->set_name (name) && _output->set_name (name));
285 PortInsert::activate ()
287 IOProcessor::activate ();
293 PortInsert::deactivate ()
295 IOProcessor::deactivate ();