Ardour  9.2-129-gdf5e1050bd
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 #pragma once
24 
25 #ifdef WAF_BUILD
26 #include "libardour-config.h"
27 #endif
28 
29 #include <atomic>
30 #include <iostream>
31 #include <list>
32 #include <set>
33 #include <cmath>
34 #include <exception>
35 #include <string>
36 
37 #include "pbd/mutex.h"
38 #include "pbd/signals.h"
39 #include "pbd/pthread_utils.h"
40 
41 #include "ardour/ardour.h"
42 #include "ardour/data_type.h"
43 #include "ardour/session_handle.h"
45 #include "ardour/types.h"
46 #include "ardour/chan_count.h"
47 #include "ardour/port_manager.h"
48 
49 class MTDM;
50 
51 namespace ARDOUR {
52 
53 class InternalPort;
54 class MidiPort;
55 class MIDIDM;
56 class Port;
57 class Session;
58 class ProcessThread;
59 class AudioBackend;
60 struct AudioBackendInfo;
61 
63 {
64  public:
65 
66  static AudioEngine* create ();
67 
68  virtual ~AudioEngine ();
69 
71  std::vector<const AudioBackendInfo*> available_backends() const;
72  std::string current_backend_name () const;
73  std::shared_ptr<AudioBackend> set_backend (const std::string&, const std::string& arg1, const std::string& arg2);
74  std::shared_ptr<AudioBackend> current_backend() const { return _backend; }
75  bool setup_required () const;
76  bool is_jack () const;
77 
78  ProcessThread* main_thread() const { return _main_thread; }
79 
80  /* START BACKEND PROXY API
81  *
82  * See audio_backend.h for full documentation and semantics. These wrappers
83  * just forward to a backend implementation.
84  */
85 
86  int start (bool for_latency_measurement=false);
87  int stop (bool for_latency_measurement=false);
88  int freewheel (bool start_stop);
89  float get_dsp_load() const ;
90  void transport_start ();
91  void transport_stop ();
97  int usecs_per_cycle () const;
102  bool get_sync_offset (pframes_t& offset) const;
103 
104  std::string get_last_backend_error () const { return _last_backend_error_string; }
105 
106  int create_process_thread (std::function<void()> func);
109  uint32_t process_thread_count ();
110 
115 
116  // for the user which hold state_lock to check if reset operation is pending
117  bool is_reset_requested() const { return _hw_reset_request_count.load(); }
118 
119  int set_device_name (const std::string&);
120  int set_sample_rate (float);
121  int set_buffer_size (uint32_t);
122  int set_interleaved (bool yn);
125 
126  /* END BACKEND PROXY API */
127 
128  bool freewheeling() const { return _freewheeling; }
129  bool running() const { return _running; }
130 
131  std::string backend_id (bool for_input);
132 
133  PBD::Mutex& process_lock() { return _process_lock; }
134  PBD::Mutex& latency_lock() { return _latency_lock; }
135 
137  return set_buffer_size (samples);
138  }
139 
140  samplecnt_t processed_samples() const { return _processed_samples; }
141 
143  void remove_session (); // not a replacement for SessionHandle::session_going_away()
144  Session* session() const { return _session; }
145 
146  class NoBackendAvailable : public std::exception {
147  public:
148  virtual const char *what() const throw() { return "could not connect to engine backend"; }
149  };
150 
151  void split_cycle (pframes_t offset);
152 
154 
156 
157  /* this signal is sent for every process() cycle while freewheeling.
158  (the regular process() call to session->process() is not made)
159  */
160 
162 
163  PBD::Signal<void()> Xrun;
164 
167 
170 
173 
174  /* this signal is emitted if the device list changed */
175 
177 
178  /* this signal is sent if the backend ever disconnects us */
179 
180  PBD::Signal<void(const char*)> Halted;
181 
182  /* these two are emitted when the engine itself is
183  started and stopped
184  */
185 
186  PBD::Signal<void(uint32_t)> Running;
188 
189  /* these two are emitted when a device reset is initiated/finished
190  */
191 
194 
195  static AudioEngine* instance() { return _instance; }
196  static void destroy();
197 
198  void died ();
199 
200  /* The backend will cause these at the appropriate time(s) */
204  void freewheel_callback (bool);
205  void timebase_callback (TransportState state, pframes_t nframes, samplepos_t pos, int new_position);
208  void latency_callback (bool for_playback);
209  void halted_callback (const char* reason);
210 
211  /* checks if current thread is properly set up for audio processing */
213 
214  /* sets up the process callback thread */
215  static void thread_init_callback (void *);
216 
217  /* latency measurement */
218 
219  MTDM* mtdm() { return _mtdm; }
220  MIDIDM* mididm() { return _mididm; }
221 
225  void set_latency_input_port (const std::string&);
226  void set_latency_output_port (const std::string&);
227  uint32_t latency_signal_delay () const { return _latency_signal_latency; }
228 
232  MeasureMIDI
233  };
234 
235  LatencyMeasurement measuring_latency () const { return _measuring_latency; }
236 
237  /* These two are used only in builds where SILENCE_AFTER_SECONDS was
238  * set. BecameSilent will be emitted when the audioengine goes silent.
239  * reset_silence_countdown() can be used to reset the silence
240  * countdown, whose duration will be reduced to half of its previous
241  * value.
242  */
243 
246 
248  void queue_latency_update (bool);
249 
250  enum TimingTypes {
251  ProcessCallback = 0,
252  /* end */
253  NTT = 1
254  };
255 
256  PBD::TimingStats dsp_stats[NTT];
257 
258  private:
260 
262 
266  std::recursive_mutex _state_lock;
272  bool _running;
287  std::string _latency_input_name;
288  std::string _latency_output_name;
293 
296  std::atomic<int> _hw_reset_request_count;
299  std::atomic<int> _stop_hw_reset_processing;
301  std::atomic<int> _hw_devicelist_update_count;
305  uint32_t _start_cnt;
306  uint32_t _init_countdown;
309 
314 
315  typedef std::map<std::string,AudioBackendInfo*> BackendMap;
317  AudioBackendInfo* backend_discover (const std::string&);
318  void drop_backend ();
319 
320 #ifdef SILENCE_AFTER
321  samplecnt_t _silence_countdown;
322  uint32_t _silence_hit_cnt;
323 #endif
324 
325 };
326 
327 } // namespace ARDOUR
328 
virtual const char * what() const
Definition: audioengine.h:148
int usecs_per_cycle() const
static void destroy()
std::atomic< int > _stop_hw_reset_processing
Definition: audioengine.h:299
void request_device_list_update()
PBD::Signal< void(pframes_t)> Freewheel
Definition: audioengine.h:161
PBD::Signal< void(samplecnt_t)> SampleRateChanged
Definition: audioengine.h:166
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:227
void freewheel_callback(bool)
void halted_callback(const char *reason)
sampleoffset_t session_removal_countdown
Definition: audioengine.h:268
PBD::Mutex _devicelist_update_lock
Definition: audioengine.h:303
std::string _last_backend_error_string
Definition: audioengine.h:294
samplepos_t sample_time_at_cycle_start()
std::string get_last_backend_error() const
Definition: audioengine.h:104
PBD::Signal< void()> DeviceResetFinished
Definition: audioengine.h:193
bool is_reset_requested() const
Definition: audioengine.h:117
static bool thread_initialised_for_audio_processing()
Session * session() const
Definition: audioengine.h:144
PBD::Signal< void(uint32_t)> Running
Definition: audioengine.h:186
PBD::Signal< void()> DeviceResetStarted
Definition: audioengine.h:192
PBD::Cond _hw_devicelist_update_condition
Definition: audioengine.h:302
PBD::Signal< void(const char *)> Halted
Definition: audioengine.h:180
int request_buffer_size(pframes_t samples)
Definition: audioengine.h:136
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:195
int set_sample_rate(float)
std::shared_ptr< AudioBackend > current_backend() const
Definition: audioengine.h:74
uint32_t _init_countdown
Definition: audioengine.h:306
uint32_t process_thread_count()
static void thread_init_callback(void *)
static AudioEngine * _instance
Definition: audioengine.h:261
PortEngine::PortPtr _latency_input_port
Definition: audioengine.h:284
int port_registration_callback()
PortEngine::PortPtr _latency_output_port
Definition: audioengine.h:285
PBD::Signal< void()> Stopped
Definition: audioengine.h:187
samplecnt_t _processed_samples
the number of samples processed since start() was called
Definition: audioengine.h:279
LatencyMeasurement measuring_latency() const
Definition: audioengine.h:235
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:275
virtual ~AudioEngine()
PBD::Signal< void()> BecameSilent
Definition: audioengine.h:244
PBD::Signal< void()> DeviceError
Definition: audioengine.h:172
std::map< std::string, AudioBackendInfo * > BackendMap
Definition: audioengine.h:315
void start_hw_event_processing()
TransportState transport_state()
bool is_jack() const
samplecnt_t _latency_flush_samples
Definition: audioengine.h:286
ProcessThread * main_thread() const
Definition: audioengine.h:78
void set_latency_output_port(const std::string &)
void split_cycle(pframes_t offset)
int create_process_thread(std::function< void()> func)
void launch_device_control_app()
BackendMap _backends
Definition: audioengine.h:316
PBD::Thread * _hw_devicelist_update_thread
Definition: audioengine.h:300
std::atomic< int > _hw_devicelist_update_count
Definition: audioengine.h:301
gain_t session_removal_gain_step
Definition: audioengine.h:271
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:128
int set_buffer_size(uint32_t)
PBD::Mutex _reset_request_lock
Definition: audioengine.h:298
int buffer_size_change(pframes_t nframes)
LatencyMeasurement _measuring_latency
Definition: audioengine.h:283
samplecnt_t last_monitor_check
time of the last monitor check in samples
Definition: audioengine.h:277
void latency_callback(bool for_playback)
samplepos_t transport_sample()
std::atomic< int > _pending_capture_latency_callback
Definition: audioengine.h:308
void queue_latency_update(bool)
samplecnt_t processed_samples() const
Definition: audioengine.h:140
int sample_rate_change(pframes_t nframes)
pframes_t samples_per_cycle() const
std::shared_ptr< AudioBackend > set_backend(const std::string &, const std::string &arg1, const std::string &arg2)
ProcessThread * _main_thread
Definition: audioengine.h:280
void transport_locate(samplepos_t pos)
void set_session(Session *)
static AudioEngine * create()
std::string _latency_input_name
Definition: audioengine.h:287
int process_callback(pframes_t nframes)
int set_systemic_input_latency(uint32_t)
PBD::Signal< void()> DeviceListChanged
Definition: audioengine.h:176
bool running() const
Definition: audioengine.h:129
PBD::Cond _hw_reset_condition
Definition: audioengine.h:297
std::string _latency_output_name
Definition: audioengine.h:288
PBD::Signal< void()> Xrun
Definition: audioengine.h:163
pframes_t samples_since_cycle_start()
samplecnt_t sample_rate() const
PBD::Cond _session_removed
Definition: audioengine.h:265
std::recursive_mutex _state_lock
Definition: audioengine.h:266
void stop_hw_event_processing()
std::atomic< int > _stop_hw_devicelist_processing
Definition: audioengine.h:304
bool setup_required() const
PBD::Mutex _latency_lock
Definition: audioengine.h:264
PBD::Signal< void(pframes_t)> BufferSizeChanged
Definition: audioengine.h:169
int stop(bool for_latency_measurement=false)
void set_latency_input_port(const std::string &)
gain_t session_removal_gain
Definition: audioengine.h:270
PBD::Mutex _process_lock
Definition: audioengine.h:263
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:289
void add_pending_port_deletion(Port *)
AudioBackendInfo * backend_discover(const std::string &)
int prepare_for_latency_measurement()
PBD::Thread * _hw_reset_event_thread
Definition: audioengine.h:295
void stop_latency_detection()
PBD::Mutex & process_lock()
Definition: audioengine.h:133
int start(bool for_latency_measurement=false)
PBD::Mutex & latency_lock()
Definition: audioengine.h:134
std::atomic< int > _pending_playback_latency_callback
Definition: audioengine.h:307
std::atomic< int > _hw_reset_request_count
Definition: audioengine.h:296
std::shared_ptr< ProtoPort > PortPtr
Definition: port_engine.h:107
Definition: mtdm.h:28
#define LIBARDOUR_API
uint32_t pframes_t
Temporal::samplecnt_t samplecnt_t
Temporal::sampleoffset_t sampleoffset_t
Temporal::samplepos_t samplepos_t