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