fix all manner of wrongness with port buffer offsets
[ardour.git] / libs / ardour / ardour / port.h
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 #ifndef __ardour_port_h__
21 #define __ardour_port_h__
22
23 #include <set>
24 #include <string>
25 #include <vector>
26 #include <jack/jack.h>
27 #include <boost/utility.hpp>
28 #include "pbd/signals.h"
29
30 #include "ardour/data_type.h"
31 #include "ardour/types.h"
32
33 namespace ARDOUR {
34
35 class AudioEngine;
36 class Buffer;
37
38 class Port : public boost::noncopyable
39 {
40 public:
41         enum Flags {
42                 IsInput = JackPortIsInput,
43                 IsOutput = JackPortIsOutput,
44         };
45
46         virtual ~Port ();
47
48         static void set_buffer_size (pframes_t sz) {
49                 _buffer_size = sz;
50         }
51         static void set_connecting_blocked( bool yn ) { 
52                 _connecting_blocked = yn;
53         }
54         static bool connecting_blocked() { 
55                 return _connecting_blocked;
56         }
57
58         /** @return Port short name */
59         std::string name () const {
60                 return _name;
61         }
62
63         int set_name (std::string const &);
64
65         /** @return flags */
66         Flags flags () const {
67                 return _flags;
68         }
69
70         /** @return true if this Port receives input, otherwise false */
71         bool receives_input () const {
72                 return _flags & IsInput;
73         }
74
75         /** @return true if this Port sends output, otherwise false */
76         bool sends_output () const {
77                 return _flags & IsOutput;
78         }
79
80         bool connected () const;
81         int disconnect_all ();
82         int get_connections (std::vector<std::string> &) const;
83
84         /* connection by name */
85         bool connected_to (std::string const &) const;
86         int connect (std::string const &);
87         int disconnect (std::string const &);
88
89         /* connection by Port* */
90         bool connected_to (Port *) const;
91         virtual int connect (Port *);
92         int disconnect (Port *);
93
94         void ensure_monitor_input (bool);
95         bool monitoring_input () const;
96         framecnt_t total_latency () const;
97         int reestablish ();
98         int reconnect ();
99         void request_monitor_input (bool);
100         void set_latency (framecnt_t);
101         
102 #ifdef HAVE_JACK_NEW_LATENCY
103         void get_connected_latency_range (jack_latency_range_t& range, bool playback) const;
104         void set_latency_range (jack_latency_range_t& range, bool playback) const;
105 #endif
106
107         virtual void reset ();
108
109         /** @return the size of the raw buffer (bytes) for duration @a nframes (audio frames) */
110         virtual size_t raw_buffer_size (pframes_t nframes) const = 0;
111
112         virtual DataType type () const = 0;
113         virtual void cycle_start (pframes_t) = 0;
114         virtual void cycle_end (pframes_t) = 0;
115         virtual void cycle_split () = 0;
116         virtual Buffer& get_buffer (framecnt_t nframes) = 0;
117         virtual void flush_buffers (pframes_t nframes, framepos_t /*time*/) {}
118         virtual void transport_stopped () {}
119
120         bool physically_connected () const;
121
122         static void set_engine (AudioEngine *);
123
124         PBD::Signal1<void,bool> MonitorInputChanged;
125
126
127         static framecnt_t port_offset() { return _port_offset; }
128
129         static void set_port_offset (framecnt_t off) {
130                 _port_offset = off;
131         }
132         
133         static void increment_port_offset (framecnt_t n) {
134                 _port_offset += n;
135         }
136
137 protected:
138
139         Port (std::string const &, DataType, Flags);
140
141         jack_port_t* _jack_port; ///< JACK port
142
143         static pframes_t  _buffer_size;
144         static bool       _connecting_blocked;
145         static framecnt_t _port_offset;
146         
147         static AudioEngine* _engine; ///< the AudioEngine
148
149 private:
150         friend class AudioEngine;
151
152         void recompute_total_latency () const;
153
154         /* XXX */
155         bool _last_monitor;
156
157         std::string _name;  ///< port short name
158         Flags       _flags; ///< flags
159
160         /** ports that we are connected to, kept so that we can
161             reconnect to JACK when required */
162         std::set<std::string> _connections;
163
164 };
165
166 }
167
168 #endif /* __ardour_port_h__ */