changes from torben for processor/plugin count determination and other fixes; rework...
[ardour.git] / libs / ardour / ardour / audioengine.h
1 /*
2     Copyright (C) 2002-2004 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_audioengine_h__
21 #define __ardour_audioengine_h__
22
23 #include <list>
24 #include <set>
25 #include <cmath>
26 #include <exception>
27 #include <string>
28
29 #include <sigc++/signal.h>
30
31 #include <glibmm/thread.h>
32
33 #include <pbd/rcu.h>
34
35 #include <ardour/ardour.h>
36 #include <jack/jack.h>
37 #include <jack/transport.h>
38 #include <ardour/types.h>
39 #include <ardour/data_type.h>
40
41 namespace ARDOUR {
42
43 class Session;
44 class Port;
45 class InternalPort;
46
47 class AudioEngine : public sigc::trackable
48 {
49    public:
50         typedef std::set<Port*> Ports;
51
52         AudioEngine (std::string client_name);
53         virtual ~AudioEngine ();
54         
55         jack_client_t* jack() const;
56         bool connected() const { return _jack != 0; }
57
58         bool is_realtime () const;
59
60         std::string client_name() const { return jack_client_name; }
61
62         int reconnect_to_jack ();
63         int disconnect_from_jack();
64
65         bool will_reconnect_at_halt ();
66         void set_reconnect_at_halt (bool);
67
68         int stop (bool forever = false);
69         int start ();
70         bool running() const { return _running; }
71
72         Glib::Mutex& process_lock() { return _process_lock; }
73
74         nframes_t frame_rate();
75         nframes_t frames_per_cycle();
76
77         int usecs_per_cycle () const { return _usecs_per_cycle; }
78
79         bool get_sync_offset (nframes_t& offset) const;
80
81         nframes_t frames_since_cycle_start () {
82                 if (!_running || !_jack) return 0;
83                 return jack_frames_since_cycle_start (_jack);
84         }
85         nframes_t frame_time () {
86                 if (!_running || !_jack) return 0;
87                 return jack_frame_time (_jack);
88         }
89
90         nframes_t transport_frame () const {
91                 if (!_running || !_jack) return 0;
92                 return jack_get_current_transport_frame (_jack);
93         }
94         
95         int request_buffer_size (nframes_t);
96         
97         nframes_t set_monitor_check_interval (nframes_t);
98
99         float get_cpu_load() { 
100                 if (!_running || !_jack) return 0;
101                 return jack_cpu_load (_jack);
102         }
103
104         void set_session (Session *);
105         void remove_session ();
106
107         class PortRegistrationFailure : public std::exception {
108           public:
109                 PortRegistrationFailure (const char* why = "") {
110                         reason = why;
111                 }
112                 virtual const char *what() const throw() { return reason; }
113
114           private:
115                 const char* reason;
116         };
117
118         class NoBackendAvailable : public std::exception {
119           public:
120                 virtual const char *what() const throw() { return "could not connect to engine backend"; }
121         };
122
123         Port *register_input_port (DataType, const std::string& portname, bool publish);
124         Port *register_output_port (DataType, const std::string& portname, bool publish);
125         int   unregister_port (Port &);
126         
127         int connect (const std::string& source, const std::string& destination);
128         int disconnect (const std::string& source, const std::string& destination);
129         int disconnect (Port &);
130         
131         const char ** get_ports (const std::string& port_name_pattern, const std::string& type_name_pattern, uint32_t flags);
132
133         bool can_request_hardware_monitoring ();
134
135         uint32_t n_physical_outputs (DataType type) const;
136         uint32_t n_physical_inputs (DataType type) const;
137
138         void get_physical_outputs (DataType type, std::vector<std::string>&);
139         void get_physical_inputs (DataType type, std::vector<std::string>&);
140
141         std::string get_nth_physical_output (DataType type, uint32_t n) {
142                 return get_nth_physical (type, n, JackPortIsInput);
143         }
144
145         std::string get_nth_physical_input (DataType type, uint32_t n) {
146                 return get_nth_physical (type, n, JackPortIsOutput);
147         }
148
149         nframes_t get_port_total_latency (const Port&);
150         void update_total_latencies ();
151         void update_total_latency (const Port&);
152
153         /** Caller may not delete the object pointed to by the return value
154         */
155         Port *get_port_by_name (const std::string& name, bool keep = true);
156
157         enum TransportState {
158                 TransportStopped = JackTransportStopped,
159                 TransportRolling = JackTransportRolling,
160                 TransportLooping = JackTransportLooping,
161                 TransportStarting = JackTransportStarting
162         };
163
164         void transport_start ();
165         void transport_stop ();
166         void transport_locate (nframes_t);
167         TransportState transport_state ();
168
169         int  reset_timebase ();
170
171         /* start/stop freewheeling */
172
173         int freewheel (bool onoff);
174         bool freewheeling() const { return _freewheeling; }
175
176         /* this signal is sent for every process() cycle while freewheeling.
177            the regular process() call to session->process() is not made.
178         */
179
180         sigc::signal<int,nframes_t> Freewheel;
181
182         sigc::signal<void> Xrun;
183
184         /* this signal is if JACK notifies us of a graph order event */
185
186         sigc::signal<void> GraphReordered;
187
188         /* this signal is emitted if the sample rate changes */
189
190         sigc::signal<void,nframes_t> SampleRateChanged;
191
192         /* this signal is sent if JACK ever disconnects us */
193
194         sigc::signal<void> Halted;
195
196         /* these two are emitted when the engine itself is
197            started and stopped
198         */
199
200         sigc::signal<void> Running;
201         sigc::signal<void> Stopped;
202
203         std::string make_port_name_relative (std::string);
204         std::string make_port_name_non_relative (std::string);
205
206   private:
207         ARDOUR::Session*           session;
208         jack_client_t*            _jack;
209         std::string                jack_client_name;
210         Glib::Mutex               _process_lock;
211         Glib::Cond                 session_removed;
212         bool                       session_remove_pending;
213         bool                      _running;
214         bool                      _has_run;
215         nframes_t                 _buffer_size;
216         nframes_t                 _frame_rate;
217         /// number of frames between each check for changes in monitor input
218         nframes_t                  monitor_check_interval;
219         /// time of the last monitor check in frames
220         nframes_t                  last_monitor_check;
221         /// the number of frames processed since start() was called
222         nframes_t                 _processed_frames;
223         bool                      _freewheeling;
224         bool                      _freewheel_pending;
225         bool                      _freewheel_thread_registered;
226         sigc::slot<int,nframes_t>  freewheel_action;
227         bool                       reconnect_on_halt;
228         int                       _usecs_per_cycle;
229
230         SerializedRCUManager<Ports> ports;
231
232         Port *register_port (DataType type, const std::string& portname, bool input, bool publish);
233
234         int    process_callback (nframes_t nframes);
235         void   remove_all_ports ();
236
237         Port* get_port (const std::string& short_name);
238
239         typedef std::pair<std::string,std::string> PortConnection;
240         typedef std::list<PortConnection> PortConnections;
241
242         PortConnections port_connections;
243         void   remove_connections_for (Port&);
244
245         std::string get_nth_physical (DataType type, uint32_t n, int flags);
246
247         void port_registration_failure (const std::string& portname);
248
249         static int  _xrun_callback (void *arg);
250         static int  _graph_order_callback (void *arg);
251         static int  _process_callback (nframes_t nframes, void *arg);
252         static int  _sample_rate_callback (nframes_t nframes, void *arg);
253         static int  _bufsize_callback (nframes_t nframes, void *arg);
254         static void _jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int, void*);
255         static int  _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
256         static void _freewheel_callback (int , void *arg);
257
258         void jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int);
259         int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
260         int  jack_bufsize_callback (nframes_t);
261         int  jack_sample_rate_callback (nframes_t);
262
263         static void halted (void *);
264
265         int connect_to_jack (std::string client_name);
266
267         void meter_thread ();
268         void start_metering_thread ();
269         void stop_metering_thread ();
270
271         Glib::Thread*    m_meter_thread;
272         static gint      m_meter_exit;
273 };
274
275 } // namespace ARDOUR
276
277 #endif /* __ardour_audioengine_h__ */