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