Ardour  8.12
audioengine.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011 David Robillard <d@drobilla.net>
3  * Copyright (C) 2006-2019 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2012-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2013-2015 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef __ardour_audioengine_h__
24 #define __ardour_audioengine_h__
25 
26 #ifdef WAF_BUILD
27 #include "libardour-config.h"
28 #endif
29 
30 #include <atomic>
31 #include <iostream>
32 #include <list>
33 #include <set>
34 #include <cmath>
35 #include <exception>
36 #include <string>
37 
38 #include <glibmm/threads.h>
39 
40 #include "pbd/signals.h"
41 #include "pbd/pthread_utils.h"
42 
43 #include "ardour/ardour.h"
44 #include "ardour/data_type.h"
45 #include "ardour/session_handle.h"
47 #include "ardour/types.h"
48 #include "ardour/chan_count.h"
49 #include "ardour/port_manager.h"
50 
51 class MTDM;
52 
53 namespace ARDOUR {
54 
55 class InternalPort;
56 class MidiPort;
57 class MIDIDM;
58 class Port;
59 class Session;
60 class ProcessThread;
61 class AudioBackend;
62 struct AudioBackendInfo;
63 
65 {
66  public:
67 
68  static AudioEngine* create ();
69 
70  virtual ~AudioEngine ();
71 
73  std::vector<const AudioBackendInfo*> available_backends() const;
74  std::string current_backend_name () const;
75  std::shared_ptr<AudioBackend> set_backend (const std::string&, const std::string& arg1, const std::string& arg2);
76  std::shared_ptr<AudioBackend> current_backend() const { return _backend; }
77  bool setup_required () const;
78  bool is_jack () const;
79 
80  ProcessThread* main_thread() const { return _main_thread; }
81 
82  /* START BACKEND PROXY API
83  *
84  * See audio_backend.h for full documentation and semantics. These wrappers
85  * just forward to a backend implementation.
86  */
87 
88  int start (bool for_latency_measurement=false);
89  int stop (bool for_latency_measurement=false);
90  int freewheel (bool start_stop);
91  float get_dsp_load() const ;
92  void transport_start ();
93  void transport_stop ();
99  int usecs_per_cycle () const;
104  bool get_sync_offset (pframes_t& offset) const;
105 
106  std::string get_last_backend_error () const { return _last_backend_error_string; }
107 
108  int create_process_thread (boost::function<void()> func);
111  uint32_t process_thread_count ();
112 
117 
118  // for the user which hold state_lock to check if reset operation is pending
119  bool is_reset_requested() const { return _hw_reset_request_count.load(); }
120 
121  int set_device_name (const std::string&);
122  int set_sample_rate (float);
123  int set_buffer_size (uint32_t);
124  int set_interleaved (bool yn);
127 
128  /* END BACKEND PROXY API */
129 
130  bool freewheeling() const { return _freewheeling; }
131  bool running() const { return _running; }
132 
133  std::string backend_id (bool for_input);
134 
135  Glib::Threads::Mutex& process_lock() { return _process_lock; }
136  Glib::Threads::Mutex& latency_lock() { return _latency_lock; }
137 
139  return set_buffer_size (samples);
140  }
141 
142  samplecnt_t processed_samples() const { return _processed_samples; }
143 
145  void remove_session (); // not a replacement for SessionHandle::session_going_away()
146  Session* session() const { return _session; }
147 
148  class NoBackendAvailable : public std::exception {
149  public:
150  virtual const char *what() const throw() { return "could not connect to engine backend"; }
151  };
152 
153  void split_cycle (pframes_t offset);
154 
156 
158 
159  /* this signal is sent for every process() cycle while freewheeling.
160  (the regular process() call to session->process() is not made)
161  */
162 
163  PBD::Signal1<void, pframes_t> Freewheel;
164 
165  PBD::Signal0<void> Xrun;
166 
168  PBD::Signal1<void, samplecnt_t> SampleRateChanged;
169 
171  PBD::Signal1<void, pframes_t> BufferSizeChanged;
172 
174  PBD::Signal0<void> DeviceError;
175 
176  /* this signal is emitted if the device list changed */
177 
178  PBD::Signal0<void> DeviceListChanged;
179 
180  /* this signal is sent if the backend ever disconnects us */
181 
182  PBD::Signal1<void,const char*> Halted;
183 
184  /* these two are emitted when the engine itself is
185  started and stopped
186  */
187 
188  PBD::Signal1<void,uint32_t> Running;
189  PBD::Signal0<void> Stopped;
190 
191  /* these two are emitted when a device reset is initiated/finished
192  */
193 
194  PBD::Signal0<void> DeviceResetStarted;
195  PBD::Signal0<void> DeviceResetFinished;
196 
197  static AudioEngine* instance() { return _instance; }
198  static void destroy();
199 
200  void died ();
201 
202  /* The backend will cause these at the appropriate time(s) */
206  void freewheel_callback (bool);
207  void timebase_callback (TransportState state, pframes_t nframes, samplepos_t pos, int new_position);
210  void latency_callback (bool for_playback);
211  void halted_callback (const char* reason);
212 
213  /* checks if current thread is properly set up for audio processing */
215 
216  /* sets up the process callback thread */
217  static void thread_init_callback (void *);
218 
219  /* latency measurement */
220 
221  MTDM* mtdm() { return _mtdm; }
222  MIDIDM* mididm() { return _mididm; }
223 
227  void set_latency_input_port (const std::string&);
228  void set_latency_output_port (const std::string&);
229  uint32_t latency_signal_delay () const { return _latency_signal_latency; }
230 
234  MeasureMIDI
235  };
236 
237  LatencyMeasurement measuring_latency () const { return _measuring_latency; }
238 
239  /* These two are used only in builds where SILENCE_AFTER_SECONDS was
240  * set. BecameSilent will be emitted when the audioengine goes silent.
241  * reset_silence_countdown() can be used to reset the silence
242  * countdown, whose duration will be reduced to half of its previous
243  * value.
244  */
245 
246  PBD::Signal0<void> BecameSilent;
248 
250  void queue_latency_update (bool);
251 
252  enum TimingTypes {
253  ProcessCallback = 0,
254  /* end */
255  NTT = 1
256  };
257 
258  PBD::TimingStats dsp_stats[NTT];
259 
260  private:
262 
264 
265  Glib::Threads::Mutex _process_lock;
266  Glib::Threads::Mutex _latency_lock;
267  Glib::Threads::RecMutex _state_lock;
268  Glib::Threads::Cond session_removed;
274  bool _running;
289  std::string _latency_input_name;
290  std::string _latency_output_name;
295 
297 
299  std::atomic<int> _hw_reset_request_count;
300  Glib::Threads::Cond _hw_reset_condition;
301  Glib::Threads::Mutex _reset_request_lock;
302  std::atomic<int> _stop_hw_reset_processing;
304  std::atomic<int> _hw_devicelist_update_count;
305  Glib::Threads::Cond _hw_devicelist_update_condition;
306  Glib::Threads::Mutex _devicelist_update_lock;
308  uint32_t _start_cnt;
309  uint32_t _init_countdown;
312 
317 
318  typedef std::map<std::string,AudioBackendInfo*> BackendMap;
320  AudioBackendInfo* backend_discover (const std::string&);
321  void drop_backend ();
322 
323 #ifdef SILENCE_AFTER
324  samplecnt_t _silence_countdown;
325  uint32_t _silence_hit_cnt;
326 #endif
327 
328 };
329 
330 } // namespace ARDOUR
331 
332 #endif /* __ardour_audioengine_h__ */
virtual const char * what() const
Definition: audioengine.h:150
int usecs_per_cycle() const
static void destroy()
std::atomic< int > _stop_hw_reset_processing
Definition: audioengine.h:302
PBD::Signal0< void > Xrun
Definition: audioengine.h:165
PBD::Signal0< void > DeviceResetFinished
Definition: audioengine.h:195
void request_device_list_update()
void timebase_callback(TransportState state, pframes_t nframes, samplepos_t pos, int new_position)
int set_systemic_output_latency(uint32_t)
uint32_t latency_signal_delay() const
Definition: audioengine.h:229
void freewheel_callback(bool)
void halted_callback(const char *reason)
Glib::Threads::Cond session_removed
Definition: audioengine.h:268
sampleoffset_t session_removal_countdown
Definition: audioengine.h:270
std::string _last_backend_error_string
Definition: audioengine.h:296
samplepos_t sample_time_at_cycle_start()
std::string get_last_backend_error() const
Definition: audioengine.h:106
bool is_reset_requested() const
Definition: audioengine.h:119
static bool thread_initialised_for_audio_processing()
Session * session() const
Definition: audioengine.h:146
int request_buffer_size(pframes_t samples)
Definition: audioengine.h:138
Glib::Threads::Mutex & process_lock()
Definition: audioengine.h:135
float get_dsp_load() const
std::vector< const AudioBackendInfo * > available_backends() const
size_t raw_buffer_size(DataType t)
std::string backend_id(bool for_input)
int start_latency_detection(bool)
static AudioEngine * instance()
Definition: audioengine.h:197
int set_sample_rate(float)
std::shared_ptr< AudioBackend > current_backend() const
Definition: audioengine.h:76
uint32_t _init_countdown
Definition: audioengine.h:309
uint32_t process_thread_count()
static void thread_init_callback(void *)
static AudioEngine * _instance
Definition: audioengine.h:263
PBD::Signal1< void, const char * > Halted
Definition: audioengine.h:182
PortEngine::PortPtr _latency_input_port
Definition: audioengine.h:286
int port_registration_callback()
PortEngine::PortPtr _latency_output_port
Definition: audioengine.h:287
samplecnt_t _processed_samples
the number of samples processed since start() was called
Definition: audioengine.h:281
LatencyMeasurement measuring_latency() const
Definition: audioengine.h:237
int set_device_name(const std::string &)
bool get_sync_offset(pframes_t &offset) const
samplecnt_t monitor_check_interval
number of samples between each check for changes in monitor input
Definition: audioengine.h:277
virtual ~AudioEngine()
std::map< std::string, AudioBackendInfo * > BackendMap
Definition: audioengine.h:318
PBD::Signal0< void > BecameSilent
Definition: audioengine.h:246
void start_hw_event_processing()
TransportState transport_state()
bool is_jack() const
samplecnt_t _latency_flush_samples
Definition: audioengine.h:288
Glib::Threads::Mutex _devicelist_update_lock
Definition: audioengine.h:306
ProcessThread * main_thread() const
Definition: audioengine.h:80
void set_latency_output_port(const std::string &)
PBD::Signal1< void, pframes_t > BufferSizeChanged
Definition: audioengine.h:171
void split_cycle(pframes_t offset)
void launch_device_control_app()
BackendMap _backends
Definition: audioengine.h:319
PBD::Thread * _hw_devicelist_update_thread
Definition: audioengine.h:303
std::atomic< int > _hw_devicelist_update_count
Definition: audioengine.h:304
gain_t session_removal_gain_step
Definition: audioengine.h:273
int set_interleaved(bool yn)
void reset_silence_countdown()
std::string current_backend_name() const
samplepos_t sample_time()
bool freewheeling() const
Definition: audioengine.h:130
int set_buffer_size(uint32_t)
PBD::Signal0< void > Stopped
Definition: audioengine.h:189
int buffer_size_change(pframes_t nframes)
LatencyMeasurement _measuring_latency
Definition: audioengine.h:285
samplecnt_t last_monitor_check
time of the last monitor check in samples
Definition: audioengine.h:279
void latency_callback(bool for_playback)
samplepos_t transport_sample()
PBD::Signal1< void, pframes_t > Freewheel
Definition: audioengine.h:163
std::atomic< int > _pending_capture_latency_callback
Definition: audioengine.h:311
void queue_latency_update(bool)
samplecnt_t processed_samples() const
Definition: audioengine.h:142
int sample_rate_change(pframes_t nframes)
pframes_t samples_per_cycle() const
PBD::Signal0< void > DeviceError
Definition: audioengine.h:174
std::shared_ptr< AudioBackend > set_backend(const std::string &, const std::string &arg1, const std::string &arg2)
ProcessThread * _main_thread
Definition: audioengine.h:282
void transport_locate(samplepos_t pos)
void set_session(Session *)
Glib::Threads::RecMutex _state_lock
Definition: audioengine.h:267
PBD::Signal1< void, uint32_t > Running
Definition: audioengine.h:188
static AudioEngine * create()
std::string _latency_input_name
Definition: audioengine.h:289
int process_callback(pframes_t nframes)
int set_systemic_input_latency(uint32_t)
bool running() const
Definition: audioengine.h:131
Glib::Threads::Cond _hw_reset_condition
Definition: audioengine.h:300
std::string _latency_output_name
Definition: audioengine.h:290
PBD::Signal1< void, samplecnt_t > SampleRateChanged
Definition: audioengine.h:168
Glib::Threads::Mutex & latency_lock()
Definition: audioengine.h:136
pframes_t samples_since_cycle_start()
samplecnt_t sample_rate() const
Glib::Threads::Mutex _latency_lock
Definition: audioengine.h:266
void stop_hw_event_processing()
std::atomic< int > _stop_hw_devicelist_processing
Definition: audioengine.h:307
Glib::Threads::Cond _hw_devicelist_update_condition
Definition: audioengine.h:305
bool setup_required() const
Glib::Threads::Mutex _process_lock
Definition: audioengine.h:265
int create_process_thread(boost::function< void()> func)
int stop(bool for_latency_measurement=false)
PBD::Signal0< void > DeviceListChanged
Definition: audioengine.h:178
void set_latency_input_port(const std::string &)
gain_t session_removal_gain
Definition: audioengine.h:272
int sync_callback(TransportState state, samplepos_t position)
int freewheel(bool start_stop)
void request_backend_reset()
samplecnt_t _latency_signal_latency
Definition: audioengine.h:291
void add_pending_port_deletion(Port *)
PBD::Signal0< void > DeviceResetStarted
Definition: audioengine.h:194
AudioBackendInfo * backend_discover(const std::string &)
Glib::Threads::Mutex _reset_request_lock
Definition: audioengine.h:301
int prepare_for_latency_measurement()
PBD::Thread * _hw_reset_event_thread
Definition: audioengine.h:298
void stop_latency_detection()
int start(bool for_latency_measurement=false)
std::atomic< int > _pending_playback_latency_callback
Definition: audioengine.h:310
std::atomic< int > _hw_reset_request_count
Definition: audioengine.h:299
std::shared_ptr< ProtoPort > PortPtr
Definition: port_engine.h:108
Definition: mtdm.h:29
#define LIBARDOUR_API
uint32_t pframes_t
Temporal::samplecnt_t samplecnt_t
Temporal::sampleoffset_t sampleoffset_t
Temporal::samplepos_t samplepos_t