2 Copyright (C) 2002-2004 Paul Davis
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.
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.
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.
20 #ifndef __ardour_audioengine_h__
21 #define __ardour_audioengine_h__
24 #include "libardour-config.h"
34 #include <glibmm/thread.h>
37 #include "pbd/signals.h"
39 #include <jack/weakjack.h>
40 #include <jack/jack.h>
41 #include <jack/transport.h>
42 #include <jack/thread.h>
44 #include "ardour/ardour.h"
46 #include "ardour/data_type.h"
47 #include "ardour/session_handle.h"
48 #include "ardour/types.h"
49 #include "ardour/chan_count.h"
51 #ifdef HAVE_JACK_SESSION
52 #include <jack/session.h>
63 class AudioEngine : public SessionHandlePtr
66 typedef std::set<boost::shared_ptr<Port> > Ports;
68 AudioEngine (std::string client_name, std::string session_uuid);
69 virtual ~AudioEngine ();
71 jack_client_t* jack() const;
72 bool connected() const { return _jack != 0; }
74 bool is_realtime () const;
76 ProcessThread* main_thread() const { return _main_thread; }
78 std::string client_name() const { return jack_client_name; }
80 int reconnect_to_jack ();
81 int disconnect_from_jack();
83 int stop (bool forever = false);
85 bool running() const { return _running; }
87 Glib::Mutex& process_lock() { return _process_lock; }
89 framecnt_t frame_rate () const;
90 pframes_t frames_per_cycle () const;
92 size_t raw_buffer_size(DataType t);
94 int usecs_per_cycle () const { return _usecs_per_cycle; }
96 bool get_sync_offset (pframes_t & offset) const;
98 pframes_t frames_since_cycle_start () {
99 jack_client_t* _priv_jack = _jack;
100 if (!_running || !_priv_jack) {
103 return jack_frames_since_cycle_start (_priv_jack);
106 pframes_t frame_time () {
107 jack_client_t* _priv_jack = _jack;
108 if (!_running || !_priv_jack) {
111 return jack_frame_time (_priv_jack);
114 pframes_t frame_time_at_cycle_start () {
115 jack_client_t* _priv_jack = _jack;
116 if (!_running || !_priv_jack) {
119 return jack_last_frame_time (_priv_jack);
122 pframes_t transport_frame () const {
123 const jack_client_t* _priv_jack = _jack;
124 if (!_running || !_priv_jack) {
127 return jack_get_current_transport_frame (_priv_jack);
130 int request_buffer_size (pframes_t);
132 framecnt_t processed_frames() const { return _processed_frames; }
134 float get_cpu_load() {
135 jack_client_t* _priv_jack = _jack;
136 if (!_running || !_priv_jack) {
139 return jack_cpu_load (_priv_jack);
142 void set_session (Session *);
143 void remove_session (); // not a replacement for SessionHandle::session_going_away()
145 class PortRegistrationFailure : public std::exception {
147 PortRegistrationFailure (std::string const & why = "")
150 ~PortRegistrationFailure () throw () {}
152 virtual const char *what() const throw () { return reason.c_str(); }
158 class NoBackendAvailable : public std::exception {
160 virtual const char *what() const throw() { return "could not connect to engine backend"; }
163 boost::shared_ptr<Port> register_input_port (DataType, const std::string& portname);
164 boost::shared_ptr<Port> register_output_port (DataType, const std::string& portname);
165 int unregister_port (boost::shared_ptr<Port>);
167 bool port_is_physical (const std::string&) const;
168 void request_jack_monitors_input (const std::string&, bool) const;
170 void split_cycle (pframes_t offset);
172 int connect (const std::string& source, const std::string& destination);
173 int disconnect (const std::string& source, const std::string& destination);
174 int disconnect (boost::shared_ptr<Port>);
176 const char ** get_ports (const std::string& port_name_pattern, const std::string& type_name_pattern, uint32_t flags);
178 bool can_request_hardware_monitoring ();
180 ChanCount n_physical_outputs () const;
181 ChanCount n_physical_inputs () const;
183 void get_physical_outputs (DataType type, std::vector<std::string>&);
184 void get_physical_inputs (DataType type, std::vector<std::string>&);
186 boost::shared_ptr<Port> get_port_by_name (const std::string &);
188 enum TransportState {
189 TransportStopped = JackTransportStopped,
190 TransportRolling = JackTransportRolling,
191 TransportLooping = JackTransportLooping,
192 TransportStarting = JackTransportStarting
195 void transport_start ();
196 void transport_stop ();
197 void transport_locate (framepos_t);
198 TransportState transport_state ();
200 int reset_timebase ();
202 void update_latencies ();
204 /* start/stop freewheeling */
206 int freewheel (bool onoff);
207 bool freewheeling() const { return _freewheeling; }
209 /* this signal is sent for every process() cycle while freewheeling.
210 _ the regular process() call to session->process() is not made.
213 PBD::Signal1<int, pframes_t> Freewheel;
215 PBD::Signal0<void> Xrun;
217 /* this signal is if JACK notifies us of a graph order event */
219 PBD::Signal0<void> GraphReordered;
221 #ifdef HAVE_JACK_SESSION
222 PBD::Signal1<void,jack_session_event_t *> JackSessionEvent;
226 /* this signal is emitted if the sample rate changes */
228 PBD::Signal1<void, framecnt_t> SampleRateChanged;
230 /* this signal is sent if JACK ever disconnects us */
232 PBD::Signal1<void,const char*> Halted;
234 /* these two are emitted when the engine itself is
238 PBD::Signal0<void> Running;
239 PBD::Signal0<void> Stopped;
241 /** Emitted if a JACK port is registered or unregistered */
242 PBD::Signal0<void> PortRegisteredOrUnregistered;
244 /** Emitted if a JACK port is connected or disconnected.
245 * The Port parameters are the ports being connected / disconnected, or 0 if they are not known to Ardour.
246 * The std::string parameters are the (long) port names.
247 * The bool parameter is true if ports were connected, or false for disconnected.
249 PBD::Signal5<void, boost::weak_ptr<Port>, std::string, boost::weak_ptr<Port>, std::string, bool> PortConnectedOrDisconnected;
251 std::string make_port_name_relative (std::string) const;
252 std::string make_port_name_non_relative (std::string) const;
253 bool port_is_mine (const std::string&) const;
255 static AudioEngine* instance() { return _instance; }
256 static void destroy();
259 int create_process_thread (boost::function<void()>, pthread_t*, size_t stacksize);
262 static AudioEngine* _instance;
264 jack_client_t* volatile _jack; /* could be reset to null by SIGPIPE or another thread */
265 std::string jack_client_name;
266 Glib::Mutex _process_lock;
267 Glib::Cond session_removed;
268 bool session_remove_pending;
271 mutable framecnt_t _buffer_size;
272 std::map<DataType,size_t> _raw_buffer_sizes;
273 mutable framecnt_t _frame_rate;
274 /// number of frames between each check for changes in monitor input
275 framecnt_t monitor_check_interval;
276 /// time of the last monitor check in frames
277 framecnt_t last_monitor_check;
278 /// the number of frames processed since start() was called
279 framecnt_t _processed_frames;
281 int _usecs_per_cycle;
282 bool port_remove_in_progress;
284 SerializedRCUManager<Ports> ports;
286 boost::shared_ptr<Port> register_port (DataType type, const std::string& portname, bool input);
288 int process_callback (pframes_t nframes);
289 void* process_thread ();
290 void remove_all_ports ();
292 ChanCount n_physical (unsigned long) const;
293 void get_physical (DataType, unsigned long, std::vector<std::string> &);
295 void port_registration_failure (const std::string& portname);
297 static int _xrun_callback (void *arg);
298 #ifdef HAVE_JACK_SESSION
299 static void _session_callback (jack_session_event_t *event, void *arg);
301 static int _graph_order_callback (void *arg);
302 static int _process_callback (pframes_t nframes, void *arg);
303 static void* _process_thread (void *arg);
304 static int _sample_rate_callback (pframes_t nframes, void *arg);
305 static int _bufsize_callback (pframes_t nframes, void *arg);
306 static void _jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int, void*);
307 static int _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
308 static void _freewheel_callback (int , void *arg);
309 static void _registration_callback (jack_port_id_t, int, void *);
310 static void _connect_callback (jack_port_id_t, jack_port_id_t, int, void *);
312 void jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int);
313 int jack_sync_callback (jack_transport_state_t, jack_position_t*);
314 int jack_bufsize_callback (pframes_t);
315 int jack_sample_rate_callback (pframes_t);
317 void set_jack_callbacks ();
319 static void _latency_callback (jack_latency_callback_mode_t, void*);
320 void jack_latency_callback (jack_latency_callback_mode_t);
322 int connect_to_jack (std::string client_name, std::string session_uuid);
324 static void halted (void *);
325 static void halted_info (jack_status_t,const char*,void *);
327 void meter_thread ();
328 void start_metering_thread ();
329 void stop_metering_thread ();
331 Glib::Thread* m_meter_thread;
332 static gint m_meter_exit;
334 ProcessThread* _main_thread;
338 boost::function<void()> f;
341 ThreadData (AudioEngine* ae, boost::function<void()> fp, size_t stacksz)
342 : engine (ae) , f (fp) , stacksize (stacksz) {}
345 static void* _start_process_thread (void*);
348 } // namespace ARDOUR
350 #endif /* __ardour_audioengine_h__ */