Ardour  9.0-pre0-582-g084a23a80d
portaudio_backend.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2016 Tim Mayberry <mojofunk@gmail.com>
3  * Copyright (C) 2015-2018 Robin Gareus <robin@gareus.org>
4  * Copyright (C) 2017-2018 Paul Davis <paul@linuxaudiosystems.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef __libbackend_portaudio_backend_h__
22 #define __libbackend_portaudio_backend_h__
23 
24 #include <cstdint>
25 #include <memory>
26 #include <string>
27 #include <vector>
28 #include <set>
29 
30 #include <pthread.h>
31 
32 #include "ardour/audio_backend.h"
35 #include "ardour/types.h"
36 
37 #include "portaudio_io.h"
38 #include "winmmemidi_io.h"
39 #include "cycle_timer.h"
40 
41 namespace ARDOUR {
42 
43 class PortAudioBackend;
44 
46  public:
47  PortMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size);
48  PortMidiEvent (const PortMidiEvent& other);
49  size_t size () const { return _size; };
50  pframes_t timestamp () const { return _timestamp; };
51  const uint8_t* data () const { return _data; };
52  private:
53  size_t _size;
56 };
57 
58 typedef std::vector<PortMidiEvent> PortMidiBuffer;
59 
60 class PortAudioPort : public BackendPort {
61  public:
62  PortAudioPort (PortAudioBackend &b, const std::string&, PortFlags);
64 
65  DataType type () const { return DataType::AUDIO; };
66 
67  Sample* buffer () { return _buffer; }
68  const Sample* const_buffer () const { return _buffer; }
69  void* get_buffer (pframes_t nframes);
70 
71  private:
72  Sample _buffer[8192];
73 }; // class PortAudioPort
74 
75 class PortMidiPort : public BackendPort {
76  public:
77  PortMidiPort (PortAudioBackend &b, const std::string&, PortFlags);
79 
80  DataType type () const { return DataType::MIDI; };
81 
82  void* get_buffer (pframes_t nframes);
83  const PortMidiBuffer * const_buffer () const { return & _buffer[_bufperiod]; }
84 
85  void next_period() { if (_n_periods > 1) { get_buffer(0); _bufperiod = (_bufperiod + 1) % _n_periods; } }
86  void set_n_periods(int n) { if (n > 0 && n < 3) { _n_periods = n; } }
87 
88  private:
92 }; // class PortMidiPort
93 
95  public:
98 
99  /* AUDIOBACKEND API */
100 
101  std::string name () const;
102  bool is_realtime () const;
103 
105  std::string driver_name () const;
106  std::vector<std::string> enumerate_drivers () const;
107  int set_driver (const std::string&);
108 
109  bool can_request_update_devices () { return true; }
110  bool update_devices ();
111 
112  bool can_use_buffered_io () { return true; }
113  void set_use_buffered_io (bool);
115 
117  std::vector<DeviceStatus> enumerate_devices () const;
118  std::vector<DeviceStatus> enumerate_input_devices () const;
119  std::vector<DeviceStatus> enumerate_output_devices () const;
120 
121  std::vector<float> available_sample_rates (const std::string& device) const;
122  std::vector<uint32_t> available_buffer_sizes (const std::string& device) const;
123 
126 
127  int set_device_name (const std::string&);
128  int set_input_device_name (const std::string&);
129  int set_output_device_name (const std::string&);
130  int set_sample_rate (float);
131  int set_buffer_size (uint32_t);
132  int set_interleaved (bool yn);
135  int set_systemic_midi_input_latency (std::string const, uint32_t);
136  int set_systemic_midi_output_latency (std::string const, uint32_t);
137 
138  int reset_device () { return 0; };
139 
140  /* Retrieving parameters */
141  std::string device_name () const;
142  std::string input_device_name () const;
143  std::string output_device_name () const;
144  float sample_rate () const;
145  uint32_t buffer_size () const;
146  bool interleaved () const;
147  uint32_t systemic_input_latency () const;
148  uint32_t systemic_output_latency () const;
149  uint32_t systemic_midi_input_latency (std::string const) const;
150  uint32_t systemic_midi_output_latency (std::string const) const;
151 
152  bool can_measure_systemic_latency () const { return true; }
153  bool can_set_systemic_midi_latencies () const { return true; }
154 
155  /* External control app */
156  std::string control_app_name () const;
158 
159  /* MIDI */
160  std::vector<std::string> enumerate_midi_options () const;
161  int set_midi_option (const std::string&);
162  std::string midi_option () const;
163 
164  std::vector<DeviceStatus> enumerate_midi_devices () const;
165  int set_midi_device_enabled (std::string const, bool);
166  bool midi_device_enabled (std::string const) const;
167 
168  protected:
169  /* State Control */
170  int _start (bool for_latency_measurement);
171  public:
172  int stop ();
173  int freewheel (bool);
174  float dsp_load () const;
176 
177  /* Process time */
181 
182  int create_process_thread (std::function<void()> func);
185  uint32_t process_thread_count ();
186 
188 
189  /* PORTENGINE API */
190 
191  void* private_handle () const;
192  const std::string& my_name () const;
193 
194 
195  /* PortEngine API - forwarded to PortEngineSharedImpl */
196 
198  void get_physical_outputs (DataType type, std::vector<std::string>& results) { PortEngineSharedImpl::get_physical_outputs (type, results); }
199  void get_physical_inputs (DataType type, std::vector<std::string>& results) { PortEngineSharedImpl::get_physical_inputs (type, results); }
202  uint32_t port_name_size () const { return PortEngineSharedImpl::port_name_size(); }
207  int get_port_property (PortEngine::PortHandle ph, const std::string& key, std::string& value, std::string& type) const { return PortEngineSharedImpl::get_port_property (ph, key, value, type); }
208  int set_port_property (PortEngine::PortHandle ph, const std::string& key, const std::string& value, const std::string& type) { return PortEngineSharedImpl::set_port_property (ph, key, value, type); }
209  int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>& results) const { return PortEngineSharedImpl::get_ports (port_name_pattern, type, flags, results); }
211  PortEngine::PortPtr register_port (const std::string& shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags) { return PortEngineSharedImpl::register_port (shortname, type, flags); }
213  int connect (const std::string& src, const std::string& dst) { return PortEngineSharedImpl::connect (src, dst); }
214  int disconnect (const std::string& src, const std::string& dst) { return PortEngineSharedImpl::disconnect (src, dst); }
215  int connect (PortEngine::PortHandle ph, const std::string& other) { return PortEngineSharedImpl::connect (ph, other); }
216  int disconnect (PortEngine::PortHandle ph, const std::string& other) { return PortEngineSharedImpl::disconnect (ph, other); }
218  bool connected (PortEngine::PortHandle ph, bool process_callback_safe) { return PortEngineSharedImpl::connected (ph, process_callback_safe); }
219  bool connected_to (PortEngine::PortHandle ph, const std::string& other, bool process_callback_safe) { return PortEngineSharedImpl::connected_to (ph, other, process_callback_safe); }
220  bool physically_connected (PortEngine::PortHandle ph, bool process_callback_safe) { return PortEngineSharedImpl::physically_connected (ph, process_callback_safe); }
221  int get_connections (PortEngine::PortHandle ph, std::vector<std::string>& results, bool process_callback_safe) { return PortEngineSharedImpl::get_connections (ph, results, process_callback_safe); }
222 
223  /* MIDI */
224  int midi_event_get (pframes_t& timestamp, size_t& size, uint8_t const** buf, void* port_buffer, uint32_t event_index);
225  int midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size);
226  uint32_t get_midi_event_count (void* port_buffer);
227  void midi_clear (void* port_buffer);
228 
229  /* Monitoring */
230 
231  bool can_monitor_input () const;
235 
236  /* Latency management */
237 
238  void set_latency_range (PortHandle, bool for_playback, LatencyRange);
240 
241  /* Getting access to the data buffer for a port */
242 
244 
246 
248 
249  private: // Methods
253  bool blocking_process_main (const float* interleaved_input_data,
254  float* interleaved_output_data);
255 
259 
260  bool engine_halted ();
261  bool running ();
262 
263  static int portaudio_callback(const void* input,
264  void* output,
265  unsigned long frameCount,
266  const PaStreamCallbackTimeInfo* timeInfo,
267  PaStreamCallbackFlags statusFlags,
268  void* userData);
269 
270  bool process_callback(const float* input,
271  float* output,
272  uint32_t sample_count,
273  const PaStreamCallbackTimeInfo* timeInfo,
274  PaStreamCallbackFlags statusFlags);
275 
278 
279  static bool set_mmcss_pro_audio (HANDLE* task_handle);
280  static bool reset_mmcss (HANDLE task_handle);
281 
282  private:
283  std::string _instance_name;
286 
287  bool _run; /* keep going or stop, ardour thread */
288  bool _active; /* is running, process thread */
296 
298 
300 
301  pthread_mutex_t _freewheel_mutex;
302  pthread_cond_t _freewheel_signal;
303 
304  uint64_t _cycle_count;
307 
310 
311  static std::vector<std::string> _midi_options;
312  static std::vector<AudioBackend::DeviceStatus> _input_audio_device_status;
313  static std::vector<AudioBackend::DeviceStatus> _output_audio_device_status;
314  static std::vector<AudioBackend::DeviceStatus> _midi_device_status;
315 
316  mutable std::string _input_audio_device;
317  mutable std::string _output_audio_device;
318  std::string _midi_driver_option;
319 
320  /* audio settings */
321  float _samplerate;
323  static size_t _max_buffer_size;
324 
327 
328  MidiDeviceInfo* midi_device_info(const std::string&) const;
329 
330  /* portaudio specific */
331  int name_to_id(std::string) const;
332 
333  /* processing */
334  float _dsp_load;
336 
337  /* blocking thread */
339 
340  /* main thread in callback mode(or fw thread when running) */
341  pthread_t _main_thread;
342 
343  /* freewheel thread in callback mode */
345 
346  /* process threads */
347  static void* portaudio_process_thread (void *);
348  std::vector<pthread_t> _threads;
349 
350  struct ThreadData {
352  std::function<void ()> f;
353  size_t stacksize;
354 
355  ThreadData (PortAudioBackend* e, std::function<void ()> fp, size_t stacksz)
356  : engine (e) , f (fp) , stacksize (stacksz) {}
357  };
358 
359  /* port engine */
361 
363  int register_system_midi_ports (std::string const& device = "");
364 
366 
367 }; // class PortAudioBackend
368 
369 } // namespace
370 
371 #endif /* __libbackend_portaudio_backend_h__ */
AudioBackendInfo & info() const
std::vector< pthread_t > _threads
ChanCount n_physical_outputs() const
PortEngine::PortPtr get_port_by_name(std::string const &name) const
void * private_handle() const
bool midi_device_enabled(std::string const) const
LatencyRange get_latency_range(PortHandle, bool for_playback)
std::vector< DeviceStatus > enumerate_devices() const
std::string name() const
static bool set_mmcss_pro_audio(HANDLE *task_handle)
int midi_event_get(pframes_t &timestamp, size_t &size, uint8_t const **buf, void *port_buffer, uint32_t event_index)
static std::vector< std::string > _midi_options
bool port_is_physical(PortEngine::PortHandle ph) const
uint32_t systemic_output_latency() const
std::string device_name() const
int connect(PortEngine::PortHandle ph, const std::string &other)
uint32_t get_midi_event_count(void *port_buffer)
int disconnect(PortEngine::PortHandle ph, const std::string &other)
std::vector< std::string > enumerate_midi_options() const
void process_port_connection_changes()
static std::vector< AudioBackend::DeviceStatus > _output_audio_device_status
bool can_set_systemic_midi_latencies() const
bool use_separate_input_and_output_devices() const
MidiDeviceInfo * midi_device_info(const std::string &) const
PortFlags get_port_flags(PortEngine::PortHandle ph) const
int create_process_thread(std::function< void()> func)
std::vector< DeviceStatus > enumerate_midi_devices() const
BackendPort * port_factory(std::string const &name, ARDOUR::DataType dt, ARDOUR::PortFlags flags)
std::vector< float > available_sample_rates(const std::string &device) const
bool connected(PortEngine::PortHandle ph, bool process_callback_safe)
int ensure_input_monitoring(PortHandle, bool)
std::vector< DeviceStatus > enumerate_input_devices() const
int name_to_id(std::string) const
int set_device_name(const std::string &)
int disconnect(const std::string &src, const std::string &dst)
int disconnect_all(PortEngine::PortHandle ph)
int register_system_midi_ports(std::string const &device="")
uint32_t systemic_input_latency() const
pframes_t samples_since_cycle_start()
std::string get_port_name(PortEngine::PortHandle ph) const
pthread_cond_t _freewheel_signal
ChanCount n_physical_inputs() const
bool blocking_process_main(const float *interleaved_input_data, float *interleaved_output_data)
static std::vector< AudioBackend::DeviceStatus > _input_audio_device_status
bool physically_connected(PortEngine::PortHandle ph, bool process_callback_safe)
void get_physical_outputs(DataType type, std::vector< std::string > &results)
static int portaudio_callback(const void *input, void *output, unsigned long frameCount, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
int set_systemic_midi_input_latency(std::string const, uint32_t)
int set_systemic_input_latency(uint32_t)
std::vector< uint32_t > available_buffer_sizes(const std::string &device) const
ARDOUR::DSPLoadCalculator _dsp_calc
PortAudioBackend(AudioEngine &e, AudioBackendInfo &info)
uint32_t buffer_size() const
std::string driver_name() const
int set_port_name(PortEngine::PortHandle ph, const std::string &name)
bool can_change_sample_rate_when_running() const
int set_buffer_size(uint32_t)
uint32_t port_name_size() const
std::string output_device_name() const
bool connected_to(PortEngine::PortHandle ph, const std::string &other, bool process_callback_safe)
int get_port_property(PortEngine::PortHandle ph, const std::string &key, std::string &value, std::string &type) const
bool process_callback(const float *input, float *output, uint32_t sample_count, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags)
int get_ports(const std::string &port_name_pattern, DataType type, PortFlags flags, std::vector< std::string > &results) const
int request_input_monitoring(PortHandle, bool)
static std::vector< AudioBackend::DeviceStatus > _midi_device_status
void set_latency_range(PortHandle, bool for_playback, LatencyRange)
int set_midi_option(const std::string &)
void * get_buffer(PortHandle, pframes_t)
std::string input_device_name() const
void set_use_buffered_io(bool)
uint32_t systemic_midi_output_latency(std::string const) const
uint32_t process_thread_count()
int set_interleaved(bool yn)
std::vector< std::string > enumerate_drivers() const
static bool reset_mmcss(HANDLE task_handle)
int get_connections(PortEngine::PortHandle ph, std::vector< std::string > &results, bool process_callback_safe)
int set_systemic_output_latency(uint32_t)
bool requires_driver_selection() const
int set_output_device_name(const std::string &)
size_t raw_buffer_size(DataType t)
void midi_clear(void *port_buffer)
uint32_t systemic_midi_input_latency(std::string const) const
samplepos_t sample_time_at_cycle_start()
bool monitoring_input(PortHandle)
PortEngine::PortPtr register_port(const std::string &shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags)
std::string control_app_name() const
bool can_measure_systemic_latency() const
int set_midi_device_enabled(std::string const, bool)
static void * portaudio_process_thread(void *)
int midi_event_put(void *port_buffer, pframes_t timestamp, const uint8_t *buffer, size_t size)
void unregister_port(PortHandle ph)
void get_physical_inputs(DataType type, std::vector< std::string > &results)
const std::string & my_name() const
bool can_change_buffer_size_when_running() const
std::string midi_option() const
int set_systemic_midi_output_latency(std::string const, uint32_t)
bool can_monitor_input() const
int _start(bool for_latency_measurement)
samplepos_t sample_time()
int set_driver(const std::string &)
int set_port_property(PortEngine::PortHandle ph, const std::string &key, const std::string &value, const std::string &type)
DataType port_data_type(PortEngine::PortHandle ph) const
pthread_mutex_t _freewheel_mutex
std::vector< DeviceStatus > enumerate_output_devices() const
int connect(const std::string &src, const std::string &dst)
int set_input_device_name(const std::string &)
float sample_rate() const
DataType type() const
const Sample * const_buffer() const
PortAudioPort(PortAudioBackend &b, const std::string &, PortFlags)
void * get_buffer(pframes_t nframes)
int set_port_name(PortEngine::PortHandle, const std::string &)
uint32_t port_name_size() const
virtual void unregister_port(PortEngine::PortHandle)
bool physically_connected(PortEngine::PortHandle, bool process_callback_safe)
int disconnect(const std::string &src, const std::string &dst)
PortEngine::PortPtr get_port_by_name(const std::string &) const
int get_ports(const std::string &port_name_pattern, DataType type, PortFlags flags, std::vector< std::string > &) const
int connect(const std::string &src, const std::string &dst)
ChanCount n_physical_outputs() const
PortFlags get_port_flags(PortEngine::PortHandle) const
bool connected_to(PortEngine::PortHandle, const std::string &, bool process_callback_safe)
bool connected(PortEngine::PortHandle, bool process_callback_safe)
int get_connections(PortEngine::PortHandle, std::vector< std::string > &, bool process_callback_safe)
std::string get_port_name(PortEngine::PortHandle) const
ChanCount n_physical_inputs() const
void get_physical_outputs(DataType type, std::vector< std::string > &)
void get_physical_inputs(DataType type, std::vector< std::string > &)
DataType port_data_type(PortEngine::PortHandle) const
int disconnect_all(PortEngine::PortHandle)
bool port_is_physical(PortEngine::PortHandle) const
int get_port_property(PortEngine::PortHandle, const std::string &key, std::string &value, std::string &type) const
int set_port_property(PortEngine::PortHandle, const std::string &key, const std::string &value, const std::string &type)
PortEngine::PortPtr register_port(const std::string &shortname, ARDOUR::DataType, ARDOUR::PortFlags)
PortPtr const & PortHandle
Definition: port_engine.h:113
std::shared_ptr< ProtoPort > PortPtr
Definition: port_engine.h:107
PortMidiEvent(const PortMidiEvent &other)
const uint8_t * data() const
PortMidiEvent(const pframes_t timestamp, const uint8_t *data, size_t size)
pframes_t timestamp() const
PortMidiBuffer _buffer[2]
PortMidiPort(PortAudioBackend &b, const std::string &, PortFlags)
const PortMidiBuffer * const_buffer() const
DataType type() const
void * get_buffer(pframes_t nframes)
std::vector< PortMidiEvent > PortMidiBuffer
uint32_t pframes_t
Temporal::samplecnt_t samplecnt_t
Temporal::samplepos_t samplepos_t
ThreadData(PortAudioBackend *e, std::function< void()> fp, size_t stacksz)
#define MaxWinMidiEventSize