fb688c6f84f72c13cda8f79e49caf61921de008b
[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 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))
47 {
48         _mtdm = 0;
49         _latency_detect = false;
50         _latency_flush_frames = false;
51         _measured_latency = 0;
52
53         ProcessorCreated (this); /* EMIT SIGNAL */
54 }
55
56 PortInsert::PortInsert (Session& s, boost::shared_ptr<MuteMaster> mm, const XMLNode& node)
57         : IOProcessor (s, true, true, "unnamed port insert")
58         , _out (new Delivery (s, _output, mm, _name, Delivery::Insert))
59
60 {
61         _mtdm = 0;
62         _latency_detect = false;
63         _latency_flush_frames = false;
64         _measured_latency = 0;
65         
66         if (set_state (node, Stateful::loading_state_version)) {
67                 throw failed_constructor();
68         }
69
70         ProcessorCreated (this); /* EMIT SIGNAL */
71 }
72
73 PortInsert::~PortInsert ()
74 {
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 (nframes_t n)
100 {
101         _measured_latency = n;
102 }
103
104 void
105 PortInsert::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes, bool)
106 {
107         if (_output->n_ports().n_total() == 0) {
108                 return;
109         }
110
111         if (_latency_detect) {
112                 
113                 if (_input->n_ports().n_audio() != 0) {
114
115                         AudioBuffer& outbuf (_output->ports().nth_audio_port(0)->get_audio_buffer (nframes));
116                         Sample* in = _input->ports().nth_audio_port(0)->get_audio_buffer (nframes).data();
117                         Sample* out = outbuf.data();
118                         
119                         _mtdm->process (nframes, in, out);
120                         
121                         outbuf.is_silent (false);
122                 }
123                 
124                 return;
125                 
126         } else if (_latency_flush_frames) {
127                 
128                 /* wait for the entire input buffer to drain before picking up input again so that we can't
129                    hear the remnants of whatever MTDM pumped into the pipeline.
130                 */
131                 
132                 silence (nframes);
133                 
134                 if (_latency_flush_frames > nframes) {
135                         _latency_flush_frames -= nframes;
136                 } else {
137                         _latency_flush_frames = 0;
138                 }
139                 
140                 return;
141         }
142         
143         if (!_active && !_pending_active) {
144                 /* deliver silence */
145                 silence (nframes);
146                 goto out;
147         }
148
149         _out->run (bufs, start_frame, end_frame, nframes, true);
150         _input->collect_input (bufs, nframes, ChanCount::ZERO);
151
152   out:
153         _active = _pending_active;
154 }
155
156 XMLNode&
157 PortInsert::get_state(void)
158 {
159         return state (true);
160 }
161
162 XMLNode&
163 PortInsert::state (bool full)
164 {
165         XMLNode& node = Processor::state(full);
166         char buf[32];
167         node.add_property ("type", "port");
168         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
169         node.add_property ("bitslot", buf);
170
171         return node;
172 }
173
174 int
175 PortInsert::set_state (const XMLNode& node, int version)
176 {
177         XMLNodeList nlist = node.children();
178         XMLNodeIterator niter;
179         XMLPropertyList plist;
180         const XMLProperty *prop;
181
182         if ((prop = node.property ("type")) == 0) {
183                 error << _("XML node describing port insert is missing the `type' field") << endmsg;
184                 return -1;
185         }
186
187         if (prop->value() != "port") {
188                 error << _("non-port insert XML used for port plugin insert") << endmsg;
189                 return -1;
190         }
191
192         if ((prop = node.property ("bitslot")) == 0) {
193                 bitslot = _session.next_insert_id();
194         } else {
195                 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
196                 _session.mark_insert_id (bitslot);
197         }
198
199         const XMLNode* insert_node = &node;
200
201         // legacy sessions: search for child IOProcessor node
202         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
203                 if ((*niter)->name() == "IOProcessor") {
204                         insert_node = *niter;
205                         break;
206                 }
207         }
208
209         Processor::set_state (*insert_node, version);
210
211         return 0;
212 }
213
214 ARDOUR::nframes_t
215 PortInsert::signal_latency() const
216 {
217         /* because we deliver and collect within the same cycle,
218            all I/O is necessarily delayed by at least frames_per_cycle().
219
220            if the return port for insert has its own latency, we
221            need to take that into account too.
222         */
223
224         if (_measured_latency == 0) {
225                 return _session.engine().frames_per_cycle() + _input->signal_latency();
226         } else {
227                 return _measured_latency;
228         }
229 }
230
231 bool
232 PortInsert::configure_io (ChanCount in, ChanCount out)
233 {
234         /* for an insert, processor input corresponds to IO output, and vice versa */
235
236         if (_input->ensure_io (in, false, this) != 0) {
237                 return false;
238         }
239
240         if (_output->ensure_io (out, false, this) != 0) {
241                 return false;
242         }
243
244         return Processor::configure_io (in, out);
245 }
246
247 bool
248 PortInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
249 {
250         out = in;
251         return true;
252 }
253
254 bool
255 PortInsert::set_name (const std::string& name)
256 {
257         bool ret = Processor::set_name (name);
258
259         ret = (ret && _input->set_name (name) && _output->set_name (name));
260
261         return ret;
262 }
263
264 void
265 PortInsert::activate ()
266 {
267         IOProcessor::activate ();
268
269         _out->activate ();
270 }
271
272 void
273 PortInsert::deactivate ()
274 {
275         IOProcessor::deactivate ();
276
277         _out->deactivate ();
278 }