merged with 1697 revision of trunk (which is post-rc1 but pre-rc2
[ardour.git] / libs / ardour / ardour / port.h
1 /*
2     Copyright (C) 2002 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 #ifndef __ardour_port_h__
21 #define __ardour_port_h__
22
23 #include <sigc++/signal.h>
24 #include <pbd/failed_constructor.h>
25 #include <ardour/ardour.h>
26 #include <jack/jack.h>
27
28 namespace ARDOUR {
29
30 class AudioEngine;
31
32 class Port : public sigc::trackable {
33    public:
34         virtual ~Port() { 
35                 free (_port);
36         }
37
38         Sample *get_buffer (nframes_t nframes) {
39                 if (_flags & JackPortIsOutput) {
40                         return _buffer;
41                 } else {
42                         return (Sample *) jack_port_get_buffer (_port, nframes);
43                 }
44         }
45
46         void reset_buffer () {
47                 if (_flags & JackPortIsOutput) {
48                         _buffer = (Sample *) jack_port_get_buffer (_port, 0);
49                 } else {
50                         _buffer = 0; /* catch illegal attempts to use it */
51                 }
52                 _silent = false;
53         }
54
55         std::string name() { 
56                 return _name;
57         }
58
59         std::string short_name() { 
60                 return jack_port_short_name (_port);
61         }
62         
63         int set_name (std::string str);
64
65         JackPortFlags flags() const {
66                 return _flags;
67         }
68
69         bool is_mine (jack_client_t *client) { 
70                 return jack_port_is_mine (client, _port);
71         }
72
73         const char* type() const {
74                 return _type.c_str();
75         }
76
77         int connected () const {
78                 return jack_port_connected (_port);
79         }
80         
81         bool connected_to (const std::string& portname) const {
82                 return jack_port_connected_to (_port, portname.c_str());
83         }
84
85         const char ** get_connections () const {
86                 return jack_port_get_connections (_port);
87         }
88
89         void reset_overs () {
90                 _short_overs = 0;
91                 _long_overs = 0;
92                 _overlen = 0;
93         }
94
95         void reset_peak_meter () {
96                 _peak = 0;
97         }
98         
99         void reset_meters () {
100                 reset_peak_meter ();
101                 reset_overs ();
102         }
103
104         void enable_metering() {
105                 _metering++;
106         }
107         
108         void disable_metering () {
109                 if (_metering) { _metering--; }
110         }
111
112         float                       peak_db() const { return _peak_db; }
113         jack_default_audio_sample_t peak()    const { return _peak; }
114
115         uint32_t short_overs () const { return _short_overs; }
116         uint32_t long_overs ()  const { return _long_overs; }
117         
118         static void set_short_over_length (nframes_t);
119         static void set_long_over_length (nframes_t);
120
121         bool receives_input() const {
122                 return _flags & JackPortIsInput;
123         }
124
125         bool sends_output () const {
126                 return _flags & JackPortIsOutput;
127         }
128         
129         bool monitoring_input () const {
130                 return jack_port_monitoring_input (_port);
131         }
132
133         bool can_monitor () const {
134                 return _flags & JackPortCanMonitor;
135         }
136         
137         void ensure_monitor_input (bool yn) {
138
139 #ifdef HAVE_JACK_PORT_ENSURE_MONITOR
140                 jack_port_ensure_monitor (_port, yn);
141 #else
142                 jack_port_request_monitor(_port, yn);
143 #endif
144
145         }
146
147         /*XXX completely bloody useless imho*/
148         void request_monitor_input (bool yn) {
149                 jack_port_request_monitor (_port, yn);
150         }
151
152         nframes_t latency () const {
153                 return jack_port_get_latency (_port);
154         }
155
156         void set_latency (nframes_t nframes) {
157                 jack_port_set_latency (_port, nframes);
158         }
159
160         sigc::signal<void,bool> MonitorInputChanged;
161         sigc::signal<void,bool> ClockSyncChanged;
162
163         bool is_silent() const { return _silent; }
164
165         /** Assumes that the port is an audio output port */
166         void silence (nframes_t nframes, nframes_t offset) {
167                 if (!_silent) {
168                         memset (_buffer + offset, 0, sizeof (Sample) * nframes);
169                         if (offset == 0) {
170                                 /* XXX this isn't really true, but i am not sure
171                                    how to set this correctly. we really just
172                                    want to set it true when the entire port
173                                    buffer has been overrwritten.
174                                 */
175                                 _silent = true;
176                         }
177                 }
178         }
179         
180         void mark_silence (bool yn) {
181                 _silent = yn;
182         }
183
184   private:
185         friend class AudioEngine;
186
187         Port (jack_port_t *port);
188         void reset ();
189         
190         /* engine isn't supposed to below here */
191
192         Sample *_buffer;
193
194         /* cache these 3 from JACK so that we can
195            access them for reconnecting.
196         */
197
198         JackPortFlags _flags;
199         std::string   _type;
200         std::string   _name;
201
202         bool                         _last_monitor : 1;
203         bool                         _silent : 1;
204         jack_port_t                 *_port;
205         nframes_t               _overlen;
206         jack_default_audio_sample_t  _peak;
207         float                        _peak_db;
208         uint32_t                     _short_overs;
209         uint32_t                     _long_overs;
210         unsigned short               _metering;
211         
212         static nframes_t        _long_over_length;
213         static nframes_t        _short_over_length;
214 };
215  
216 } // namespace ARDOUR
217
218 #endif /* __ardour_port_h__ */