update wavesaudio backend, now supports Windows (ASIO) as well as OS X (CoreAudio)
[ardour.git] / libs / backends / wavesaudio / waves_dataport.cc
1 /*
2     Copyright (C) 2013 Valeriy Kamyshniy
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 "waves_dataport.h"
21
22 using namespace ARDOUR;
23
24 WavesDataPort::WavesDataPort (const std::string& inport_name, PortFlags inflags)
25     : _name (inport_name)
26     , _flags (inflags)
27 {
28     _capture_latency_range.min = 
29     _capture_latency_range.max = 
30     _playback_latency_range.min = 
31     _playback_latency_range.max = 0;
32 }
33
34
35 WavesDataPort::~WavesDataPort ()
36 {
37     disconnect_all ();
38 }
39
40
41 int WavesDataPort::connect (WavesDataPort *port)
42 {
43     if (!port) {
44         std::cerr << "WavesDataPort::connect (): invalid (null) port to connect to!" << std::endl;
45         return -1;
46     }
47
48     if (type () != port->type ())    {
49         std::cerr << "WavesDataPort::connect (): wrong type of the port to connect to!" << std::endl;
50         return -1;
51     }
52
53     if (is_output () && port->is_output ()) {
54         std::cerr << "WavesDataPort::connect (): attempt to connect output port to output port!" << std::endl;
55         return -1;
56     }
57
58     if (is_input () && port->is_input ()) {
59         std::cerr << "WavesDataPort::connect (): attempt to connect input port to input port!" << std::endl;
60         return -1;
61     }
62
63     if (this == port) {
64         std::cerr << "WavesDataPort::connect (): attempt to connect port to itself!" << std::endl;
65         return -1; 
66     }
67
68     if (is_connected (port)) {
69         std::cerr << "WavesDataPort::connect (): the ports are already connected!" << std::endl;
70         return -1;
71     }
72
73     _connect (port, true);
74     return 0;
75 }
76
77
78 void WavesDataPort::_connect (WavesDataPort *port, bool api_call)
79 {
80     _connections.push_back (port);
81     if (api_call) {
82         port->_connect (this, false);
83     }
84 }
85
86
87 int WavesDataPort::disconnect (WavesDataPort *port)
88 {
89     if (port == NULL) {
90         std::cerr << "WavesDataPort::disconnect (): invalid (null) port to disconnect from!" << std::endl;
91         return -1;
92     }
93
94     if (!is_connected (port)) {
95         std::cerr << "WavesDataPort::disconnect (): the ports are not connected!" << std::endl;
96         return -1;
97     }
98
99     _disconnect (port, true);
100
101     return 0;
102 }
103
104
105 void WavesDataPort::_disconnect (WavesDataPort *port, bool api_call)
106 {
107     std::vector<WavesDataPort*>::iterator it = std::find (_connections.begin (), _connections.end (), port);
108     
109     if (it != _connections.end ()) { // actually, it's supposed to be always true.
110         _connections.erase (it);
111     }
112
113     if (api_call) {
114         port->_disconnect (this, false);
115     }
116
117         if (is_input() && _connections.empty())
118         {
119                 _wipe_buffer();
120         }
121 }
122
123
124 void WavesDataPort::disconnect_all ()
125 {
126     while (!_connections.empty ()) {
127         _connections.back ()->_disconnect (this, false);
128         _connections.pop_back ();
129     }
130 }
131
132
133 bool WavesDataPort::is_physically_connected () const
134 {
135     for (std::vector<WavesDataPort*>::const_iterator it = _connections.begin (); it != _connections.end (); ++it) {
136         if ((*it)->is_physical ()) {
137             return true;
138         }
139     }
140
141     return false;
142 }