second (and hopefully) final part of changes to respond to header format changes...
[ArdourMidi.git] / libs / ardour / io_processor.cc
blobd1b7e9c232103d6c63e7adcb90438ea9efc66142
1 /*
2 Copyright (C) 2001 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.
20 #include <fstream>
21 #include <algorithm>
22 #include <string>
23 #include <cerrno>
24 #include <unistd.h>
25 #include <sstream>
28 #include "pbd/xml++.h"
29 #include "pbd/enumwriter.h"
31 #include "ardour/io_processor.h"
32 #include "ardour/session.h"
33 #include "ardour/utils.h"
34 #include "ardour/send.h"
35 #include "ardour/port_insert.h"
36 #include "ardour/plugin_insert.h"
37 #include "ardour/io.h"
38 #include "ardour/route.h"
40 #include "i18n.h"
42 using namespace std;
43 using namespace ARDOUR;
44 using namespace PBD;
46 /* create an IOProcessor that proxies to a new IO object */
48 IOProcessor::IOProcessor (Session& s, bool with_input, bool with_output,
49 const string& proc_name, const string io_name, DataType dtype)
50 : Processor(s, proc_name)
52 /* these are true in this constructor whether we actually create the associated
53 IO objects or not.
56 _own_input = true;
57 _own_output = true;
59 if (with_input) {
60 _input.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Input, dtype));
63 if (with_output) {
64 _output.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Output, dtype));
68 /* create an IOProcessor that proxies to an existing IO object */
70 IOProcessor::IOProcessor (Session& s, boost::shared_ptr<IO> in, boost::shared_ptr<IO> out,
71 const string& proc_name, DataType /*dtype*/)
72 : Processor(s, proc_name)
73 , _input (in)
74 , _output (out)
76 if (in) {
77 _own_input = false;
78 } else {
79 _own_input = true;
82 if (out) {
83 _own_output = false;
84 } else {
85 _own_output = true;
89 IOProcessor::~IOProcessor ()
93 void
94 IOProcessor::set_input (boost::shared_ptr<IO> io)
96 /* CALLER MUST HOLD PROCESS LOCK */
98 _input = io;
99 _own_input = false;
102 void
103 IOProcessor::set_output (boost::shared_ptr<IO> io)
105 /* CALLER MUST HOLD PROCESS LOCK */
107 _output = io;
108 _own_output = false;
111 XMLNode&
112 IOProcessor::state (bool full_state)
114 XMLNode& node (Processor::state (full_state));
116 if (_own_input) {
117 node.add_property ("own-input", "yes");
118 if (_input) {
119 XMLNode& i (_input->state (full_state));
120 // i.name() = X_("output");
121 node.add_child_nocopy (i);
123 } else {
124 node.add_property ("own-input", "no");
125 if (_input) {
126 node.add_property ("input", _input->name());
130 if (_own_output) {
131 node.add_property ("own-output", "yes");
132 if (_output) {
133 XMLNode& o (_output->state (full_state));
134 node.add_child_nocopy (o);
136 } else {
137 node.add_property ("own-output", "no");
138 if (_output) {
139 node.add_property ("output", _output->name());
143 return node;
147 IOProcessor::set_state (const XMLNode& node, int version)
149 const XMLProperty *prop;
150 const XMLNode *io_node = 0;
152 Processor::set_state(node, version);
154 if ((prop = node.property ("own-input")) != 0) {
155 _own_input = string_is_affirmative (prop->value());
158 if ((prop = node.property ("own-output")) != 0) {
159 _own_output = string_is_affirmative (prop->value());
162 /* don't attempt to set state for a proxied IO that we don't own */
164 XMLNodeList nlist = node.children();
165 XMLNodeIterator niter;
166 const string instr = enum_2_string (IO::Input);
167 const string outstr = enum_2_string (IO::Output);
169 if (_own_input) {
170 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
171 const XMLProperty* prop;
172 if ((prop = (*niter)->property ("name")) != 0) {
173 if (_name == prop->value()) {
174 if ((prop = (*niter)->property ("direction")) != 0) {
175 if (prop->value() == instr) {
176 io_node = (*niter);
177 break;
184 if (io_node) {
185 _input->set_state(*io_node, version);
187 // legacy sessions: use IO name
188 if ((prop = node.property ("name")) == 0) {
189 set_name (_input->name());
192 } else {
193 /* no input, which is OK */
198 if (_own_output) {
199 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
200 if ((*niter)->name() == "IO") {
201 const XMLProperty* prop;
202 if ((prop = (*niter)->property ("name")) != 0) {
203 if (_name == prop->value()) {
204 if ((prop = (*niter)->property ("direction")) != 0) {
205 if (prop->value() == outstr) {
206 io_node = (*niter);
207 break;
215 if (io_node) {
216 _output->set_state(*io_node, version);
218 // legacy sessions: use IO name
219 if ((prop = node.property ("name")) == 0) {
220 set_name (_output->name());
222 } else {
223 /* no output, which is OK */
227 return 0;
230 void
231 IOProcessor::silence (nframes_t nframes)
233 if (_own_output && _output) {
234 _output->silence (nframes);
238 ChanCount
239 IOProcessor::natural_output_streams() const
241 return _output ? _output->n_ports() : ChanCount::ZERO;
244 ChanCount
245 IOProcessor::natural_input_streams () const
247 return _input ? _input->n_ports() : ChanCount::ZERO;
250 bool
251 IOProcessor::set_name (const std::string& name)
253 bool ret = SessionObject::set_name (name);
255 if (ret && _own_input && _input) {
256 ret = _input->set_name (name);
259 if (ret && _own_output && _output) {
260 ret = _output->set_name (name);
263 return ret;
266 bool
267 IOProcessor::feeds (boost::shared_ptr<Route> other) const
269 return _output && _output->connected_to (other->input());
272 void
273 IOProcessor::disconnect ()
275 if (_input) {
276 _input->disconnect (this);
279 if (_output) {
280 _output->disconnect (this);