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;
44 PortInsert::PortInsert (Session& s, boost::shared_ptr<MuteMaster> mm)
45 : IOProcessor (s, true, true, string_compose (_("insert %1"), (bitslot = s.next_insert_id()) + 1), "")
46 , _out (new Delivery (s, _output, mm, _name, Delivery::Insert))
49 _latency_detect = false;
50 _latency_flush_frames = false;
51 _measured_latency = 0;
54 PortInsert::~PortInsert ()
56 _session.unmark_insert_id (bitslot);
61 PortInsert::start_latency_detection ()
68 _latency_flush_frames = false;
69 _latency_detect = true;
70 _measured_latency = 0;
74 PortInsert::stop_latency_detection ()
76 _latency_flush_frames = signal_latency() + _session.engine().frames_per_cycle();
77 _latency_detect = false;
81 PortInsert::set_measured_latency (nframes_t n)
83 _measured_latency = n;
87 PortInsert::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes, bool)
89 if (_output->n_ports().n_total() == 0) {
93 if (_latency_detect) {
95 if (_input->n_ports().n_audio() != 0) {
97 AudioBuffer& outbuf (_output->ports().nth_audio_port(0)->get_audio_buffer (nframes));
98 Sample* in = _input->ports().nth_audio_port(0)->get_audio_buffer (nframes).data();
99 Sample* out = outbuf.data();
101 _mtdm->process (nframes, in, out);
103 outbuf.is_silent (false);
108 } else if (_latency_flush_frames) {
110 /* wait for the entire input buffer to drain before picking up input again so that we can't
111 hear the remnants of whatever MTDM pumped into the pipeline.
116 if (_latency_flush_frames > nframes) {
117 _latency_flush_frames -= nframes;
119 _latency_flush_frames = 0;
125 if (!_active && !_pending_active) {
126 /* deliver silence */
131 _out->run (bufs, start_frame, end_frame, nframes, true);
132 _input->collect_input (bufs, nframes, ChanCount::ZERO);
135 _active = _pending_active;
139 PortInsert::get_state(void)
145 PortInsert::state (bool full)
147 XMLNode& node = Processor::state(full);
149 node.add_property ("type", "port");
150 snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
151 node.add_property ("bitslot", buf);
157 PortInsert::set_state (const XMLNode& node, int version)
159 XMLNodeList nlist = node.children();
160 XMLNodeIterator niter;
161 XMLPropertyList plist;
162 const XMLProperty *prop;
164 const XMLNode* insert_node = &node;
166 // legacy sessions: search for child IOProcessor node
167 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
168 if ((*niter)->name() == "IOProcessor") {
169 insert_node = *niter;
174 Processor::set_state (*insert_node, version);
176 if ((prop = node.property ("type")) == 0) {
177 error << _("XML node describing port insert is missing the `type' field") << endmsg;
181 if (prop->value() != "port") {
182 error << _("non-port insert XML used for port plugin insert") << endmsg;
186 if ((prop = node.property ("bitslot")) == 0) {
187 bitslot = _session.next_insert_id();
189 _session.unmark_insert_id (bitslot);
190 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
191 _session.mark_insert_id (bitslot);
198 PortInsert::signal_latency() const
200 /* because we deliver and collect within the same cycle,
201 all I/O is necessarily delayed by at least frames_per_cycle().
203 if the return port for insert has its own latency, we
204 need to take that into account too.
207 if (_measured_latency == 0) {
208 return _session.engine().frames_per_cycle() + _input->signal_latency();
210 return _measured_latency;
215 PortInsert::configure_io (ChanCount in, ChanCount out)
217 /* for an insert, processor input corresponds to IO output, and vice versa */
219 if (_input->ensure_io (in, false, this) != 0) {
223 if (_output->ensure_io (out, false, this) != 0) {
227 return Processor::configure_io (in, out);
231 PortInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
238 PortInsert::set_name (const std::string& name)
240 bool ret = Processor::set_name (name);
242 ret = (ret && _input->set_name (name) && _output->set_name (name));
248 PortInsert::activate ()
250 IOProcessor::activate ();
256 PortInsert::deactivate ()
258 IOProcessor::deactivate ();