Ardour  9.0-pre0-582-g084a23a80d
coreaudio_pcmio.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef __libbackend_coreaudio_pcmio_h__
20 #define __libbackend_coreaudio_pcmio_h__
21 
22 #include <CoreServices/CoreServices.h>
23 #include <CoreAudio/CoreAudio.h>
24 #include <AudioUnit/AudioUnit.h>
25 
26 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
27 # include <AudioToolbox/AudioToolbox.h>
28 #endif
29 
30 #include <AudioUnit/AudioUnit.h>
31 #include <AudioToolbox/AudioToolbox.h>
32 
33 #undef nil
34 
35 #include <map>
36 #include <vector>
37 #include <string>
38 
39 #define AUHAL_OUTPUT_ELEMENT 0
40 #define AUHAL_INPUT_ELEMENT 1
41 
42 
43 namespace PBD {
44 class Timing;
45 }
46 
47 namespace ARDOUR {
48 
49 class CoreAudioPCM {
50 public:
51  CoreAudioPCM (void);
52  ~CoreAudioPCM (void);
53 
54 
55  int state (void) const { return _state; }
56  uint32_t n_playback_channels (void) const { return _playback_channels; }
57  uint32_t n_capture_channels (void) const { return _capture_channels; }
58 
59  void discover();
60  void device_list (std::map<size_t, std::string> &devices) const { devices = _devices;}
61  void input_device_list (std::map<size_t, std::string> &devices) const { devices = _input_devices;}
62  void output_device_list (std::map<size_t, std::string> &devices) const { devices = _output_devices;}
63  void duplex_device_list (std::map<size_t, std::string> &devices) const { devices = _duplex_devices;}
64 
65  int available_sample_rates (uint32_t device_id, std::vector<float>& sampleRates);
66  int available_buffer_sizes (uint32_t device_id, std::vector<uint32_t>& sampleRates);
67  uint32_t available_channels (uint32_t device_id, bool input);
68  float current_sample_rate (uint32_t device_id, bool input = false);
69  uint32_t get_latency (uint32_t device_id, bool input);
70 
71  std::string cached_port_name (uint32_t portnum, bool input) const;
72 
73  float sample_rate ();
74  uint32_t samples_per_period () const { return _samples_per_period; };
75 
76  int set_samples_per_period (uint32_t);
77 
78  void launch_control_app (uint32_t device_id);
79 
80 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
81  bool workgroup (os_workgroup_t&);
82 #endif
83 
84  void pcm_stop (void);
85  int pcm_start (
86  uint32_t input_device,
87  uint32_t output_device,
88  uint32_t sample_rate,
89  uint32_t samples_per_period,
90  int (process_callback (void*, const uint32_t, const uint64_t)),
91  void * process_arg,
92  PBD::TimingStats& dsp_timer
93  );
94 
96  void ( error_callback (void*)),
97  void * error_arg
98  ) {
99  _error_callback = error_callback;
100  _error_arg = error_arg;
101  }
102 
104  void ( halted_callback (void*)),
105  void * halted_arg
106  ) {
108  _halted_arg = halted_arg;
109  }
110 
112  void ( callback (void*)),
113  void * arg
114  ) {
115  _hw_changed_callback = callback;
116  _hw_changed_arg = arg;
117  }
118 
120  void ( callback (void*)),
121  void * arg
122  ) {
123  _xrun_callback = callback;
124  _xrun_arg = arg;
125  }
127  void ( callback (void*)),
128  void * arg
129  ) {
130  _buffer_size_callback = callback;
131  _buffer_size_arg = arg;
132  }
134  void ( callback (void*)),
135  void * arg
136  ) {
137  _sample_rate_callback = callback;
138  _sample_rate_arg = arg;
139  }
140 
141  // must be called from process_callback;
142  int get_capture_channel (uint32_t chn, float *input, uint32_t n_samples);
143  int set_playback_channel (uint32_t chn, const float *input, uint32_t n_samples);
144  uint32_t n_samples() const { return _cur_samples_per_period; };
145 
146  // really private
147  OSStatus render_callback (
148  AudioUnitRenderActionFlags* ioActionFlags,
149  const AudioTimeStamp* inTimeStamp,
150  UInt32 inBusNumber,
151  UInt32 inNumberSamples,
152  AudioBufferList* ioData);
153 
154  void xrun_callback ();
159 
160 private:
161  float current_sample_rate_id (AudioDeviceID id, bool input);
162  uint32_t current_buffer_size_id (AudioDeviceID id);
163  int set_device_sample_rate_id (AudioDeviceID id, float rate, bool input);
164  int set_device_buffer_size_id (AudioDeviceID id, uint32_t samples_per_period);
165  int set_device_sample_rate (uint32_t device_id, float rate, bool input);
166  void get_stream_latencies (uint32_t device_id, bool input, std::vector<uint32_t>& latencies);
167  void cache_port_names (AudioDeviceID id, bool input);
168 
171  AudioDeviceID input_device_id,
172  AudioDeviceID output_device_id,
173  uint32_t sample_rate,
174  AudioDeviceID *created_device);
175 
176 
178  AudioDeviceID* _device_ids;
179  AudioBufferList* _input_audio_buffer_list;
180  AudioBufferList* _output_audio_buffer_list;
181 
182  AudioDeviceID _active_device_id;
183  AudioDeviceID _aggregate_device_id;
184  AudioDeviceID _aggregate_plugin_id;
185 
186  int _state;
187 
193  size_t _n_devices;
194 
195  int (* _process_callback) (void*, const uint32_t, const uint64_t);
196  void * _process_arg;
197 
199 
200  void (* _error_callback) (void*);
201  void * _error_arg;
202 
203  void (* _halted_callback) (void*);
204  void * _halted_arg;
205 
206  void (* _hw_changed_callback) (void*);
208 
209  void (* _xrun_callback) (void*);
210  void * _xrun_arg;
211 
212  void (* _buffer_size_callback) (void*);
214 
215  void (* _sample_rate_callback) (void*);
217 
218 
219  // TODO proper device info struct
220  std::map<size_t, std::string> _devices;
221  std::map<size_t, std::string> _input_devices;
222  std::map<size_t, std::string> _output_devices;
223  std::map<size_t, std::string> _duplex_devices;
224  uint32_t * _device_ins;
225  uint32_t * _device_outs;
226  std::vector<std::string> _input_names;
227  std::vector<std::string> _output_names;
228 
229  pthread_mutex_t _discovery_lock;
230 };
231 
232 } // namespace
233 
234 #endif /* __libbackend_coreaudio_pcmio_h__ */
std::string cached_port_name(uint32_t portnum, bool input) const
uint32_t n_capture_channels(void) const
void(* _error_callback)(void *)
std::map< size_t, std::string > _devices
pthread_mutex_t _discovery_lock
void set_hw_changed_callback(void(callback(void *)), void *arg)
uint32_t n_samples() const
uint32_t available_channels(uint32_t device_id, bool input)
uint32_t samples_per_period() const
void destroy_aggregate_device()
void cache_port_names(AudioDeviceID id, bool input)
void set_xrun_callback(void(callback(void *)), void *arg)
int state(void) const
std::map< size_t, std::string > _input_devices
void set_buffer_size_callback(void(callback(void *)), void *arg)
int pcm_start(uint32_t input_device, uint32_t output_device, uint32_t sample_rate, uint32_t samples_per_period, int(process_callback(void *, const uint32_t, const uint64_t)), void *process_arg, PBD::TimingStats &dsp_timer)
uint32_t get_latency(uint32_t device_id, bool input)
uint32_t _cur_samples_per_period
std::vector< std::string > _input_names
void set_sample_rate_callback(void(callback(void *)), void *arg)
void duplex_device_list(std::map< size_t, std::string > &devices) const
void launch_control_app(uint32_t device_id)
int get_capture_channel(uint32_t chn, float *input, uint32_t n_samples)
OSStatus render_callback(AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberSamples, AudioBufferList *ioData)
AudioDeviceID _aggregate_plugin_id
AudioDeviceID * _device_ids
void output_device_list(std::map< size_t, std::string > &devices) const
void(* _sample_rate_callback)(void *)
AudioDeviceID _aggregate_device_id
int(* _process_callback)(void *, const uint32_t, const uint64_t)
void input_device_list(std::map< size_t, std::string > &devices) const
int set_device_sample_rate_id(AudioDeviceID id, float rate, bool input)
std::vector< std::string > _output_names
void(* _buffer_size_callback)(void *)
std::map< size_t, std::string > _output_devices
void(* _xrun_callback)(void *)
void set_error_callback(void(error_callback(void *)), void *error_arg)
AudioBufferList * _input_audio_buffer_list
AudioBufferList * _output_audio_buffer_list
void get_stream_latencies(uint32_t device_id, bool input, std::vector< uint32_t > &latencies)
uint32_t n_playback_channels(void) const
PBD::TimingStats * _dsp_timer
void set_halted_callback(void(halted_callback(void *)), void *halted_arg)
int create_aggregate_device(AudioDeviceID input_device_id, AudioDeviceID output_device_id, uint32_t sample_rate, AudioDeviceID *created_device)
int set_playback_channel(uint32_t chn, const float *input, uint32_t n_samples)
int set_device_sample_rate(uint32_t device_id, float rate, bool input)
void device_list(std::map< size_t, std::string > &devices) const
void(* _halted_callback)(void *)
int available_buffer_sizes(uint32_t device_id, std::vector< uint32_t > &sampleRates)
std::map< size_t, std::string > _duplex_devices
int set_samples_per_period(uint32_t)
AudioDeviceID _active_device_id
float current_sample_rate(uint32_t device_id, bool input=false)
int available_sample_rates(uint32_t device_id, std::vector< float > &sampleRates)
uint32_t current_buffer_size_id(AudioDeviceID id)
float current_sample_rate_id(AudioDeviceID id, bool input)
void(* _hw_changed_callback)(void *)
int set_device_buffer_size_id(AudioDeviceID id, uint32_t samples_per_period)
@ AudioUnit
Definition: plugin_types.h:24
DebugBits Timing
Definition: axis_view.h:42