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/threads.h>
36 #include "pbd/signals.h"
37 #include "pbd/stacktrace.h"
39 #include "ardour/ardour.h"
40 #include "ardour/data_type.h"
41 #include "ardour/session_handle.h"
42 #include "ardour/libardour_visibility.h"
43 #include "ardour/types.h"
44 #include "ardour/chan_count.h"
45 #include "ardour/port_manager.h"
58 struct AudioBackendInfo;
60 class LIBARDOUR_API AudioEngine : public PortManager, public SessionHandlePtr
64 static AudioEngine* create ();
66 virtual ~AudioEngine ();
68 int discover_backends();
69 std::vector<const AudioBackendInfo*> available_backends() const;
70 std::string current_backend_name () const;
71 boost::shared_ptr<AudioBackend> set_default_backend ();
72 boost::shared_ptr<AudioBackend> set_backend (const std::string&, const std::string& arg1, const std::string& arg2);
73 boost::shared_ptr<AudioBackend> current_backend() const { return _backend; }
74 bool setup_required () const;
76 ProcessThread* main_thread() const { return _main_thread; }
78 /* START BACKEND PROXY API
80 * See audio_backend.h for full documentation and semantics. These wrappers
81 * just forward to a backend implementation.
84 int start (bool for_latency_measurement=false);
85 int stop (bool for_latency_measurement=false);
86 int freewheel (bool start_stop);
87 float get_dsp_load() const ;
88 void transport_start ();
89 void transport_stop ();
90 TransportState transport_state ();
91 void transport_locate (framepos_t pos);
92 framepos_t transport_frame();
93 framecnt_t sample_rate () const;
94 pframes_t samples_per_cycle () const;
95 int usecs_per_cycle () const;
96 size_t raw_buffer_size (DataType t);
97 framepos_t sample_time ();
98 framepos_t sample_time_at_cycle_start ();
99 pframes_t samples_since_cycle_start ();
100 bool get_sync_offset (pframes_t& offset) const;
102 std::string get_last_backend_error () const { return _last_backend_error_string; }
104 int create_process_thread (boost::function<void()> func);
105 int join_process_threads ();
106 bool in_process_thread ();
107 uint32_t process_thread_count ();
109 int backend_reset_requested();
110 void request_backend_reset();
111 void request_device_list_update();
112 void launch_device_control_app();
114 bool is_realtime() const;
115 bool connected() const;
117 // for the user which hold state_lock to check if reset operation is pending
118 bool is_reset_requested() const { return g_atomic_int_get(const_cast<gint*>(&_hw_reset_request_count)); }
120 int set_device_name (const std::string&);
121 int set_sample_rate (float);
122 int set_buffer_size (uint32_t);
123 int set_interleaved (bool yn);
124 int set_input_channels (uint32_t);
125 int set_output_channels (uint32_t);
126 int set_systemic_input_latency (uint32_t);
127 int set_systemic_output_latency (uint32_t);
129 /* END BACKEND PROXY API */
131 bool freewheeling() const { return _freewheeling; }
132 bool running() const { return _running; }
134 Glib::Threads::Mutex& process_lock() { return _process_lock; }
135 Glib::Threads::RecMutex& state_lock() { return _state_lock; }
137 int request_buffer_size (pframes_t samples) {
138 return set_buffer_size (samples);
141 framecnt_t processed_frames() const { return _processed_frames; }
143 void set_session (Session *);
144 void remove_session (); // not a replacement for SessionHandle::session_going_away()
145 Session* session() const { return _session; }
147 void reconnect_session_routes (bool reconnect_inputs = true, bool reconnect_outputs = true);
149 class NoBackendAvailable : public std::exception {
151 virtual const char *what() const throw() { return "could not connect to engine backend"; }
154 void split_cycle (pframes_t offset);
156 int reset_timebase ();
158 void update_latencies ();
160 /* this signal is sent for every process() cycle while freewheeling.
161 (the regular process() call to session->process() is not made)
164 PBD::Signal1<int, pframes_t> Freewheel;
166 PBD::Signal0<void> Xrun;
168 /** this signal is emitted if the sample rate changes */
169 PBD::Signal1<void, framecnt_t> SampleRateChanged;
171 /** this signal is emitted if the buffer size changes */
172 PBD::Signal1<void, pframes_t> BufferSizeChanged;
174 /** this signal is emitted if the device cannot operate properly */
175 PBD::Signal0<void> DeviceError;
177 /* this signal is emitted if the device list changed */
179 PBD::Signal0<void> DeviceListChanged;
181 /* this signal is sent if the backend ever disconnects us */
183 PBD::Signal1<void,const char*> Halted;
185 /* these two are emitted when the engine itself is
189 PBD::Signal0<void> Running;
190 PBD::Signal0<void> Stopped;
192 /* these two are emitted when a device reset is initiated/finished
195 PBD::Signal0<void> DeviceResetStarted;
196 PBD::Signal0<void> DeviceResetFinished;
198 static AudioEngine* instance() { return _instance; }
199 static void destroy();
202 /* The backend will cause these at the appropriate time(s)
204 int process_callback (pframes_t nframes);
205 int buffer_size_change (pframes_t nframes);
206 int sample_rate_change (pframes_t nframes);
207 void freewheel_callback (bool);
208 void timebase_callback (TransportState state, pframes_t nframes, framepos_t pos, int new_position);
209 int sync_callback (TransportState state, framepos_t position);
210 int port_registration_callback ();
211 void latency_callback (bool for_playback);
212 void halted_callback (const char* reason);
214 /* checks if current thread is properly set up for audio processing */
215 static bool thread_initialised_for_audio_processing ();
217 /* sets up the process callback thread */
218 static void thread_init_callback (void *);
220 /* latency measurement */
222 MTDM* mtdm() { return _mtdm; }
223 MIDIDM* mididm() { return _mididm; }
225 int prepare_for_latency_measurement ();
226 int start_latency_detection (bool);
227 void stop_latency_detection ();
228 void set_latency_input_port (const std::string&);
229 void set_latency_output_port (const std::string&);
230 uint32_t latency_signal_delay () const { return _latency_signal_latency; }
232 enum LatencyMeasurement {
238 LatencyMeasurement measuring_latency () const { return _measuring_latency; }
240 /* These two are used only in builds where SILENCE_AFTER_SECONDS was
241 * set. BecameSilent will be emitted when the audioengine goes silent.
242 * reset_silence_countdown() can be used to reset the silence
243 * countdown, whose duration will be reduced to half of its previous
247 PBD::Signal0<void> BecameSilent;
248 void reset_silence_countdown ();
253 static AudioEngine* _instance;
255 Glib::Threads::Mutex _process_lock;
256 Glib::Threads::RecMutex _state_lock;
257 Glib::Threads::Cond session_removed;
258 bool session_remove_pending;
259 frameoffset_t session_removal_countdown;
260 gain_t session_removal_gain;
261 gain_t session_removal_gain_step;
264 /// number of frames between each check for changes in monitor input
265 framecnt_t monitor_check_interval;
266 /// time of the last monitor check in frames
267 framecnt_t last_monitor_check;
268 /// the number of frames processed since start() was called
269 framecnt_t _processed_frames;
270 Glib::Threads::Thread* m_meter_thread;
271 ProcessThread* _main_thread;
274 LatencyMeasurement _measuring_latency;
275 PortEngine::PortHandle _latency_input_port;
276 PortEngine::PortHandle _latency_output_port;
277 framecnt_t _latency_flush_frames;
278 std::string _latency_input_name;
279 std::string _latency_output_name;
280 framecnt_t _latency_signal_latency;
281 bool _stopped_for_latency;
282 bool _started_for_latency;
285 std::string _last_backend_error_string;
287 Glib::Threads::Thread* _hw_reset_event_thread;
288 gint _hw_reset_request_count;
289 Glib::Threads::Cond _hw_reset_condition;
290 Glib::Threads::Mutex _reset_request_lock;
291 gint _stop_hw_reset_processing;
292 Glib::Threads::Thread* _hw_devicelist_update_thread;
293 gint _hw_devicelist_update_count;
294 Glib::Threads::Cond _hw_devicelist_update_condition;
295 Glib::Threads::Mutex _devicelist_update_lock;
296 gint _stop_hw_devicelist_processing;
298 void start_hw_event_processing();
299 void stop_hw_event_processing();
300 void do_reset_backend();
301 void do_devicelist_update();
303 typedef std::map<std::string,AudioBackendInfo*> BackendMap;
304 BackendMap _backends;
305 AudioBackendInfo* backend_discover (const std::string&);
306 void drop_backend ();
309 framecnt_t _silence_countdown;
310 uint32_t _silence_hit_cnt;
315 } // namespace ARDOUR
317 #endif /* __ardour_audioengine_h__ */