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"
35 #include <glibmm/thread.h>
38 #include "pbd/signals.h"
40 #include "ardour/ardour.h"
41 #include <jack/jack.h>
42 #include <jack/transport.h>
44 #include "ardour/data_type.h"
45 #include "ardour/session_handle.h"
46 #include "ardour/types.h"
47 #include "ardour/chan_count.h"
49 #ifdef HAVE_JACK_SESSION
50 #include <jack/session.h>
61 class AudioEngine : public SessionHandlePtr
64 typedef std::set<Port*> Ports;
66 AudioEngine (std::string client_name, std::string session_uuid);
67 virtual ~AudioEngine ();
69 jack_client_t* jack() const;
70 bool connected() const { return _jack != 0; }
72 bool is_realtime () const;
74 ProcessThread* main_thread() const { return _main_thread; }
76 std::string client_name() const { return jack_client_name; }
78 int reconnect_to_jack ();
79 int disconnect_from_jack();
81 bool will_reconnect_at_halt ();
82 void set_reconnect_at_halt (bool);
84 int stop (bool forever = false);
86 bool running() const { return _running; }
88 Glib::Mutex& process_lock() { return _process_lock; }
90 nframes_t frame_rate() const;
91 nframes_t frames_per_cycle() const;
93 size_t raw_buffer_size(DataType t);
95 int usecs_per_cycle () const { return _usecs_per_cycle; }
97 bool get_sync_offset (nframes_t& offset) const;
99 nframes_t frames_since_cycle_start () {
100 jack_client_t* _priv_jack = _jack;
101 if (!_running || !_priv_jack) return 0;
102 return jack_frames_since_cycle_start (_priv_jack);
104 nframes_t frame_time () {
105 jack_client_t* _priv_jack = _jack;
106 if (!_running || !_priv_jack) return 0;
107 return jack_frame_time (_priv_jack);
110 nframes_t frame_time_at_cycle_start () {
111 jack_client_t* _priv_jack = _jack;
112 if (!_running || !_priv_jack) return 0;
113 return jack_last_frame_time (_priv_jack);
116 nframes_t transport_frame () const {
117 const jack_client_t* _priv_jack = _jack;
118 if (!_running || !_priv_jack) return 0;
119 return jack_get_current_transport_frame (_priv_jack);
122 int request_buffer_size (nframes_t);
124 nframes_t set_monitor_check_interval (nframes_t);
125 nframes_t processed_frames() const { return _processed_frames; }
127 float get_cpu_load() {
128 jack_client_t* _priv_jack = _jack;
129 if (!_running || !_priv_jack) return 0;
130 return jack_cpu_load (_priv_jack);
133 void set_session (Session *);
134 void remove_session (); // not a replacement for SessionHandle::session_going_away()
136 class PortRegistrationFailure : public std::exception {
138 PortRegistrationFailure (std::string const & why = "")
141 ~PortRegistrationFailure () throw () {}
143 virtual const char *what() const throw () { return reason.c_str(); }
149 class NoBackendAvailable : public std::exception {
151 virtual const char *what() const throw() { return "could not connect to engine backend"; }
154 Port *register_input_port (DataType, const std::string& portname);
155 Port *register_output_port (DataType, const std::string& portname);
156 int unregister_port (Port &);
158 void split_cycle (nframes_t offset);
160 int connect (const std::string& source, const std::string& destination);
161 int disconnect (const std::string& source, const std::string& destination);
162 int disconnect (Port &);
164 const char ** get_ports (const std::string& port_name_pattern, const std::string& type_name_pattern, uint32_t flags);
166 bool can_request_hardware_monitoring ();
168 ChanCount n_physical_outputs () const;
169 ChanCount n_physical_inputs () const;
171 void get_physical_outputs (DataType type, std::vector<std::string>&);
172 void get_physical_inputs (DataType type, std::vector<std::string>&);
174 void update_total_latencies ();
175 void update_total_latency (const Port&);
177 Port *get_port_by_name (const std::string &);
178 Port *get_port_by_name_locked (const std::string &);
180 enum TransportState {
181 TransportStopped = JackTransportStopped,
182 TransportRolling = JackTransportRolling,
183 TransportLooping = JackTransportLooping,
184 TransportStarting = JackTransportStarting
187 void transport_start ();
188 void transport_stop ();
189 void transport_locate (nframes_t);
190 TransportState transport_state ();
192 int reset_timebase ();
194 /* start/stop freewheeling */
196 int freewheel (bool onoff);
197 bool freewheeling() const { return _freewheeling; }
199 /* this signal is sent for every process() cycle while freewheeling.
200 _ the regular process() call to session->process() is not made.
203 PBD::Signal1<int,nframes_t> Freewheel;
205 PBD::Signal0<void> Xrun;
207 /* this signal is if JACK notifies us of a graph order event */
209 PBD::Signal0<void> GraphReordered;
211 #ifdef HAVE_JACK_SESSION
212 PBD::Signal1<void,jack_session_event_t *> JackSessionEvent;
216 /* this signal is emitted if the sample rate changes */
218 PBD::Signal1<void,nframes_t> SampleRateChanged;
220 /* this signal is sent if JACK ever disconnects us */
222 PBD::Signal1<void,const char*> Halted;
224 /* these two are emitted when the engine itself is
228 PBD::Signal0<void> Running;
229 PBD::Signal0<void> Stopped;
231 /** Emitted if a JACK port is registered or unregistered */
232 PBD::Signal0<void> PortRegisteredOrUnregistered;
234 /** Emitted if a JACK port is connected or disconnected */
235 PBD::Signal0<void> PortConnectedOrDisconnected;
237 std::string make_port_name_relative (std::string);
238 std::string make_port_name_non_relative (std::string);
240 static AudioEngine* instance() { return _instance; }
243 int create_process_thread (boost::function<void()>, pthread_t*, size_t stacksize);
246 static AudioEngine* _instance;
248 jack_client_t* volatile _jack; /* could be reset to null by SIGPIPE or another thread */
249 std::string jack_client_name;
250 Glib::Mutex _process_lock;
251 Glib::Cond session_removed;
252 bool session_remove_pending;
255 mutable nframes_t _buffer_size;
256 std::map<DataType,size_t> _raw_buffer_sizes;
257 mutable nframes_t _frame_rate;
258 /// number of frames between each check for changes in monitor input
259 nframes_t monitor_check_interval;
260 /// time of the last monitor check in frames
261 nframes_t last_monitor_check;
262 /// the number of frames processed since start() was called
263 nframes_t _processed_frames;
265 bool _freewheel_pending;
266 boost::function<int(nframes_t)> freewheel_action;
267 bool reconnect_on_halt;
268 int _usecs_per_cycle;
270 SerializedRCUManager<Ports> ports;
272 Port *register_port (DataType type, const std::string& portname, bool input);
274 int process_callback (nframes_t nframes);
275 void* process_thread ();
276 void finish_process_cycle (int status);
277 void remove_all_ports ();
279 ChanCount n_physical (unsigned long) const;
280 void get_physical (DataType, unsigned long, std::vector<std::string> &);
282 void port_registration_failure (const std::string& portname);
284 static int _xrun_callback (void *arg);
285 #ifdef HAVE_JACK_SESSION
286 static void _session_callback (jack_session_event_t *event, void *arg);
288 static int _graph_order_callback (void *arg);
289 static int _process_callback (nframes_t nframes, void *arg);
290 static void* _process_thread (void *arg);
291 static int _sample_rate_callback (nframes_t nframes, void *arg);
292 static int _bufsize_callback (nframes_t nframes, void *arg);
293 static void _jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int, void*);
294 static int _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
295 static void _freewheel_callback (int , void *arg);
296 static void _registration_callback (jack_port_id_t, int, void *);
297 static void _connect_callback (jack_port_id_t, jack_port_id_t, int, void *);
299 void jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int);
300 int jack_sync_callback (jack_transport_state_t, jack_position_t*);
301 int jack_bufsize_callback (nframes_t);
302 int jack_sample_rate_callback (nframes_t);
304 int connect_to_jack (std::string client_name, std::string session_uuid);
306 static void halted (void *);
307 static void halted_info (jack_status_t,const char*,void *);
309 void meter_thread ();
310 void start_metering_thread ();
311 void stop_metering_thread ();
313 Glib::Thread* m_meter_thread;
314 static gint m_meter_exit;
316 ProcessThread* _main_thread;
320 boost::function<void()> f;
323 ThreadData (AudioEngine* ae, boost::function<void()> fp, size_t stacksz)
324 : engine (ae) , f (fp) , stacksize (stacksz) {}
327 static void* _start_process_thread (void*);
330 } // namespace ARDOUR
332 #endif /* __ardour_audioengine_h__ */