| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  audio_driver_coreaudio.cpp                                            */ | 
|---|
| 3 | /**************************************************************************/ | 
|---|
| 4 | /*                         This file is part of:                          */ | 
|---|
| 5 | /*                             GODOT ENGINE                               */ | 
|---|
| 6 | /*                        https://godotengine.org                         */ | 
|---|
| 7 | /**************************************************************************/ | 
|---|
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
|---|
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
|---|
| 10 | /*                                                                        */ | 
|---|
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
|---|
| 12 | /* a copy of this software and associated documentation files (the        */ | 
|---|
| 13 | /* "Software"), to deal in the Software without restriction, including    */ | 
|---|
| 14 | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
|---|
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
|---|
| 16 | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
|---|
| 17 | /* the following conditions:                                              */ | 
|---|
| 18 | /*                                                                        */ | 
|---|
| 19 | /* The above copyright notice and this permission notice shall be         */ | 
|---|
| 20 | /* included in all copies or substantial portions of the Software.        */ | 
|---|
| 21 | /*                                                                        */ | 
|---|
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
|---|
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
|---|
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
|---|
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
|---|
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
|---|
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
|---|
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
|---|
| 29 | /**************************************************************************/ | 
|---|
| 30 |  | 
|---|
| 31 | #include "audio_driver_coreaudio.h" | 
|---|
| 32 |  | 
|---|
| 33 | #ifdef COREAUDIO_ENABLED | 
|---|
| 34 |  | 
|---|
| 35 | #include "core/config/project_settings.h" | 
|---|
| 36 | #include "core/os/os.h" | 
|---|
| 37 |  | 
|---|
| 38 | #define kOutputBus 0 | 
|---|
| 39 | #define kInputBus 1 | 
|---|
| 40 |  | 
|---|
| 41 | #ifdef MACOS_ENABLED | 
|---|
| 42 | OSStatus AudioDriverCoreAudio::input_device_address_cb(AudioObjectID inObjectID, | 
|---|
| 43 | UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, | 
|---|
| 44 | void *inClientData) { | 
|---|
| 45 | AudioDriverCoreAudio *driver = static_cast<AudioDriverCoreAudio *>(inClientData); | 
|---|
| 46 |  | 
|---|
| 47 | // If our selected input device is the Default, call set_input_device to update the | 
|---|
| 48 | // kAudioOutputUnitProperty_CurrentDevice property | 
|---|
| 49 | if (driver->input_device_name == "Default") { | 
|---|
| 50 | driver->set_input_device( "Default"); | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | return noErr; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | OSStatus AudioDriverCoreAudio::output_device_address_cb(AudioObjectID inObjectID, | 
|---|
| 57 | UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, | 
|---|
| 58 | void *inClientData) { | 
|---|
| 59 | AudioDriverCoreAudio *driver = static_cast<AudioDriverCoreAudio *>(inClientData); | 
|---|
| 60 |  | 
|---|
| 61 | // If our selected output device is the Default call set_output_device to update the | 
|---|
| 62 | // kAudioOutputUnitProperty_CurrentDevice property | 
|---|
| 63 | if (driver->output_device_name == "Default") { | 
|---|
| 64 | driver->set_output_device( "Default"); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | return noErr; | 
|---|
| 68 | } | 
|---|
| 69 | #endif | 
|---|
| 70 |  | 
|---|
| 71 | Error AudioDriverCoreAudio::init() { | 
|---|
| 72 | AudioComponentDescription desc; | 
|---|
| 73 | memset(&desc, 0, sizeof(desc)); | 
|---|
| 74 | desc.componentType = kAudioUnitType_Output; | 
|---|
| 75 | #ifdef MACOS_ENABLED | 
|---|
| 76 | desc.componentSubType = kAudioUnitSubType_HALOutput; | 
|---|
| 77 | #else | 
|---|
| 78 | desc.componentSubType = kAudioUnitSubType_RemoteIO; | 
|---|
| 79 | #endif | 
|---|
| 80 | desc.componentManufacturer = kAudioUnitManufacturer_Apple; | 
|---|
| 81 |  | 
|---|
| 82 | AudioComponent comp = AudioComponentFindNext(nullptr, &desc); | 
|---|
| 83 | ERR_FAIL_NULL_V(comp, FAILED); | 
|---|
| 84 |  | 
|---|
| 85 | OSStatus result = AudioComponentInstanceNew(comp, &audio_unit); | 
|---|
| 86 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 87 |  | 
|---|
| 88 | #ifdef MACOS_ENABLED | 
|---|
| 89 | AudioObjectPropertyAddress prop; | 
|---|
| 90 | prop.mSelector = kAudioHardwarePropertyDefaultOutputDevice; | 
|---|
| 91 | prop.mScope = kAudioObjectPropertyScopeGlobal; | 
|---|
| 92 | prop.mElement = kAudioObjectPropertyElementMaster; | 
|---|
| 93 |  | 
|---|
| 94 | result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this); | 
|---|
| 95 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 96 | #endif | 
|---|
| 97 |  | 
|---|
| 98 | AudioStreamBasicDescription strdesc; | 
|---|
| 99 |  | 
|---|
| 100 | memset(&strdesc, 0, sizeof(strdesc)); | 
|---|
| 101 | UInt32 size = sizeof(strdesc); | 
|---|
| 102 | result = AudioUnitGetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kOutputBus, &strdesc, &size); | 
|---|
| 103 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 104 |  | 
|---|
| 105 | switch (strdesc.mChannelsPerFrame) { | 
|---|
| 106 | case 2: // Stereo | 
|---|
| 107 | case 4: // Surround 3.1 | 
|---|
| 108 | case 6: // Surround 5.1 | 
|---|
| 109 | case 8: // Surround 7.1 | 
|---|
| 110 | channels = strdesc.mChannelsPerFrame; | 
|---|
| 111 | break; | 
|---|
| 112 |  | 
|---|
| 113 | default: | 
|---|
| 114 | // Unknown number of channels, default to stereo | 
|---|
| 115 | channels = 2; | 
|---|
| 116 | break; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | mix_rate = _get_configured_mix_rate(); | 
|---|
| 120 |  | 
|---|
| 121 | memset(&strdesc, 0, sizeof(strdesc)); | 
|---|
| 122 | strdesc.mFormatID = kAudioFormatLinearPCM; | 
|---|
| 123 | strdesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; | 
|---|
| 124 | strdesc.mChannelsPerFrame = channels; | 
|---|
| 125 | strdesc.mSampleRate = mix_rate; | 
|---|
| 126 | strdesc.mFramesPerPacket = 1; | 
|---|
| 127 | strdesc.mBitsPerChannel = 16; | 
|---|
| 128 | strdesc.mBytesPerFrame = strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8; | 
|---|
| 129 | strdesc.mBytesPerPacket = strdesc.mBytesPerFrame * strdesc.mFramesPerPacket; | 
|---|
| 130 |  | 
|---|
| 131 | result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc)); | 
|---|
| 132 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 133 |  | 
|---|
| 134 | int latency = Engine::get_singleton()->get_audio_output_latency(); | 
|---|
| 135 | // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels) | 
|---|
| 136 | buffer_frames = closest_power_of_2(latency * mix_rate / 1000); | 
|---|
| 137 |  | 
|---|
| 138 | #ifdef MACOS_ENABLED | 
|---|
| 139 | result = AudioUnitSetProperty(audio_unit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, kOutputBus, &buffer_frames, sizeof(UInt32)); | 
|---|
| 140 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 141 | #endif | 
|---|
| 142 |  | 
|---|
| 143 | unsigned int buffer_size = buffer_frames * channels; | 
|---|
| 144 | samples_in.resize(buffer_size); | 
|---|
| 145 | input_buf.resize(buffer_size); | 
|---|
| 146 |  | 
|---|
| 147 | print_verbose( "CoreAudio: detected "+ itos(channels) + " channels"); | 
|---|
| 148 | print_verbose( "CoreAudio: audio buffer frames: "+ itos(buffer_frames) + " calculated latency: "+ itos(buffer_frames * 1000 / mix_rate) + "ms"); | 
|---|
| 149 |  | 
|---|
| 150 | AURenderCallbackStruct callback; | 
|---|
| 151 | memset(&callback, 0, sizeof(AURenderCallbackStruct)); | 
|---|
| 152 | callback.inputProc = &AudioDriverCoreAudio::output_callback; | 
|---|
| 153 | callback.inputProcRefCon = this; | 
|---|
| 154 | result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback)); | 
|---|
| 155 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 156 |  | 
|---|
| 157 | result = AudioUnitInitialize(audio_unit); | 
|---|
| 158 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 159 |  | 
|---|
| 160 | if (GLOBAL_GET( "audio/driver/enable_input")) { | 
|---|
| 161 | return init_input_device(); | 
|---|
| 162 | } | 
|---|
| 163 | return OK; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | OSStatus AudioDriverCoreAudio::output_callback(void *inRefCon, | 
|---|
| 167 | AudioUnitRenderActionFlags *ioActionFlags, | 
|---|
| 168 | const AudioTimeStamp *inTimeStamp, | 
|---|
| 169 | UInt32 inBusNumber, UInt32 inNumberFrames, | 
|---|
| 170 | AudioBufferList *ioData) { | 
|---|
| 171 | AudioDriverCoreAudio *ad = static_cast<AudioDriverCoreAudio *>(inRefCon); | 
|---|
| 172 |  | 
|---|
| 173 | if (!ad->active || !ad->try_lock()) { | 
|---|
| 174 | for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) { | 
|---|
| 175 | AudioBuffer *abuf = &ioData->mBuffers[i]; | 
|---|
| 176 | memset(abuf->mData, 0, abuf->mDataByteSize); | 
|---|
| 177 | } | 
|---|
| 178 | return 0; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | ad->start_counting_ticks(); | 
|---|
| 182 |  | 
|---|
| 183 | for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) { | 
|---|
| 184 | AudioBuffer *abuf = &ioData->mBuffers[i]; | 
|---|
| 185 | unsigned int frames_left = inNumberFrames; | 
|---|
| 186 | int16_t *out = (int16_t *)abuf->mData; | 
|---|
| 187 |  | 
|---|
| 188 | while (frames_left) { | 
|---|
| 189 | unsigned int frames = MIN(frames_left, ad->buffer_frames); | 
|---|
| 190 | ad->audio_server_process(frames, ad->samples_in.ptrw()); | 
|---|
| 191 |  | 
|---|
| 192 | for (unsigned int j = 0; j < frames * ad->channels; j++) { | 
|---|
| 193 | out[j] = ad->samples_in[j] >> 16; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | frames_left -= frames; | 
|---|
| 197 | out += frames * ad->channels; | 
|---|
| 198 | } | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | ad->stop_counting_ticks(); | 
|---|
| 202 | ad->unlock(); | 
|---|
| 203 |  | 
|---|
| 204 | return 0; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon, | 
|---|
| 208 | AudioUnitRenderActionFlags *ioActionFlags, | 
|---|
| 209 | const AudioTimeStamp *inTimeStamp, | 
|---|
| 210 | UInt32 inBusNumber, UInt32 inNumberFrames, | 
|---|
| 211 | AudioBufferList *ioData) { | 
|---|
| 212 | AudioDriverCoreAudio *ad = static_cast<AudioDriverCoreAudio *>(inRefCon); | 
|---|
| 213 | if (!ad->active) { | 
|---|
| 214 | return 0; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | ad->lock(); | 
|---|
| 218 | ad->start_counting_ticks(); | 
|---|
| 219 |  | 
|---|
| 220 | AudioBufferList bufferList; | 
|---|
| 221 | bufferList.mNumberBuffers = 1; | 
|---|
| 222 | bufferList.mBuffers[0].mData = ad->input_buf.ptrw(); | 
|---|
| 223 | bufferList.mBuffers[0].mNumberChannels = ad->capture_channels; | 
|---|
| 224 | bufferList.mBuffers[0].mDataByteSize = ad->input_buf.size() * sizeof(int16_t); | 
|---|
| 225 |  | 
|---|
| 226 | OSStatus result = AudioUnitRender(ad->input_unit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList); | 
|---|
| 227 | if (result == noErr) { | 
|---|
| 228 | for (unsigned int i = 0; i < inNumberFrames * ad->capture_channels; i++) { | 
|---|
| 229 | int32_t sample = ad->input_buf[i] << 16; | 
|---|
| 230 | ad->input_buffer_write(sample); | 
|---|
| 231 |  | 
|---|
| 232 | if (ad->capture_channels == 1) { | 
|---|
| 233 | // In case input device is single channel convert it to Stereo | 
|---|
| 234 | ad->input_buffer_write(sample); | 
|---|
| 235 | } | 
|---|
| 236 | } | 
|---|
| 237 | } else { | 
|---|
| 238 | ERR_PRINT( "AudioUnitRender failed, code: "+ itos(result)); | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | ad->stop_counting_ticks(); | 
|---|
| 242 | ad->unlock(); | 
|---|
| 243 |  | 
|---|
| 244 | return result; | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | void AudioDriverCoreAudio::start() { | 
|---|
| 248 | if (!active) { | 
|---|
| 249 | OSStatus result = AudioOutputUnitStart(audio_unit); | 
|---|
| 250 | if (result != noErr) { | 
|---|
| 251 | ERR_PRINT( "AudioOutputUnitStart failed, code: "+ itos(result)); | 
|---|
| 252 | } else { | 
|---|
| 253 | active = true; | 
|---|
| 254 | } | 
|---|
| 255 | } | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | void AudioDriverCoreAudio::stop() { | 
|---|
| 259 | if (active) { | 
|---|
| 260 | OSStatus result = AudioOutputUnitStop(audio_unit); | 
|---|
| 261 | if (result != noErr) { | 
|---|
| 262 | ERR_PRINT( "AudioOutputUnitStop failed, code: "+ itos(result)); | 
|---|
| 263 | } else { | 
|---|
| 264 | active = false; | 
|---|
| 265 | } | 
|---|
| 266 | } | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | int AudioDriverCoreAudio::get_mix_rate() const { | 
|---|
| 270 | return mix_rate; | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | AudioDriver::SpeakerMode AudioDriverCoreAudio::get_speaker_mode() const { | 
|---|
| 274 | return get_speaker_mode_by_total_channels(channels); | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | void AudioDriverCoreAudio::lock() { | 
|---|
| 278 | mutex.lock(); | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | void AudioDriverCoreAudio::unlock() { | 
|---|
| 282 | mutex.unlock(); | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | bool AudioDriverCoreAudio::try_lock() { | 
|---|
| 286 | return mutex.try_lock(); | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | void AudioDriverCoreAudio::finish() { | 
|---|
| 290 | finish_input_device(); | 
|---|
| 291 |  | 
|---|
| 292 | if (audio_unit) { | 
|---|
| 293 | OSStatus result; | 
|---|
| 294 |  | 
|---|
| 295 | lock(); | 
|---|
| 296 |  | 
|---|
| 297 | AURenderCallbackStruct callback; | 
|---|
| 298 | memset(&callback, 0, sizeof(AURenderCallbackStruct)); | 
|---|
| 299 | result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback)); | 
|---|
| 300 | if (result != noErr) { | 
|---|
| 301 | ERR_PRINT( "AudioUnitSetProperty failed"); | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | if (active) { | 
|---|
| 305 | result = AudioOutputUnitStop(audio_unit); | 
|---|
| 306 | if (result != noErr) { | 
|---|
| 307 | ERR_PRINT( "AudioOutputUnitStop failed"); | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 | active = false; | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | result = AudioUnitUninitialize(audio_unit); | 
|---|
| 314 | if (result != noErr) { | 
|---|
| 315 | ERR_PRINT( "AudioUnitUninitialize failed"); | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 | #ifdef MACOS_ENABLED | 
|---|
| 319 | AudioObjectPropertyAddress prop; | 
|---|
| 320 | prop.mSelector = kAudioHardwarePropertyDefaultOutputDevice; | 
|---|
| 321 | prop.mScope = kAudioObjectPropertyScopeGlobal; | 
|---|
| 322 | prop.mElement = kAudioObjectPropertyElementMaster; | 
|---|
| 323 |  | 
|---|
| 324 | result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this); | 
|---|
| 325 | if (result != noErr) { | 
|---|
| 326 | ERR_PRINT( "AudioObjectRemovePropertyListener failed"); | 
|---|
| 327 | } | 
|---|
| 328 | #endif | 
|---|
| 329 |  | 
|---|
| 330 | result = AudioComponentInstanceDispose(audio_unit); | 
|---|
| 331 | if (result != noErr) { | 
|---|
| 332 | ERR_PRINT( "AudioComponentInstanceDispose failed"); | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | audio_unit = nullptr; | 
|---|
| 336 | unlock(); | 
|---|
| 337 | } | 
|---|
| 338 | } | 
|---|
| 339 |  | 
|---|
| 340 | Error AudioDriverCoreAudio::init_input_device() { | 
|---|
| 341 | AudioComponentDescription desc; | 
|---|
| 342 | memset(&desc, 0, sizeof(desc)); | 
|---|
| 343 | desc.componentType = kAudioUnitType_Output; | 
|---|
| 344 | #ifdef MACOS_ENABLED | 
|---|
| 345 | desc.componentSubType = kAudioUnitSubType_HALOutput; | 
|---|
| 346 | #else | 
|---|
| 347 | desc.componentSubType = kAudioUnitSubType_RemoteIO; | 
|---|
| 348 | #endif | 
|---|
| 349 | desc.componentManufacturer = kAudioUnitManufacturer_Apple; | 
|---|
| 350 |  | 
|---|
| 351 | AudioComponent comp = AudioComponentFindNext(nullptr, &desc); | 
|---|
| 352 | ERR_FAIL_NULL_V(comp, FAILED); | 
|---|
| 353 |  | 
|---|
| 354 | OSStatus result = AudioComponentInstanceNew(comp, &input_unit); | 
|---|
| 355 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 356 |  | 
|---|
| 357 | #ifdef MACOS_ENABLED | 
|---|
| 358 | AudioObjectPropertyAddress prop; | 
|---|
| 359 | prop.mSelector = kAudioHardwarePropertyDefaultInputDevice; | 
|---|
| 360 | prop.mScope = kAudioObjectPropertyScopeGlobal; | 
|---|
| 361 | prop.mElement = kAudioObjectPropertyElementMaster; | 
|---|
| 362 |  | 
|---|
| 363 | result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop, &input_device_address_cb, this); | 
|---|
| 364 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 365 | #endif | 
|---|
| 366 |  | 
|---|
| 367 | UInt32 flag = 1; | 
|---|
| 368 | result = AudioUnitSetProperty(input_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); | 
|---|
| 369 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 370 | flag = 0; | 
|---|
| 371 | result = AudioUnitSetProperty(input_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &flag, sizeof(flag)); | 
|---|
| 372 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 373 |  | 
|---|
| 374 | UInt32 size; | 
|---|
| 375 | #ifdef MACOS_ENABLED | 
|---|
| 376 | AudioDeviceID deviceId; | 
|---|
| 377 | size = sizeof(AudioDeviceID); | 
|---|
| 378 | AudioObjectPropertyAddress property = { kAudioHardwarePropertyDefaultInputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; | 
|---|
| 379 |  | 
|---|
| 380 | result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &property, 0, nullptr, &size, &deviceId); | 
|---|
| 381 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 382 |  | 
|---|
| 383 | result = AudioUnitSetProperty(input_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &deviceId, sizeof(AudioDeviceID)); | 
|---|
| 384 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 385 | #endif | 
|---|
| 386 |  | 
|---|
| 387 | AudioStreamBasicDescription strdesc; | 
|---|
| 388 | memset(&strdesc, 0, sizeof(strdesc)); | 
|---|
| 389 | size = sizeof(strdesc); | 
|---|
| 390 | result = AudioUnitGetProperty(input_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, &size); | 
|---|
| 391 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 392 |  | 
|---|
| 393 | switch (strdesc.mChannelsPerFrame) { | 
|---|
| 394 | case 1: // Mono | 
|---|
| 395 | capture_channels = 1; | 
|---|
| 396 | break; | 
|---|
| 397 |  | 
|---|
| 398 | case 2: // Stereo | 
|---|
| 399 | capture_channels = 2; | 
|---|
| 400 | break; | 
|---|
| 401 |  | 
|---|
| 402 | default: | 
|---|
| 403 | // Unknown number of channels, default to stereo | 
|---|
| 404 | capture_channels = 2; | 
|---|
| 405 | break; | 
|---|
| 406 | } | 
|---|
| 407 |  | 
|---|
| 408 | mix_rate = _get_configured_mix_rate(); | 
|---|
| 409 |  | 
|---|
| 410 | memset(&strdesc, 0, sizeof(strdesc)); | 
|---|
| 411 | strdesc.mFormatID = kAudioFormatLinearPCM; | 
|---|
| 412 | strdesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; | 
|---|
| 413 | strdesc.mChannelsPerFrame = capture_channels; | 
|---|
| 414 | strdesc.mSampleRate = mix_rate; | 
|---|
| 415 | strdesc.mFramesPerPacket = 1; | 
|---|
| 416 | strdesc.mBitsPerChannel = 16; | 
|---|
| 417 | strdesc.mBytesPerFrame = strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8; | 
|---|
| 418 | strdesc.mBytesPerPacket = strdesc.mBytesPerFrame * strdesc.mFramesPerPacket; | 
|---|
| 419 |  | 
|---|
| 420 | result = AudioUnitSetProperty(input_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, sizeof(strdesc)); | 
|---|
| 421 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 422 |  | 
|---|
| 423 | AURenderCallbackStruct callback; | 
|---|
| 424 | memset(&callback, 0, sizeof(AURenderCallbackStruct)); | 
|---|
| 425 | callback.inputProc = &AudioDriverCoreAudio::input_callback; | 
|---|
| 426 | callback.inputProcRefCon = this; | 
|---|
| 427 | result = AudioUnitSetProperty(input_unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, kInputBus, &callback, sizeof(callback)); | 
|---|
| 428 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 429 |  | 
|---|
| 430 | result = AudioUnitInitialize(input_unit); | 
|---|
| 431 | ERR_FAIL_COND_V(result != noErr, FAILED); | 
|---|
| 432 |  | 
|---|
| 433 | return OK; | 
|---|
| 434 | } | 
|---|
| 435 |  | 
|---|
| 436 | void AudioDriverCoreAudio::finish_input_device() { | 
|---|
| 437 | if (input_unit) { | 
|---|
| 438 | lock(); | 
|---|
| 439 |  | 
|---|
| 440 | AURenderCallbackStruct callback; | 
|---|
| 441 | memset(&callback, 0, sizeof(AURenderCallbackStruct)); | 
|---|
| 442 | OSStatus result = AudioUnitSetProperty(input_unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &callback, sizeof(callback)); | 
|---|
| 443 | if (result != noErr) { | 
|---|
| 444 | ERR_PRINT( "AudioUnitSetProperty failed"); | 
|---|
| 445 | } | 
|---|
| 446 |  | 
|---|
| 447 | result = AudioUnitUninitialize(input_unit); | 
|---|
| 448 | if (result != noErr) { | 
|---|
| 449 | ERR_PRINT( "AudioUnitUninitialize failed"); | 
|---|
| 450 | } | 
|---|
| 451 |  | 
|---|
| 452 | #ifdef MACOS_ENABLED | 
|---|
| 453 | AudioObjectPropertyAddress prop; | 
|---|
| 454 | prop.mSelector = kAudioHardwarePropertyDefaultInputDevice; | 
|---|
| 455 | prop.mScope = kAudioObjectPropertyScopeGlobal; | 
|---|
| 456 | prop.mElement = kAudioObjectPropertyElementMaster; | 
|---|
| 457 |  | 
|---|
| 458 | result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop, &input_device_address_cb, this); | 
|---|
| 459 | if (result != noErr) { | 
|---|
| 460 | ERR_PRINT( "AudioObjectRemovePropertyListener failed"); | 
|---|
| 461 | } | 
|---|
| 462 | #endif | 
|---|
| 463 |  | 
|---|
| 464 | result = AudioComponentInstanceDispose(input_unit); | 
|---|
| 465 | if (result != noErr) { | 
|---|
| 466 | ERR_PRINT( "AudioComponentInstanceDispose failed"); | 
|---|
| 467 | } | 
|---|
| 468 |  | 
|---|
| 469 | input_unit = nullptr; | 
|---|
| 470 | unlock(); | 
|---|
| 471 | } | 
|---|
| 472 | } | 
|---|
| 473 |  | 
|---|
| 474 | Error AudioDriverCoreAudio::input_start() { | 
|---|
| 475 | input_buffer_init(buffer_frames); | 
|---|
| 476 |  | 
|---|
| 477 | OSStatus result = AudioOutputUnitStart(input_unit); | 
|---|
| 478 | if (result != noErr) { | 
|---|
| 479 | ERR_PRINT( "AudioOutputUnitStart failed, code: "+ itos(result)); | 
|---|
| 480 | } | 
|---|
| 481 |  | 
|---|
| 482 | return OK; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 | Error AudioDriverCoreAudio::input_stop() { | 
|---|
| 486 | if (input_unit) { | 
|---|
| 487 | OSStatus result = AudioOutputUnitStop(input_unit); | 
|---|
| 488 | if (result != noErr) { | 
|---|
| 489 | ERR_PRINT( "AudioOutputUnitStop failed, code: "+ itos(result)); | 
|---|
| 490 | } | 
|---|
| 491 | } | 
|---|
| 492 |  | 
|---|
| 493 | return OK; | 
|---|
| 494 | } | 
|---|
| 495 |  | 
|---|
| 496 | #ifdef MACOS_ENABLED | 
|---|
| 497 |  | 
|---|
| 498 | PackedStringArray AudioDriverCoreAudio::_get_device_list(bool input) { | 
|---|
| 499 | PackedStringArray list; | 
|---|
| 500 |  | 
|---|
| 501 | list.push_back( "Default"); | 
|---|
| 502 |  | 
|---|
| 503 | AudioObjectPropertyAddress prop; | 
|---|
| 504 |  | 
|---|
| 505 | prop.mSelector = kAudioHardwarePropertyDevices; | 
|---|
| 506 | prop.mScope = kAudioObjectPropertyScopeGlobal; | 
|---|
| 507 | prop.mElement = kAudioObjectPropertyElementMaster; | 
|---|
| 508 |  | 
|---|
| 509 | UInt32 size = 0; | 
|---|
| 510 | AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &prop, 0, nullptr, &size); | 
|---|
| 511 | AudioDeviceID *audioDevices = (AudioDeviceID *)memalloc(size); | 
|---|
| 512 | ERR_FAIL_NULL_V_MSG(audioDevices, list, "Out of memory."); | 
|---|
| 513 | AudioObjectGetPropertyData(kAudioObjectSystemObject, &prop, 0, nullptr, &size, audioDevices); | 
|---|
| 514 |  | 
|---|
| 515 | UInt32 deviceCount = size / sizeof(AudioDeviceID); | 
|---|
| 516 | for (UInt32 i = 0; i < deviceCount; i++) { | 
|---|
| 517 | prop.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; | 
|---|
| 518 | prop.mSelector = kAudioDevicePropertyStreamConfiguration; | 
|---|
| 519 |  | 
|---|
| 520 | AudioObjectGetPropertyDataSize(audioDevices[i], &prop, 0, nullptr, &size); | 
|---|
| 521 | AudioBufferList *bufferList = (AudioBufferList *)memalloc(size); | 
|---|
| 522 | ERR_FAIL_NULL_V_MSG(bufferList, list, "Out of memory."); | 
|---|
| 523 | AudioObjectGetPropertyData(audioDevices[i], &prop, 0, nullptr, &size, bufferList); | 
|---|
| 524 |  | 
|---|
| 525 | UInt32 channelCount = 0; | 
|---|
| 526 | for (UInt32 j = 0; j < bufferList->mNumberBuffers; j++) { | 
|---|
| 527 | channelCount += bufferList->mBuffers[j].mNumberChannels; | 
|---|
| 528 | } | 
|---|
| 529 |  | 
|---|
| 530 | memfree(bufferList); | 
|---|
| 531 |  | 
|---|
| 532 | if (channelCount >= 1) { | 
|---|
| 533 | CFStringRef cfname; | 
|---|
| 534 |  | 
|---|
| 535 | size = sizeof(CFStringRef); | 
|---|
| 536 | prop.mSelector = kAudioObjectPropertyName; | 
|---|
| 537 |  | 
|---|
| 538 | AudioObjectGetPropertyData(audioDevices[i], &prop, 0, nullptr, &size, &cfname); | 
|---|
| 539 |  | 
|---|
| 540 | CFIndex length = CFStringGetLength(cfname); | 
|---|
| 541 | CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1; | 
|---|
| 542 | char *buffer = (char *)memalloc(maxSize); | 
|---|
| 543 | ERR_FAIL_NULL_V_MSG(buffer, list, "Out of memory."); | 
|---|
| 544 | if (CFStringGetCString(cfname, buffer, maxSize, kCFStringEncodingUTF8)) { | 
|---|
| 545 | // Append the ID to the name in case we have devices with duplicate name | 
|---|
| 546 | list.push_back(String::utf8(buffer) + " ("+ itos(audioDevices[i]) + ")"); | 
|---|
| 547 | } | 
|---|
| 548 |  | 
|---|
| 549 | memfree(buffer); | 
|---|
| 550 | } | 
|---|
| 551 | } | 
|---|
| 552 |  | 
|---|
| 553 | memfree(audioDevices); | 
|---|
| 554 |  | 
|---|
| 555 | return list; | 
|---|
| 556 | } | 
|---|
| 557 |  | 
|---|
| 558 | void AudioDriverCoreAudio::_set_device(const String &output_device, bool input) { | 
|---|
| 559 | AudioDeviceID deviceId; | 
|---|
| 560 | bool found = false; | 
|---|
| 561 | if (output_device != "Default") { | 
|---|
| 562 | AudioObjectPropertyAddress prop; | 
|---|
| 563 |  | 
|---|
| 564 | prop.mSelector = kAudioHardwarePropertyDevices; | 
|---|
| 565 | prop.mScope = kAudioObjectPropertyScopeGlobal; | 
|---|
| 566 | prop.mElement = kAudioObjectPropertyElementMaster; | 
|---|
| 567 |  | 
|---|
| 568 | UInt32 size = 0; | 
|---|
| 569 | AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &prop, 0, nullptr, &size); | 
|---|
| 570 | AudioDeviceID *audioDevices = (AudioDeviceID *)memalloc(size); | 
|---|
| 571 | ERR_FAIL_NULL_MSG(audioDevices, "Out of memory."); | 
|---|
| 572 | AudioObjectGetPropertyData(kAudioObjectSystemObject, &prop, 0, nullptr, &size, audioDevices); | 
|---|
| 573 |  | 
|---|
| 574 | UInt32 deviceCount = size / sizeof(AudioDeviceID); | 
|---|
| 575 | for (UInt32 i = 0; i < deviceCount && !found; i++) { | 
|---|
| 576 | prop.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; | 
|---|
| 577 | prop.mSelector = kAudioDevicePropertyStreamConfiguration; | 
|---|
| 578 |  | 
|---|
| 579 | AudioObjectGetPropertyDataSize(audioDevices[i], &prop, 0, nullptr, &size); | 
|---|
| 580 | AudioBufferList *bufferList = (AudioBufferList *)memalloc(size); | 
|---|
| 581 | ERR_FAIL_NULL_MSG(bufferList, "Out of memory."); | 
|---|
| 582 | AudioObjectGetPropertyData(audioDevices[i], &prop, 0, nullptr, &size, bufferList); | 
|---|
| 583 |  | 
|---|
| 584 | UInt32 channelCount = 0; | 
|---|
| 585 | for (UInt32 j = 0; j < bufferList->mNumberBuffers; j++) { | 
|---|
| 586 | channelCount += bufferList->mBuffers[j].mNumberChannels; | 
|---|
| 587 | } | 
|---|
| 588 |  | 
|---|
| 589 | memfree(bufferList); | 
|---|
| 590 |  | 
|---|
| 591 | if (channelCount >= 1) { | 
|---|
| 592 | CFStringRef cfname; | 
|---|
| 593 |  | 
|---|
| 594 | size = sizeof(CFStringRef); | 
|---|
| 595 | prop.mSelector = kAudioObjectPropertyName; | 
|---|
| 596 |  | 
|---|
| 597 | AudioObjectGetPropertyData(audioDevices[i], &prop, 0, nullptr, &size, &cfname); | 
|---|
| 598 |  | 
|---|
| 599 | CFIndex length = CFStringGetLength(cfname); | 
|---|
| 600 | CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1; | 
|---|
| 601 | char *buffer = (char *)memalloc(maxSize); | 
|---|
| 602 | ERR_FAIL_NULL_MSG(buffer, "Out of memory."); | 
|---|
| 603 | if (CFStringGetCString(cfname, buffer, maxSize, kCFStringEncodingUTF8)) { | 
|---|
| 604 | String name = String::utf8(buffer) + " ("+ itos(audioDevices[i]) + ")"; | 
|---|
| 605 | if (name == output_device) { | 
|---|
| 606 | deviceId = audioDevices[i]; | 
|---|
| 607 | found = true; | 
|---|
| 608 | } | 
|---|
| 609 | } | 
|---|
| 610 |  | 
|---|
| 611 | memfree(buffer); | 
|---|
| 612 | } | 
|---|
| 613 | } | 
|---|
| 614 |  | 
|---|
| 615 | memfree(audioDevices); | 
|---|
| 616 | } | 
|---|
| 617 |  | 
|---|
| 618 | if (!found) { | 
|---|
| 619 | // If we haven't found the desired device get the system default one | 
|---|
| 620 | UInt32 size = sizeof(AudioDeviceID); | 
|---|
| 621 | UInt32 elem = input ? kAudioHardwarePropertyDefaultInputDevice : kAudioHardwarePropertyDefaultOutputDevice; | 
|---|
| 622 | AudioObjectPropertyAddress property = { elem, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; | 
|---|
| 623 |  | 
|---|
| 624 | OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &property, 0, nullptr, &size, &deviceId); | 
|---|
| 625 | ERR_FAIL_COND(result != noErr); | 
|---|
| 626 |  | 
|---|
| 627 | found = true; | 
|---|
| 628 | } | 
|---|
| 629 |  | 
|---|
| 630 | if (found) { | 
|---|
| 631 | OSStatus result = AudioUnitSetProperty(input ? input_unit : audio_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &deviceId, sizeof(AudioDeviceID)); | 
|---|
| 632 | ERR_FAIL_COND(result != noErr); | 
|---|
| 633 |  | 
|---|
| 634 | if (input) { | 
|---|
| 635 | // Reset audio input to keep synchronization. | 
|---|
| 636 | input_position = 0; | 
|---|
| 637 | input_size = 0; | 
|---|
| 638 | } | 
|---|
| 639 | } | 
|---|
| 640 | } | 
|---|
| 641 |  | 
|---|
| 642 | PackedStringArray AudioDriverCoreAudio::get_output_device_list() { | 
|---|
| 643 | return _get_device_list(); | 
|---|
| 644 | } | 
|---|
| 645 |  | 
|---|
| 646 | String AudioDriverCoreAudio::get_output_device() { | 
|---|
| 647 | return output_device_name; | 
|---|
| 648 | } | 
|---|
| 649 |  | 
|---|
| 650 | void AudioDriverCoreAudio::set_output_device(const String &p_name) { | 
|---|
| 651 | output_device_name = p_name; | 
|---|
| 652 | if (active) { | 
|---|
| 653 | _set_device(output_device_name); | 
|---|
| 654 | } | 
|---|
| 655 | } | 
|---|
| 656 |  | 
|---|
| 657 | PackedStringArray AudioDriverCoreAudio::get_input_device_list() { | 
|---|
| 658 | return _get_device_list(true); | 
|---|
| 659 | } | 
|---|
| 660 |  | 
|---|
| 661 | String AudioDriverCoreAudio::get_input_device() { | 
|---|
| 662 | return input_device_name; | 
|---|
| 663 | } | 
|---|
| 664 |  | 
|---|
| 665 | void AudioDriverCoreAudio::set_input_device(const String &p_name) { | 
|---|
| 666 | input_device_name = p_name; | 
|---|
| 667 | if (active) { | 
|---|
| 668 | _set_device(input_device_name, true); | 
|---|
| 669 | } | 
|---|
| 670 | } | 
|---|
| 671 |  | 
|---|
| 672 | #endif | 
|---|
| 673 |  | 
|---|
| 674 | AudioDriverCoreAudio::AudioDriverCoreAudio() { | 
|---|
| 675 | samples_in.clear(); | 
|---|
| 676 | } | 
|---|
| 677 |  | 
|---|
| 678 | #endif // COREAUDIO_ENABLED | 
|---|
| 679 |  | 
|---|