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__
30 #include <glibmm/thread.h>
33 #include "pbd/signals.h"
35 #include "ardour/ardour.h"
36 #include <jack/jack.h>
37 #include <jack/transport.h>
39 #include "ardour/data_type.h"
40 #include "ardour/session_handle.h"
41 #include "ardour/types.h"
50 class AudioEngine : public SessionHandlePtr
53 typedef std::set<Port*> Ports;
55 AudioEngine (std::string client_name);
56 virtual ~AudioEngine ();
58 jack_client_t* jack() const;
59 bool connected() const { return _jack != 0; }
61 bool is_realtime () const;
63 std::string client_name() const { return jack_client_name; }
65 int reconnect_to_jack ();
66 int disconnect_from_jack();
68 bool will_reconnect_at_halt ();
69 void set_reconnect_at_halt (bool);
71 int stop (bool forever = false);
73 bool running() const { return _running; }
75 Glib::Mutex& process_lock() { return _process_lock; }
77 nframes_t frame_rate();
78 nframes_t frames_per_cycle();
80 size_t raw_buffer_size(DataType t);
82 int usecs_per_cycle () const { return _usecs_per_cycle; }
84 bool get_sync_offset (nframes_t& offset) const;
86 nframes_t frames_since_cycle_start () {
87 jack_client_t* _priv_jack = _jack;
88 if (!_running || !_priv_jack) return 0;
89 return jack_frames_since_cycle_start (_priv_jack);
91 nframes_t frame_time () {
92 jack_client_t* _priv_jack = _jack;
93 if (!_running || !_priv_jack) return 0;
94 return jack_frame_time (_priv_jack);
97 nframes_t transport_frame () const {
98 const jack_client_t* _priv_jack = _jack;
99 if (!_running || !_priv_jack) return 0;
100 return jack_get_current_transport_frame (_priv_jack);
103 int request_buffer_size (nframes_t);
105 nframes_t set_monitor_check_interval (nframes_t);
106 nframes_t processed_frames() const { return _processed_frames; }
108 float get_cpu_load() {
109 jack_client_t* _priv_jack = _jack;
110 if (!_running || !_priv_jack) return 0;
111 return jack_cpu_load (_priv_jack);
114 void set_session (Session *);
115 void remove_session (); // not a replacement for SessionHandle::session_going_away()
117 class PortRegistrationFailure : public std::exception {
119 PortRegistrationFailure (const char* why = "") {
122 virtual const char *what() const throw() { return reason; }
128 class NoBackendAvailable : public std::exception {
130 virtual const char *what() const throw() { return "could not connect to engine backend"; }
133 Port *register_input_port (DataType, const std::string& portname);
134 Port *register_output_port (DataType, const std::string& portname);
135 int unregister_port (Port &);
137 void split_cycle (nframes_t offset);
139 int connect (const std::string& source, const std::string& destination);
140 int disconnect (const std::string& source, const std::string& destination);
141 int disconnect (Port &);
143 const char ** get_ports (const std::string& port_name_pattern, const std::string& type_name_pattern, uint32_t flags);
145 bool can_request_hardware_monitoring ();
147 uint32_t n_physical_outputs (DataType type) const;
148 uint32_t n_physical_inputs (DataType type) const;
150 void get_physical_outputs (DataType type, std::vector<std::string>&);
151 void get_physical_inputs (DataType type, std::vector<std::string>&);
153 std::string get_nth_physical_output (DataType type, uint32_t n) {
154 return get_nth_physical (type, n, JackPortIsInput);
157 std::string get_nth_physical_input (DataType type, uint32_t n) {
158 return get_nth_physical (type, n, JackPortIsOutput);
161 void update_total_latencies ();
162 void update_total_latency (const Port&);
164 Port *get_port_by_name (const std::string &);
165 Port *get_port_by_name_locked (const std::string &);
167 enum TransportState {
168 TransportStopped = JackTransportStopped,
169 TransportRolling = JackTransportRolling,
170 TransportLooping = JackTransportLooping,
171 TransportStarting = JackTransportStarting
174 void transport_start ();
175 void transport_stop ();
176 void transport_locate (nframes_t);
177 TransportState transport_state ();
179 int reset_timebase ();
181 /* start/stop freewheeling */
183 int freewheel (bool onoff);
184 bool freewheeling() const { return _freewheeling; }
186 /* this signal is sent for every process() cycle while freewheeling.
187 _ the regular process() call to session->process() is not made.
190 PBD::Signal1<int,nframes_t> Freewheel;
192 PBD::Signal0<void> Xrun;
194 /* this signal is if JACK notifies us of a graph order event */
196 PBD::Signal0<void> GraphReordered;
198 /* this signal is emitted if the sample rate changes */
200 PBD::Signal1<void,nframes_t> SampleRateChanged;
202 /* this signal is sent if JACK ever disconnects us */
204 PBD::Signal0<void> Halted;
206 /* these two are emitted when the engine itself is
210 PBD::Signal0<void> Running;
211 PBD::Signal0<void> Stopped;
213 /* this signal is emitted if a JACK port is registered or unregistered */
215 PBD::Signal0<void> PortRegisteredOrUnregistered;
217 std::string make_port_name_relative (std::string);
218 std::string make_port_name_non_relative (std::string);
220 static AudioEngine* instance() { return _instance; }
224 static AudioEngine* _instance;
226 jack_client_t* volatile _jack; /* could be reset to null by SIGPIPE or another thread */
227 std::string jack_client_name;
228 Glib::Mutex _process_lock;
229 Glib::Cond session_removed;
230 bool session_remove_pending;
233 nframes_t _buffer_size;
234 std::map<DataType,size_t> _raw_buffer_sizes;
235 nframes_t _frame_rate;
236 /// number of frames between each check for changes in monitor input
237 nframes_t monitor_check_interval;
238 /// time of the last monitor check in frames
239 nframes_t last_monitor_check;
240 /// the number of frames processed since start() was called
241 nframes_t _processed_frames;
243 bool _freewheel_pending;
244 bool _freewheel_thread_registered;
245 boost::function<int(nframes_t)> freewheel_action;
246 bool reconnect_on_halt;
247 int _usecs_per_cycle;
249 SerializedRCUManager<Ports> ports;
251 Port *register_port (DataType type, const std::string& portname, bool input);
253 int process_callback (nframes_t nframes);
254 void remove_all_ports ();
256 std::string get_nth_physical (DataType type, uint32_t n, int flags);
258 void port_registration_failure (const std::string& portname);
260 static int _xrun_callback (void *arg);
261 static int _graph_order_callback (void *arg);
262 static int _process_callback (nframes_t nframes, void *arg);
263 static int _sample_rate_callback (nframes_t nframes, void *arg);
264 static int _bufsize_callback (nframes_t nframes, void *arg);
265 static void _jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int, void*);
266 static int _jack_sync_callback (jack_transport_state_t, jack_position_t*, void *arg);
267 static void _freewheel_callback (int , void *arg);
268 static void _registration_callback (jack_port_id_t, int, void *);
270 void jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int);
271 int jack_sync_callback (jack_transport_state_t, jack_position_t*);
272 int jack_bufsize_callback (nframes_t);
273 int jack_sample_rate_callback (nframes_t);
275 int connect_to_jack (std::string client_name);
277 static void halted (void *);
279 void meter_thread ();
280 void start_metering_thread ();
281 void stop_metering_thread ();
283 Glib::Thread* m_meter_thread;
284 static gint m_meter_exit;
287 } // namespace ARDOUR
289 #endif /* __ardour_audioengine_h__ */