got MIDI Clock slave closer to functioning properly:
[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 <set>
24 #include <vector>
25 #include <string>
26 #include <cstring>
27 #include <sigc++/signal.h>
28 #include <pbd/failed_constructor.h>
29 #include <ardour/ardour.h>
30 #include <ardour/data_type.h>
31 #include <jack/jack.h>
32
33 namespace ARDOUR {
34
35 class AudioEngine;
36 class Buffer;
37
38 /** Abstract base for ports
39  */
40 class Port : public virtual sigc::trackable {
41    public:
42         enum Flags {
43                 IsInput = JackPortIsInput,
44                 IsOutput = JackPortIsOutput,
45                 IsPhysical = JackPortIsPhysical,
46                 IsTerminal = JackPortIsTerminal,
47                 CanMonitor = JackPortCanMonitor
48         };
49
50         virtual ~Port();
51
52         std::string name() const { 
53                 return _name;
54         }
55
56         Flags flags() const {
57                 return _flags;
58         }
59
60         bool receives_input() const {
61                 return _flags & IsInput;
62         }
63
64         bool sends_output () const {
65                 return _flags & IsOutput;
66         }
67
68         bool can_monitor () const {
69                 return _flags & CanMonitor;
70         }
71
72         void enable_metering() {
73                 _metering++;
74         }
75         
76         void disable_metering () {
77                 if (_metering) { _metering--; }
78         }
79
80         virtual void cycle_start (nframes_t nframes, nframes_t offset) {}
81         virtual void cycle_end (nframes_t nframes, nframes_t offset) {}
82         virtual void flush_buffers (nframes_t nframes, nframes_t offset ) {}
83         virtual DataType type() const = 0;
84         virtual Buffer& get_buffer( nframes_t nframes, nframes_t offset ) = 0;
85
86         virtual bool connected () const;
87         virtual bool connected_to (const std::string& portname) const;
88         virtual int get_connections (std::vector<std::string>&) const;
89
90         virtual int connect (Port& other);
91         virtual int disconnect (Port& other);
92         virtual int disconnect_all ();
93         
94         virtual void reset ();
95         virtual int reestablish () {return 0; }
96         virtual int reconnect () { return 0; }
97
98         virtual int set_name (const std::string& str) {
99                 _name = str;
100                 return 0;
101         }
102
103         virtual std::string short_name() const = 0;
104         virtual bool monitoring_input () const = 0;
105         virtual void ensure_monitor_input (bool yn) = 0;
106         virtual void request_monitor_input (bool yn) = 0;
107         virtual nframes_t latency () const = 0;
108         virtual nframes_t total_latency () const = 0;
109         virtual void set_latency (nframes_t nframes) = 0;
110
111         sigc::signal<void,bool> MonitorInputChanged;
112         sigc::signal<void,bool> ClockSyncChanged;
113
114         static void set_engine (AudioEngine*);
115
116   protected:
117         friend class AudioEngine;
118
119         Port (const std::string& name, Flags flgs);
120
121         virtual void recompute_total_latency() const {}
122         
123         /* engine isn't supposed to access below here */
124
125         Flags          _flags;
126         std::string    _type;
127         std::string    _name;
128         unsigned short _metering;
129         bool           _last_monitor;
130         nframes_t      _latency;
131
132         std::set<Port*> _connections;
133
134         static AudioEngine* engine;
135 };
136
137 class PortConnectableByName {
138   public:
139         PortConnectableByName() {}
140         virtual ~PortConnectableByName() {}
141
142         virtual int connect (const std::string& other_name) = 0;
143         virtual int disconnect (const std::string& other_name) = 0;
144 };
145  
146 class PortFacade : public virtual Port, public PortConnectableByName { 
147   public:
148         PortFacade (const std::string& name, Flags flgs) : Port (name, flgs), _ext_port (0) {}
149         ~PortFacade() {}
150
151         void reset ();
152         int reestablish ();
153         int reconnect ();
154
155         int connect (Port& other);
156         int disconnect (Port& other);
157         int disconnect_all ();
158
159         int connect (const std::string& other_name);
160         int disconnect (const std::string& other_name);
161
162         bool connected () const;
163         bool connected_to (const std::string& portname) const;
164         int get_connections (std::vector<std::string>&) const;
165
166         std::string short_name() const;
167         int         set_name (const std::string& str);
168         bool        monitoring_input () const;
169         void        ensure_monitor_input (bool yn);
170         void        request_monitor_input (bool yn);
171         nframes_t   latency () const;
172         nframes_t   total_latency () const;
173         void        set_latency (nframes_t nframes);
174
175   protected:
176         Port* _ext_port;
177 };
178
179 } // namespace ARDOUR
180
181 #endif /* __ardour_port_h__ */