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 ()
63 _session.unmark_insert_id (_bitslot);
68 PortInsert::start_latency_detection ()
75 _latency_flush_frames = false;
76 _latency_detect = true;
77 _measured_latency = 0;
81 PortInsert::stop_latency_detection ()
83 _latency_flush_frames = signal_latency() + _session.engine().frames_per_cycle();
84 _latency_detect = false;
88 PortInsert::set_measured_latency (framecnt_t n)
90 _measured_latency = n;
94 PortInsert::latency() const
96 /* because we deliver and collect within the same cycle,
97 all I/O is necessarily delayed by at least frames_per_cycle().
99 if the return port for insert has its own latency, we
100 need to take that into account too.
103 if (_measured_latency == 0) {
104 return _session.engine().frames_per_cycle() + _input->latency();
106 return _measured_latency;
111 PortInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
113 if (_output->n_ports().n_total() == 0) {
117 if (_latency_detect) {
119 if (_input->n_ports().n_audio() != 0) {
121 AudioBuffer& outbuf (_output->ports().nth_audio_port(0)->get_audio_buffer (nframes));
122 Sample* in = _input->ports().nth_audio_port(0)->get_audio_buffer (nframes).data();
123 Sample* out = outbuf.data();
125 _mtdm->process (nframes, in, out);
127 outbuf.set_is_silent (false);
132 } else if (_latency_flush_frames) {
134 /* wait for the entire input buffer to drain before picking up input again so that we can't
135 hear the remnants of whatever MTDM pumped into the pipeline.
140 if (_latency_flush_frames > nframes) {
141 _latency_flush_frames -= nframes;
143 _latency_flush_frames = 0;
149 if (!_active && !_pending_active) {
150 /* deliver silence */
155 _out->run (bufs, start_frame, end_frame, nframes, true);
156 _input->collect_input (bufs, nframes, ChanCount::ZERO);
159 _active = _pending_active;
163 PortInsert::get_state(void)
169 PortInsert::state (bool full)
171 XMLNode& node = IOProcessor::state(full);
173 node.add_property ("type", "port");
174 snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
175 node.add_property ("bitslot", buf);
176 snprintf (buf, sizeof (buf), "%" PRId64, _measured_latency);
177 node.add_property("latency", buf);
178 snprintf (buf, sizeof (buf), "%u", _session.get_block_size());
179 node.add_property("block_size", buf);
185 PortInsert::set_state (const XMLNode& node, int version)
187 XMLNodeList nlist = node.children();
188 XMLNodeIterator niter;
189 XMLPropertyList plist;
190 const XMLProperty *prop;
192 const XMLNode* insert_node = &node;
194 // legacy sessions: search for child Redirect node
195 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
196 if ((*niter)->name() == "Redirect") {
197 insert_node = *niter;
202 IOProcessor::set_state (*insert_node, version);
204 if ((prop = node.property ("type")) == 0) {
205 error << _("XML node describing port insert is missing the `type' field") << endmsg;
209 if (prop->value() != "port") {
210 error << _("non-port insert XML used for port plugin insert") << endmsg;
214 uint32_t blocksize = 0;
215 if ((prop = node.property ("block_size")) != 0) {
216 sscanf (prop->value().c_str(), "%u", &blocksize);
219 //if the jack period is the same as when the value was saved, we can recall our latency..
220 if ( (_session.get_block_size() == blocksize) && (prop = node.property ("latency")) != 0) {
221 uint32_t latency = 0;
222 sscanf (prop->value().c_str(), "%u", &latency);
223 _measured_latency = latency;
226 if (!node.property ("ignore-bitslot")) {
227 if ((prop = node.property ("bitslot")) == 0) {
228 _bitslot = _session.next_insert_id();
230 _session.unmark_insert_id (_bitslot);
231 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
232 _session.mark_insert_id (_bitslot);
240 PortInsert::signal_latency() const
242 /* because we deliver and collect within the same cycle,
243 all I/O is necessarily delayed by at least frames_per_cycle().
245 if the return port for insert has its own latency, we
246 need to take that into account too.
249 if (_measured_latency == 0) {
250 return _session.engine().frames_per_cycle() + _input->signal_latency();
252 return _measured_latency;
256 /** Caller must hold process lock */
258 PortInsert::configure_io (ChanCount in, ChanCount out)
260 assert (!AudioEngine::instance()->process_lock().trylock());
262 /* for an insert, processor input corresponds to IO output, and vice versa */
264 if (_input->ensure_io (in, false, this) != 0) {
268 if (_output->ensure_io (out, false, this) != 0) {
272 return Processor::configure_io (in, out);
276 PortInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
283 PortInsert::set_name (const std::string& name)
285 bool ret = Processor::set_name (name);
287 ret = (ret && _input->set_name (name) && _output->set_name (name));
293 PortInsert::activate ()
295 IOProcessor::activate ();
301 PortInsert::deactivate ()
303 IOProcessor::deactivate ();