remove offset from process callback tree. some breakage may have occured. yes, really.
[ardour.git] / libs / ardour / port.cc
1 /*
2     Copyright (C) 2009 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 "ardour/port.h"
21 #include "ardour/audioengine.h"
22 #include "pbd/failed_constructor.h"
23 #include "pbd/error.h"
24 #include "pbd/compose.h"
25 #include <stdexcept>
26
27 #include "i18n.h"
28
29 using namespace std;
30 using namespace ARDOUR;
31
32 AudioEngine* Port::_engine = 0;
33 nframes_t Port::_port_offset = 0;
34 nframes_t Port::_buffer_size = 0;
35
36 /** @param n Port short name */
37 Port::Port (std::string const & n, DataType t, Flags f)
38         : _last_monitor (false)
39         , _name (n)
40         , _flags (f)
41 {
42
43         /* Unfortunately we have to pass the DataType into this constructor so that we can
44            create the right kind of JACK port; aside from this we'll use the virtual function type ()
45            to establish type. 
46         */
47
48         assert (_name.find_first_of (':') == std::string::npos);
49         
50         if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
51                 throw failed_constructor ();
52         }
53 }
54
55 /** Port destructor */
56 Port::~Port ()
57 {
58         jack_port_unregister (_engine->jack (), _jack_port);
59 }
60
61 /** @return true if this port is connected to anything */
62 bool
63 Port::connected () const
64 {
65         return (jack_port_connected (_jack_port) != 0);
66 }
67
68 int
69 Port::disconnect_all ()
70 {
71         jack_port_disconnect (_engine->jack(), _jack_port);
72         _connections.clear ();
73
74         return 0;
75 }
76
77 /** @param o Port name
78  * @return true if this port is connected to o, otherwise false.
79  */
80 bool
81 Port::connected_to (std::string const & o) const
82 {
83         return jack_port_connected_to (_jack_port, _engine->make_port_name_non_relative(o).c_str ());
84 }
85
86 /** @param o Filled in with port full names of ports that we are connected to */
87 int
88 Port::get_connections (std::vector<std::string> & c) const
89 {
90         int n = 0;
91
92         const char** jc = jack_port_get_connections (_jack_port);
93         if (jc) {
94                 for (int i = 0; jc[i]; ++i) {
95                         c.push_back (jc[i]);
96                         ++n;
97                 }
98         }
99         
100         return n;
101 }
102
103 int
104 Port::connect (std::string const & other)
105 {
106         /* caller must hold process lock */
107
108         std::string const other_shrt = _engine->make_port_name_non_relative (other);
109         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
110
111         int r = 0;
112                 
113         if (sends_output ()) {
114                 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
115         } else {
116                 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
117         }
118         
119         if (r == 0) {
120                 _connections.insert (other);
121         }
122
123         return r;
124 }
125
126 int
127 Port::disconnect (std::string const & other)
128 {
129         /* caller must hold process lock */
130
131         std::string const other_shrt = _engine->make_port_name_non_relative (other);
132         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
133
134         int r = 0;
135         
136         if (sends_output ()) {
137                 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
138         } else {
139                 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
140         }
141         
142         if (r == 0) {
143                 _connections.erase (other);
144         }
145         
146 return r;
147 }
148
149
150 bool
151 Port::connected_to (Port* o) const
152 {
153         return connected_to (o->name ());
154 }
155
156 int
157 Port::connect (Port* o)
158 {
159         return connect (o->name ());
160 }
161
162 int
163 Port::disconnect (Port* o)
164 {
165         return disconnect (o->name ());
166 }
167
168 void
169 Port::set_engine (AudioEngine* e)
170 {
171         _engine = e;
172 }
173
174 void
175 Port::ensure_monitor_input (bool yn)
176 {
177         jack_port_ensure_monitor (_jack_port, yn);
178 }
179
180 bool
181 Port::monitoring_input () const
182 {
183         return jack_port_monitoring_input (_jack_port);
184 }
185
186 void
187 Port::reset ()
188 {
189         _last_monitor = false;
190
191         // XXX
192         // _metering = 0;
193         // reset_meters ();
194 }
195
196 void
197 Port::recompute_total_latency () const
198 {
199 #ifdef HAVE_JACK_RECOMPUTE_LATENCY      
200         jack_recompute_total_latency (_engine->jack (), _jack_port);
201 #endif  
202 }
203
204 nframes_t
205 Port::total_latency () const
206 {
207         return jack_port_get_total_latency (_engine->jack (), _jack_port);
208 }
209
210 int
211 Port::reestablish ()
212 {
213         _jack_port = jack_port_register (_engine->jack(), _name.c_str(), type().to_jack_type(), _flags, 0);
214
215         if (_jack_port == 0) {
216                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
217                 return -1;
218         }
219
220         reset ();
221
222         return 0;
223 }
224
225
226 int
227 Port::reconnect ()
228 {
229         /* caller must hold process lock; intended to be used only after reestablish() */
230
231         for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
232                 if (connect (*i)) {
233                         return -1;
234                 }
235         }
236
237         return 0;
238 }
239
240 /** @param n Short name */
241 int
242 Port::set_name (std::string const & n)
243 {
244         assert (_name.find_first_of (':') == std::string::npos);
245
246         int const r = jack_port_set_name (_jack_port, n.c_str());
247         if (r == 0) {
248                 _name = n;
249         }
250
251         return r;
252 }
253
254 void
255 Port::request_monitor_input (bool yn)
256 {
257         jack_port_request_monitor (_jack_port, yn);
258 }
259
260 void
261 Port::set_latency (nframes_t n)
262 {
263         jack_port_set_latency (_jack_port, n);
264 }
265