Merge branch 'master' into windows
[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 #include "pbd/xml++.h"
23
24 #include "ardour/audio_port.h"
25 #include "ardour/audioengine.h"
26 #include "ardour/delivery.h"
27 #include "ardour/io.h"
28 #include "ardour/mtdm.h"
29 #include "ardour/port_insert.h"
30 #include "ardour/session.h"
31 #include "ardour/types.h"
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 string
40 PortInsert::name_and_id_new_insert (Session& s, uint32_t& bitslot)
41 {
42         bitslot = s.next_insert_id ();
43         return string_compose (_("insert %1"), bitslot+ 1);
44 }
45
46 PortInsert::PortInsert (Session& s, boost::shared_ptr<Pannable> pannable, boost::shared_ptr<MuteMaster> mm)
47         : IOProcessor (s, true, true, name_and_id_new_insert (s, _bitslot), "", DataType::AUDIO, true)
48         , _out (new Delivery (s, _output, pannable, mm, _name, Delivery::Insert))
49 {
50         _mtdm = 0;
51         _latency_detect = false;
52         _latency_flush_frames = 0;
53         _measured_latency = 0;
54 }
55
56 PortInsert::~PortInsert ()
57 {
58         _session.unmark_insert_id (_bitslot);
59         delete _mtdm;
60 }
61
62 void
63 PortInsert::start_latency_detection ()
64 {
65         delete _mtdm;
66         _mtdm = new MTDM (_session.frame_rate());
67         _latency_flush_frames = 0;
68         _latency_detect = true;
69         _measured_latency = 0;
70 }
71
72 void
73 PortInsert::stop_latency_detection ()
74 {
75         _latency_flush_frames = signal_latency() + _session.engine().samples_per_cycle();
76         _latency_detect = false;
77 }
78
79 void
80 PortInsert::set_measured_latency (framecnt_t n)
81 {
82         _measured_latency = n;
83 }
84
85 framecnt_t
86 PortInsert::latency() const
87 {
88         /* because we deliver and collect within the same cycle,
89            all I/O is necessarily delayed by at least frames_per_cycle().
90
91            if the return port for insert has its own latency, we
92            need to take that into account too.
93         */
94
95         if (_measured_latency == 0) {
96                 return _session.engine().samples_per_cycle() + _input->latency();
97         } else {
98                 return _measured_latency;
99         }
100 }
101
102 void
103 PortInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
104 {
105         if (_output->n_ports().n_total() == 0) {
106                 return;
107         }
108
109         if (_latency_detect) {
110
111                 if (_input->n_ports().n_audio() != 0) {
112
113                         AudioBuffer& outbuf (_output->ports().nth_audio_port(0)->get_audio_buffer (nframes));
114                         Sample* in = _input->ports().nth_audio_port(0)->get_audio_buffer (nframes).data();
115                         Sample* out = outbuf.data();
116
117                         _mtdm->process (nframes, in, out);
118                         
119                         outbuf.set_is_silent (false);
120                         outbuf.set_written (true);
121                 }
122
123                 return;
124
125         } else if (_latency_flush_frames) {
126
127                 /* wait for the entire input buffer to drain before picking up input again so that we can't
128                    hear the remnants of whatever MTDM pumped into the pipeline.
129                 */
130
131                 silence (nframes);
132
133                 if (_latency_flush_frames > nframes) {
134                         _latency_flush_frames -= nframes;
135                 } else {
136                         _latency_flush_frames = 0;
137                 }
138
139                 return;
140         }
141
142         if (!_active && !_pending_active) {
143                 /* deliver silence */
144                 silence (nframes);
145                 goto out;
146         }
147
148         _out->run (bufs, start_frame, end_frame, nframes, true);
149         _input->collect_input (bufs, nframes, ChanCount::ZERO);
150
151   out:
152         _active = _pending_active;
153 }
154
155 XMLNode&
156 PortInsert::get_state(void)
157 {
158         return state (true);
159 }
160
161 XMLNode&
162 PortInsert::state (bool full)
163 {
164         XMLNode& node = IOProcessor::state(full);
165         char buf[32];
166         node.add_property ("type", "port");
167         snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
168         node.add_property ("bitslot", buf);
169         snprintf (buf, sizeof (buf), "%" PRId64, _measured_latency);
170         node.add_property("latency", buf);
171         snprintf (buf, sizeof (buf), "%u", _session.get_block_size());
172         node.add_property("block_size", buf);
173
174         return node;
175 }
176
177 int
178 PortInsert::set_state (const XMLNode& node, int version)
179 {
180         XMLNodeList nlist = node.children();
181         XMLNodeIterator niter;
182         XMLPropertyList plist;
183         const XMLProperty *prop;
184
185         const XMLNode* insert_node = &node;
186
187         // legacy sessions: search for child Redirect node
188         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
189                 if ((*niter)->name() == "Redirect") {
190                         insert_node = *niter;
191                         break;
192                 }
193         }
194
195         IOProcessor::set_state (*insert_node, version);
196
197         if ((prop = node.property ("type")) == 0) {
198                 error << _("XML node describing port insert is missing the `type' field") << endmsg;
199                 return -1;
200         }
201
202         if (prop->value() != "port") {
203                 error << _("non-port insert XML used for port plugin insert") << endmsg;
204                 return -1;
205         }
206
207         uint32_t blocksize = 0;
208         if ((prop = node.property ("block_size")) != 0) {
209                 sscanf (prop->value().c_str(), "%u", &blocksize);
210         }
211
212         //if the jack period is the same as when the value was saved, we can recall our latency..
213         if ( (_session.get_block_size() == blocksize) && (prop = node.property ("latency")) != 0) {
214                 uint32_t latency = 0;
215                 sscanf (prop->value().c_str(), "%u", &latency);
216                 _measured_latency = latency;
217         }
218
219         if (!node.property ("ignore-bitslot")) {
220                 if ((prop = node.property ("bitslot")) == 0) {
221                         _bitslot = _session.next_insert_id();
222                 } else {
223                         _session.unmark_insert_id (_bitslot);
224                         sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
225                         _session.mark_insert_id (_bitslot);
226                 }
227         }
228
229         return 0;
230 }
231
232 ARDOUR::framecnt_t
233 PortInsert::signal_latency() const
234 {
235         /* because we deliver and collect within the same cycle,
236            all I/O is necessarily delayed by at least frames_per_cycle().
237
238            if the return port for insert has its own latency, we
239            need to take that into account too.
240         */
241
242         if (_measured_latency == 0) {
243                 return _session.engine().samples_per_cycle() + _input->signal_latency();
244         } else {
245                 return _measured_latency;
246         }
247 }
248
249 /** Caller must hold process lock */
250 bool
251 PortInsert::configure_io (ChanCount in, ChanCount out)
252 {
253 #ifndef PLATFORM_WINDOWS
254         assert (!AudioEngine::instance()->process_lock().trylock());
255 #endif
256
257         /* for an insert, processor input corresponds to IO output, and vice versa */
258
259         if (_input->ensure_io (in, false, this) != 0) {
260                 return false;
261         }
262
263         if (_output->ensure_io (out, false, this) != 0) {
264                 return false;
265         }
266
267         return Processor::configure_io (in, out);
268 }
269
270 bool
271 PortInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out)
272 {
273         out = in;
274         return true;
275 }
276
277 bool
278 PortInsert::set_name (const std::string& name)
279 {
280         bool ret = Processor::set_name (name);
281
282         ret = (ret && _input->set_name (name) && _output->set_name (name));
283
284         return ret;
285 }
286
287 void
288 PortInsert::activate ()
289 {
290         IOProcessor::activate ();
291
292         _out->activate ();
293 }
294
295 void
296 PortInsert::deactivate ()
297 {
298         IOProcessor::deactivate ();
299
300         _out->deactivate ();
301 }