Only show user-presets in favorite sidebar
[ardour.git] / libs / ardour / io_processor.cc
1 /*
2     Copyright (C) 2001 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 <list>
21 #include <string>
22
23 #include "pbd/xml++.h"
24 #include "pbd/enumwriter.h"
25
26 #include "ardour/chan_count.h"
27 #include "ardour/data_type.h"
28 #include "ardour/io.h"
29 #include "ardour/io_processor.h"
30 #include "ardour/processor.h"
31 #include "ardour/route.h"
32 #include "ardour/session_object.h"
33 #include "ardour/types.h"
34
35 #include "pbd/i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 namespace ARDOUR { class Session; }
42
43 /* create an IOProcessor that proxies to a new IO object */
44
45 IOProcessor::IOProcessor (Session& s, bool with_input, bool with_output,
46                           const string& proc_name, const string io_name, DataType dtype, bool sendish)
47         : Processor(s, proc_name)
48 {
49         /* these are true in this constructor whether we actually create the associated
50            IO objects or not.
51         */
52
53         _own_input = true;
54         _own_output = true;
55
56         if (with_input) {
57                 _input.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Input, dtype, sendish));
58         }
59
60         if (with_output) {
61                 _output.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Output, dtype, sendish));
62         }
63 }
64
65 /* create an IOProcessor that proxies to an existing IO object */
66
67 IOProcessor::IOProcessor (Session& s, boost::shared_ptr<IO> in, boost::shared_ptr<IO> out,
68                           const string& proc_name, DataType /*dtype*/)
69         : Processor(s, proc_name)
70         , _input (in)
71         , _output (out)
72 {
73         if (in) {
74                 _own_input = false;
75         } else {
76                 _own_input = true;
77         }
78
79         if (out) {
80                 _own_output = false;
81         } else {
82                 _own_output = true;
83         }
84 }
85
86 IOProcessor::~IOProcessor ()
87 {
88 }
89
90 void
91 IOProcessor::set_input (boost::shared_ptr<IO> io)
92 {
93         /* CALLER MUST HOLD PROCESS LOCK */
94
95         _input = io;
96         _own_input = false;
97 }
98
99 void
100 IOProcessor::set_output (boost::shared_ptr<IO> io)
101 {
102         /* CALLER MUST HOLD PROCESS LOCK */
103
104         _output = io;
105         _own_output = false;
106 }
107
108 XMLNode&
109 IOProcessor::state ()
110 {
111         XMLNode& node (Processor::state ());
112
113         node.set_property ("own-input", _own_input);
114
115         if (_input) {
116                 if (_own_input) {
117                         XMLNode& i (_input->get_state ());
118                         // i.name() = X_("output");
119                         node.add_child_nocopy (i);
120                 } else {
121                         node.set_property ("input", _input->name ());
122                 }
123         }
124
125         node.set_property ("own-output", _own_output);
126
127         if (_output) {
128                 if (_own_output) {
129                         XMLNode& o (_output->get_state ());
130                         node.add_child_nocopy (o);
131                 } else {
132                         node.set_property ("output", _output->name ());
133                 }
134         }
135
136         return node;
137 }
138
139 int
140 IOProcessor::set_state (const XMLNode& node, int version)
141 {
142         if (version < 3000) {
143                 return set_state_2X (node, version);
144         }
145
146         XMLProperty const * prop;
147         const XMLNode *io_node = 0;
148
149         Processor::set_state(node, version);
150
151         bool ignore_name = node.property ("ignore-name");
152
153         node.get_property ("own-input", _own_input);
154         node.get_property ("own-output", _own_output);
155
156         /* don't attempt to set state for a proxied IO that we don't own */
157
158         XMLNodeList nlist = node.children();
159         XMLNodeIterator niter;
160         const string instr = enum_2_string (IO::Input);
161         const string outstr = enum_2_string (IO::Output);
162
163         std::string str;
164         if (_own_input && _input) {
165                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
166                         if ((*niter)->get_property ("name", str) && (_name == str || ignore_name)) {
167                                 if ((*niter)->get_property ("direction", str) && str == instr) {
168                                         io_node = (*niter);
169                                         break;
170                                 }
171                         }
172                 }
173
174                 if (io_node) {
175                         _input->set_state(*io_node, version);
176
177                         // legacy sessions: use IO name
178                         if ((prop = node.property ("name")) == 0) {
179                                 set_name (_input->name());
180                         }
181
182                 } else {
183                         /* no input, which is OK */
184                 }
185
186         }
187
188         if (_own_output && _output) {
189                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
190                         if ((*niter)->name() == "IO") {
191                                 if ((*niter)->get_property ("name", str) && (_name == str || ignore_name)) {
192                                         if ((*niter)->get_property ("direction", str) && str == outstr) {
193                                                 io_node = (*niter);
194                                                 break;
195                                         }
196                                 }
197                         }
198                 }
199
200                 if (io_node) {
201                         _output->set_state(*io_node, version);
202
203                         // legacy sessions: use IO name
204                         if ((prop = node.property ("name")) == 0) {
205                                 set_name (_output->name());
206                         }
207                 } else {
208                         /* no output, which is OK */
209                 }
210         }
211
212         return 0;
213 }
214
215 int
216 IOProcessor::set_state_2X (const XMLNode& node, int version)
217 {
218         _own_input = _own_output = true;
219
220         Processor::set_state_2X (node, version);
221
222         return 0;
223 }
224
225 void
226 IOProcessor::silence (samplecnt_t nframes, samplepos_t /* start_sample */)
227 {
228         if (_own_output && _output) {
229                 _output->silence (nframes);
230         }
231 }
232
233 ChanCount
234 IOProcessor::natural_output_streams() const
235 {
236         return _output ? _output->n_ports() : ChanCount::ZERO;
237 }
238
239 ChanCount
240 IOProcessor::natural_input_streams () const
241 {
242         return _input ? _input->n_ports() : ChanCount::ZERO;
243 }
244
245 bool
246 IOProcessor::set_name (const std::string& name)
247 {
248         bool ret = SessionObject::set_name (name);
249
250         if (ret && _own_input && _input) {
251                 ret = _input->set_name (name);
252         }
253
254         if (ret && _own_output && _output) {
255                 ret = _output->set_name (name);
256         }
257
258         return ret;
259 }
260
261 bool
262 IOProcessor::feeds (boost::shared_ptr<Route> other) const
263 {
264         return _output && _output->connected_to (other->input());
265 }
266
267 void
268 IOProcessor::disconnect ()
269 {
270         if (_input) {
271                 _input->disconnect (this);
272         }
273
274         if (_output) {
275                 _output->disconnect (this);
276         }
277 }
278
279 /** Set up the XML description of a send so that we will not
280  *  reset its name or bitslot during ::set_state()
281  *  @param state XML send state.
282  *  @param session Session.
283  */
284 void
285 IOProcessor::prepare_for_reset (XMLNode &state, const std::string& name)
286 {
287         state.set_property ("ignore-bitslot", true);
288         state.set_property ("ignore-name", true);
289
290         XMLNodeList nlist = state.children();
291         XMLNodeIterator niter;
292
293         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
294                 if ((*niter)->name() == IO::state_node_name.c_str()) {
295                         IO::prepare_for_reset (**niter, name);
296                 }
297         }
298 }