2 Copyright (C) 2000,2007 Paul Davis
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "pbd/failed_constructor.h"
24 #include "pbd/xml++.h"
26 #include "ardour/audioengine.h"
27 #include "ardour/audio_port.h"
28 #include "ardour/buffer_set.h"
29 #include "ardour/delivery.h"
30 #include "ardour/mtdm.h"
31 #include "ardour/plugin.h"
32 #include "ardour/port.h"
33 #include "ardour/port_insert.h"
34 #include "ardour/route.h"
35 #include "ardour/session.h"
36 #include "ardour/types.h"
41 using namespace ARDOUR;
45 PortInsert::name_and_id_new_insert (Session& s, uint32_t& bitslot)
47 bitslot = s.next_insert_id ();
48 return string_compose (_("insert %1"), bitslot+ 1);
51 PortInsert::PortInsert (Session& s, boost::shared_ptr<Pannable> pannable, boost::shared_ptr<MuteMaster> mm)
52 : IOProcessor (s, true, true, name_and_id_new_insert (s, _bitslot), "")
53 , _out (new Delivery (s, _output, pannable, mm, _name, Delivery::Insert))
56 _latency_detect = false;
57 _latency_flush_frames = false;
58 _measured_latency = 0;
61 PortInsert::PortInsert (Session& s, const std::string& name, uint32_t bslot, boost::shared_ptr<Pannable> pannable, boost::shared_ptr<MuteMaster> mm)
62 : IOProcessor (s, true, true, name, "")
63 , _out (new Delivery (s, _output, pannable, mm, _name, Delivery::Insert))
67 _latency_detect = false;
68 _latency_flush_frames = false;
69 _measured_latency = 0;
72 PortInsert::~PortInsert ()
74 _session.unmark_insert_id (_bitslot);
79 PortInsert::start_latency_detection ()
86 _latency_flush_frames = false;
87 _latency_detect = true;
88 _measured_latency = 0;
92 PortInsert::stop_latency_detection ()
94 _latency_flush_frames = signal_latency() + _session.engine().frames_per_cycle();
95 _latency_detect = false;
99 PortInsert::set_measured_latency (framecnt_t n)
101 _measured_latency = n;
105 PortInsert::latency() const
107 /* because we deliver and collect within the same cycle,
108 all I/O is necessarily delayed by at least frames_per_cycle().
110 if the return port for insert has its own latency, we
111 need to take that into account too.
114 if (_measured_latency == 0) {
115 return _session.engine().frames_per_cycle() + _input->latency();
117 return _measured_latency;
122 PortInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
124 if (_output->n_ports().n_total() == 0) {
128 if (_latency_detect) {
130 if (_input->n_ports().n_audio() != 0) {
132 AudioBuffer& outbuf (_output->ports().nth_audio_port(0)->get_audio_buffer (nframes));
133 Sample* in = _input->ports().nth_audio_port(0)->get_audio_buffer (nframes).data();
134 Sample* out = outbuf.data();
136 _mtdm->process (nframes, in, out);
138 outbuf.set_is_silent (false);
143 } else if (_latency_flush_frames) {
145 /* wait for the entire input buffer to drain before picking up input again so that we can't
146 hear the remnants of whatever MTDM pumped into the pipeline.
151 if (_latency_flush_frames > nframes) {
152 _latency_flush_frames -= nframes;
154 _latency_flush_frames = 0;
160 if (!_active && !_pending_active) {
161 /* deliver silence */
166 _out->run (bufs, start_frame, end_frame, nframes, true);
167 _input->collect_input (bufs, nframes, ChanCount::ZERO);
170 _active = _pending_active;
174 PortInsert::get_state(void)
180 PortInsert::state (bool full)
182 XMLNode& node = IOProcessor::state(full);
184 node.add_property ("type", "port");
185 snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
186 node.add_property ("bitslot", buf);
187 snprintf (buf, sizeof (buf), "%" PRId64, _measured_latency);
188 node.add_property("latency", buf);
189 snprintf (buf, sizeof (buf), "%u", _session.get_block_size());
190 node.add_property("block_size", buf);
196 PortInsert::set_state (const XMLNode& node, int version)
198 XMLNodeList nlist = node.children();
199 XMLNodeIterator niter;
200 XMLPropertyList plist;
201 const XMLProperty *prop;
203 const XMLNode* insert_node = &node;
205 // legacy sessions: search for child Redirect node
206 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
207 if ((*niter)->name() == "Redirect") {
208 insert_node = *niter;
213 IOProcessor::set_state (*insert_node, version);
215 if ((prop = node.property ("type")) == 0) {
216 error << _("XML node describing port insert is missing the `type' field") << endmsg;
220 if (prop->value() != "port") {
221 error << _("non-port insert XML used for port plugin insert") << endmsg;
225 uint32_t blocksize = 0;
226 if ((prop = node.property ("block_size")) != 0) {
227 sscanf (prop->value().c_str(), "%u", &blocksize);
230 //if the jack period is the same as when the value was saved, we can recall our latency..
231 if ( (_session.get_block_size() == blocksize) && (prop = node.property ("latency")) != 0) {
232 uint32_t latency = 0;
233 sscanf (prop->value().c_str(), "%u", &latency);
234 _measured_latency = latency;
237 if (!node.property ("ignore-bitslot")) {
238 if ((prop = node.property ("bitslot")) == 0) {
239 _bitslot = _session.next_insert_id();
241 _session.unmark_insert_id (_bitslot);
242 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
243 _session.mark_insert_id (_bitslot);
251 PortInsert::signal_latency() const
253 /* because we deliver and collect within the same cycle,
254 all I/O is necessarily delayed by at least frames_per_cycle().
256 if the return port for insert has its own latency, we
257 need to take that into account too.
260 if (_measured_latency == 0) {
261 return _session.engine().frames_per_cycle() + _input->signal_latency();
263 return _measured_latency;
267 /** Caller must hold process lock */
269 PortInsert::configure_io (ChanCount in, ChanCount out)
271 assert (!AudioEngine::instance()->process_lock().trylock());
273 /* for an insert, processor input corresponds to IO output, and vice versa */
275 if (_input->ensure_io (in, false, this) != 0) {
279 if (_output->ensure_io (out, false, this) != 0) {
283 return Processor::configure_io (in, out);
287 PortInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
294 PortInsert::set_name (const std::string& name)
296 bool ret = Processor::set_name (name);
298 ret = (ret && _input->set_name (name) && _output->set_name (name));
304 PortInsert::activate ()
306 IOProcessor::activate ();
312 PortInsert::deactivate ()
314 IOProcessor::deactivate ();
319 /** Set up the XML description of a send so that we will not
320 * reset its name or bitslot during ::set_state()
321 * @param state XML insert state.
325 PortInsert::make_unique (XMLNode &state)
327 state.add_property ("ignore-bitslot", "1");
328 state.add_property ("ignore-name", "1");