more MTC debugging
[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/delivery.h"
27 #include "ardour/port_insert.h"
28 #include "ardour/plugin.h"
29 #include "ardour/port.h"
30 #include "ardour/route.h"
31 #include "ardour/buffer_set.h"
32
33 #include "ardour/audioengine.h"
34 #include "ardour/session.h"
35 #include "ardour/types.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace ARDOUR;
41 using namespace PBD;
42
43 PortInsert::PortInsert (Session& s, boost::shared_ptr<MuteMaster> mm)
44         : IOProcessor (s, true, true, string_compose (_("insert %1"), (bitslot = s.next_insert_id()) + 1), "")
45         , _out (new Delivery (s, _output, mm, _name, Delivery::Insert))
46 {
47         ProcessorCreated (this); /* EMIT SIGNAL */
48 }
49
50 PortInsert::PortInsert (Session& s, boost::shared_ptr<MuteMaster> mm, const XMLNode& node)
51         : IOProcessor (s, true, true, "unnamed port insert")
52         , _out (new Delivery (s, _output, mm, _name, Delivery::Insert))
53
54 {
55         if (set_state (node, Stateful::loading_state_version)) {
56                 throw failed_constructor();
57         }
58
59         ProcessorCreated (this); /* EMIT SIGNAL */
60 }
61
62 PortInsert::~PortInsert ()
63 {
64 }
65
66 void
67 PortInsert::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes, bool)
68 {
69         if (_output->n_ports().n_total() == 0) {
70                 return;
71         }
72
73         if (!_active && !_pending_active) {
74                 /* deliver silence */
75                 silence (nframes);
76                 goto out;
77         }
78
79         _out->run (bufs, start_frame, end_frame, nframes, true);
80         _input->collect_input (bufs, nframes, ChanCount::ZERO);
81
82   out:
83         _active = _pending_active;
84 }
85
86 XMLNode&
87 PortInsert::get_state(void)
88 {
89         return state (true);
90 }
91
92 XMLNode&
93 PortInsert::state (bool full)
94 {
95         XMLNode& node = Processor::state(full);
96         char buf[32];
97         node.add_property ("type", "port");
98         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
99         node.add_property ("bitslot", buf);
100
101         return node;
102 }
103
104 int
105 PortInsert::set_state (const XMLNode& node, int version)
106 {
107         XMLNodeList nlist = node.children();
108         XMLNodeIterator niter;
109         XMLPropertyList plist;
110         const XMLProperty *prop;
111
112         if ((prop = node.property ("type")) == 0) {
113                 error << _("XML node describing port insert is missing the `type' field") << endmsg;
114                 return -1;
115         }
116
117         if (prop->value() != "port") {
118                 error << _("non-port insert XML used for port plugin insert") << endmsg;
119                 return -1;
120         }
121
122         if ((prop = node.property ("bitslot")) == 0) {
123                 bitslot = _session.next_insert_id();
124         } else {
125                 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
126                 _session.mark_insert_id (bitslot);
127         }
128
129         const XMLNode* insert_node = &node;
130
131         // legacy sessions: search for child IOProcessor node
132         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
133                 if ((*niter)->name() == "IOProcessor") {
134                         insert_node = *niter;
135                         break;
136                 }
137         }
138
139         Processor::set_state (*insert_node, version);
140
141         return 0;
142 }
143
144 ARDOUR::nframes_t
145 PortInsert::signal_latency() const
146 {
147         /* because we deliver and collect within the same cycle,
148            all I/O is necessarily delayed by at least frames_per_cycle().
149
150            if the return port for insert has its own latency, we
151            need to take that into account too.
152         */
153
154         return _session.engine().frames_per_cycle() + _input->signal_latency();
155 }
156
157 bool
158 PortInsert::configure_io (ChanCount in, ChanCount out)
159 {
160         /* for an insert, processor input corresponds to IO output, and vice versa */
161
162         if (_input->ensure_io (in, false, this) != 0) {
163                 return false;
164         }
165
166         if (_output->ensure_io (out, false, this) != 0) {
167                 return false;
168         }
169
170         return Processor::configure_io (in, out);
171 }
172
173 bool
174 PortInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
175 {
176         out = in;
177         return true;
178 }
179
180 bool
181 PortInsert::set_name (const std::string& name)
182 {
183         bool ret = Processor::set_name (name);
184
185         ret = (ret && _input->set_name (name) && _output->set_name (name));
186
187         return ret;
188 }
189
190 void
191 PortInsert::activate ()
192 {
193         IOProcessor::activate ();
194
195         _out->activate ();
196 }
197
198 void
199 PortInsert::deactivate ()
200 {
201         IOProcessor::deactivate ();
202
203         _out->deactivate ();
204 }