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