redesign how XML state, bitslots and names get propagated during copying a send/port...
[ardour.git] / libs / ardour / port_insert.cc
1 /*
2     Copyright (C) 2000,2007 Paul Davis
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
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <string>
21
22
23 #include "pbd/failed_constructor.h"
24 #include "pbd/xml++.h"
25
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"
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 string
45 PortInsert::name_and_id_new_insert (Session& s, uint32_t& bitslot)
46 {
47         bitslot = s.next_insert_id ();
48         return string_compose (_("insert %1"), bitslot+ 1);
49 }
50
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))
54 {
55         _mtdm = 0;
56         _latency_detect = false;
57         _latency_flush_frames = false;
58         _measured_latency = 0;
59 }
60
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))
64         , _bitslot (bslot)
65 {
66         _mtdm = 0;
67         _latency_detect = false;
68         _latency_flush_frames = false;
69         _measured_latency = 0;
70 }
71
72 PortInsert::~PortInsert ()
73 {
74         _session.unmark_insert_id (_bitslot);
75         delete _mtdm;
76 }
77
78 void
79 PortInsert::start_latency_detection ()
80 {
81         if (_mtdm != 0) {
82                 delete _mtdm;
83         }
84
85         _mtdm = new MTDM;
86         _latency_flush_frames = false;
87         _latency_detect = true;
88         _measured_latency = 0;
89 }
90
91 void
92 PortInsert::stop_latency_detection ()
93 {
94         _latency_flush_frames = signal_latency() + _session.engine().frames_per_cycle();
95         _latency_detect = false;
96 }
97
98 void
99 PortInsert::set_measured_latency (framecnt_t n)
100 {
101         _measured_latency = n;
102 }
103
104 framecnt_t
105 PortInsert::latency() const
106 {
107         /* because we deliver and collect within the same cycle,
108            all I/O is necessarily delayed by at least frames_per_cycle().
109
110            if the return port for insert has its own latency, we
111            need to take that into account too.
112         */
113
114         if (_measured_latency == 0) {
115                 return _session.engine().frames_per_cycle() + _input->latency();
116         } else {
117                 return _measured_latency;
118         }
119 }
120
121 void
122 PortInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
123 {
124         if (_output->n_ports().n_total() == 0) {
125                 return;
126         }
127
128         if (_latency_detect) {
129
130                 if (_input->n_ports().n_audio() != 0) {
131
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();
135
136                         _mtdm->process (nframes, in, out);
137
138                         outbuf.set_is_silent (false);
139                 }
140
141                 return;
142
143         } else if (_latency_flush_frames) {
144
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.
147                 */
148
149                 silence (nframes);
150
151                 if (_latency_flush_frames > nframes) {
152                         _latency_flush_frames -= nframes;
153                 } else {
154                         _latency_flush_frames = 0;
155                 }
156
157                 return;
158         }
159
160         if (!_active && !_pending_active) {
161                 /* deliver silence */
162                 silence (nframes);
163                 goto out;
164         }
165
166         _out->run (bufs, start_frame, end_frame, nframes, true);
167         _input->collect_input (bufs, nframes, ChanCount::ZERO);
168
169   out:
170         _active = _pending_active;
171 }
172
173 XMLNode&
174 PortInsert::get_state(void)
175 {
176         return state (true);
177 }
178
179 XMLNode&
180 PortInsert::state (bool full)
181 {
182         XMLNode& node = IOProcessor::state(full);
183         char buf[32];
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);
191
192         return node;
193 }
194
195 int
196 PortInsert::set_state (const XMLNode& node, int version)
197 {
198         XMLNodeList nlist = node.children();
199         XMLNodeIterator niter;
200         XMLPropertyList plist;
201         const XMLProperty *prop;
202
203         const XMLNode* insert_node = &node;
204
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;
209                         break;
210                 }
211         }
212
213         IOProcessor::set_state (*insert_node, version);
214
215         if ((prop = node.property ("type")) == 0) {
216                 error << _("XML node describing port insert is missing the `type' field") << endmsg;
217                 return -1;
218         }
219
220         if (prop->value() != "port") {
221                 error << _("non-port insert XML used for port plugin insert") << endmsg;
222                 return -1;
223         }
224
225         uint32_t blocksize = 0;
226         if ((prop = node.property ("block_size")) != 0) {
227                 sscanf (prop->value().c_str(), "%u", &blocksize);
228         }
229
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;
235         }
236
237         if (!node.property ("ignore-bitslot")) {
238                 if ((prop = node.property ("bitslot")) == 0) {
239                         _bitslot = _session.next_insert_id();
240                 } else {
241                         _session.unmark_insert_id (_bitslot);
242                         sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
243                         _session.mark_insert_id (_bitslot);
244                 }
245         }
246
247         return 0;
248 }
249
250 ARDOUR::framecnt_t
251 PortInsert::signal_latency() const
252 {
253         /* because we deliver and collect within the same cycle,
254            all I/O is necessarily delayed by at least frames_per_cycle().
255
256            if the return port for insert has its own latency, we
257            need to take that into account too.
258         */
259
260         if (_measured_latency == 0) {
261                 return _session.engine().frames_per_cycle() + _input->signal_latency();
262         } else {
263                 return _measured_latency;
264         }
265 }
266
267 /** Caller must hold process lock */
268 bool
269 PortInsert::configure_io (ChanCount in, ChanCount out)
270 {
271         assert (!AudioEngine::instance()->process_lock().trylock());
272
273         /* for an insert, processor input corresponds to IO output, and vice versa */
274
275         if (_input->ensure_io (in, false, this) != 0) {
276                 return false;
277         }
278
279         if (_output->ensure_io (out, false, this) != 0) {
280                 return false;
281         }
282
283         return Processor::configure_io (in, out);
284 }
285
286 bool
287 PortInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
288 {
289         out = in;
290         return true;
291 }
292
293 bool
294 PortInsert::set_name (const std::string& name)
295 {
296         bool ret = Processor::set_name (name);
297
298         ret = (ret && _input->set_name (name) && _output->set_name (name));
299
300         return ret;
301 }
302
303 void
304 PortInsert::activate ()
305 {
306         IOProcessor::activate ();
307
308         _out->activate ();
309 }
310
311 void
312 PortInsert::deactivate ()
313 {
314         IOProcessor::deactivate ();
315
316         _out->deactivate ();
317 }
318
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.
322  */
323
324 void
325 PortInsert::make_unique (XMLNode &state)
326 {
327         state.add_property ("ignore-bitslot", "1");
328         state.add_property ("ignore-name", "1");
329 }