first part of MIDI cut/copy/paste ; fix for input/output_streams of an IOProcessor...
[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 <fstream>
21 #include <algorithm>
22 #include <string>
23 #include <cerrno>
24 #include <unistd.h>
25 #include <sstream>
26
27 #include <sigc++/bind.h>
28
29 #include "pbd/xml++.h"
30 #include "pbd/enumwriter.h"
31
32 #include "ardour/io_processor.h"
33 #include "ardour/session.h"
34 #include "ardour/utils.h"
35 #include "ardour/send.h"
36 #include "ardour/port_insert.h"
37 #include "ardour/plugin_insert.h"
38 #include "ardour/io.h"
39 #include "ardour/route.h"
40
41 #include "i18n.h"
42
43 using namespace std;
44 using namespace ARDOUR;
45 using namespace PBD;
46
47 /* create an IOProcessor that proxies to a new IO object */
48
49 IOProcessor::IOProcessor (Session& s, bool with_input, bool with_output,
50                           const string& proc_name, const string io_name, DataType dtype)
51         : Processor(s, proc_name)
52 {
53         /* these are true in this constructor whether we actually create the associated
54            IO objects or not.
55         */
56
57         _own_input = true;
58         _own_output = true;
59
60         if (with_input) {
61                 _input.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Input, dtype));
62         }
63
64         if (with_output) {
65                 _output.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Output, dtype));
66         }
67 }
68
69 /* create an IOProcessor that proxies to an existing IO object */
70
71 IOProcessor::IOProcessor (Session& s, boost::shared_ptr<IO> in, boost::shared_ptr<IO> out, 
72                           const string& proc_name, DataType /*dtype*/)
73         : Processor(s, proc_name)
74         , _input (in)
75         , _output (out)
76 {
77         if (in) {
78                 _own_input = false;
79         } else {
80                 _own_input = true;
81         }
82
83         if (out) {
84                 _own_output = false;
85         } else {
86                 _own_output = true;
87         }
88 }
89
90 IOProcessor::~IOProcessor ()
91 {
92         notify_callbacks ();
93 }
94
95 void
96 IOProcessor::set_input (boost::shared_ptr<IO> io)
97 {
98         /* CALLER MUST HOLD PROCESS LOCK */
99
100         _input = io;
101         _own_input = false;
102 }
103
104 void
105 IOProcessor::set_output (boost::shared_ptr<IO> io)
106 {
107         /* CALLER MUST HOLD PROCESS LOCK */
108
109         _output = io;
110         _own_output = false;
111 }
112
113 XMLNode&
114 IOProcessor::state (bool full_state)
115 {
116         XMLNode& node (Processor::state (full_state));
117         
118         if (_own_input) {
119                 node.add_property ("own-input", "yes");
120                 if (_input) {
121                         XMLNode& i (_input->state (full_state));
122                         // i.name() = X_("output");
123                         node.add_child_nocopy (i);
124                 }
125         } else {
126                 node.add_property ("own-input", "no");
127                 if (_input) {
128                         node.add_property ("input", _input->name());
129                 }
130         }
131         
132         if (_own_output) {
133                 node.add_property ("own-output", "yes");
134                 if (_output) {
135                         XMLNode& o (_output->state (full_state));
136                         // o.name() = X_("output");
137                         node.add_child_nocopy (o);
138                 }
139         } else {
140                 node.add_property ("own-output", "no");
141                 if (_output) {
142                         node.add_property ("output", _output->name());
143                 }
144         }
145
146         return node;
147 }
148
149 int
150 IOProcessor::set_state (const XMLNode& node)
151 {
152         const XMLProperty *prop;
153         const XMLNode *io_node = 0;
154
155         Processor::set_state(node);
156
157         if ((prop = node.property ("own-input")) != 0) {
158                 _own_input = (prop->value() == "yes");
159         }
160
161         if ((prop = node.property ("own-output")) != 0) {
162                 _own_output = (prop->value() == "yes");
163         }
164
165         /* don't attempt to set state for a proxied IO that we don't own */
166
167         XMLNodeList nlist = node.children();
168         XMLNodeIterator niter;
169         
170         if (_own_input) {
171                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
172                         if ((*niter)->name() == "input") {
173                                 io_node = (*niter);
174                                 break;
175                         }
176                 }
177                 
178                 if (io_node) {
179                         _input->set_state(*io_node);
180                         
181                         // legacy sessions: use IO name
182                         if ((prop = node.property ("name")) == 0) {
183                                 set_name (_input->name());
184                         }
185                         
186                 } else {
187                         /* no input */
188                 }
189
190         }
191         
192         if (_own_output) {
193                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
194                         if ((*niter)->name() == "output") {
195                                 io_node = (*niter);
196                                 break;
197                         }
198                 }
199                 
200                 if (io_node) {
201                         _output->set_state(*io_node);
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 */
209                 }
210         }
211
212         return 0;
213 }
214
215 void
216 IOProcessor::silence (nframes_t nframes)
217 {
218         if (_own_output && _output) {
219                 _output->silence (nframes);
220         }
221 }
222
223 ChanCount
224 IOProcessor::natural_output_streams() const
225 {
226         return _output ? _output->n_ports() : ChanCount::ZERO;
227 }
228
229 ChanCount
230 IOProcessor::natural_input_streams () const
231 {
232         return _input ? _input->n_ports() : ChanCount::ZERO;
233 }
234
235 bool
236 IOProcessor::set_name (const std::string& name)
237 {
238         bool ret = SessionObject::set_name (name);
239
240         if (ret && _own_input && _input) {
241                 ret = _input->set_name (name);
242         }
243
244         if (ret && _own_output && _output) {
245                 ret = _output->set_name (name);
246         }
247
248         return ret;
249 }
250
251 bool
252 IOProcessor::feeds (boost::shared_ptr<Route> other) const
253 {
254         return _output && _output->connected_to (other->input());
255 }
256
257 void
258 IOProcessor::disconnect ()
259 {
260         if (_input) {
261                 _input->disconnect (this);
262         }
263
264         if (_output) {
265                 _output->disconnect (this);
266         }
267 }