2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2017-09-29 18:22:42 +00:00
|
|
|
/* audio_driver_coreaudio.cpp */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
#ifdef COREAUDIO_ENABLED
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
#include "audio_driver_coreaudio.h"
|
2017-08-17 21:35:55 +00:00
|
|
|
#include "core/project_settings.h"
|
|
|
|
#include "os/os.h"
|
|
|
|
|
|
|
|
#define kOutputBus 0
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
#ifdef OSX_ENABLED
|
2018-02-14 16:10:17 +00:00
|
|
|
static OSStatus outputDeviceAddressCB(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, void *inClientData) {
|
2017-09-29 18:22:42 +00:00
|
|
|
AudioDriverCoreAudio *driver = (AudioDriverCoreAudio *)inClientData;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-17 01:29:47 +00:00
|
|
|
driver->reopen();
|
|
|
|
|
|
|
|
return noErr;
|
|
|
|
}
|
2017-09-29 18:22:42 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
Error AudioDriverCoreAudio::initDevice() {
|
2017-08-17 21:35:55 +00:00
|
|
|
AudioComponentDescription desc;
|
|
|
|
zeromem(&desc, sizeof(desc));
|
|
|
|
desc.componentType = kAudioUnitType_Output;
|
2017-09-29 18:22:42 +00:00
|
|
|
#ifdef OSX_ENABLED
|
2017-08-17 21:35:55 +00:00
|
|
|
desc.componentSubType = kAudioUnitSubType_HALOutput;
|
2017-09-29 18:22:42 +00:00
|
|
|
#else
|
|
|
|
desc.componentSubType = kAudioUnitSubType_RemoteIO;
|
|
|
|
#endif
|
2017-08-17 21:35:55 +00:00
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
|
|
|
|
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
|
|
|
|
ERR_FAIL_COND_V(comp == NULL, FAILED);
|
|
|
|
|
|
|
|
OSStatus result = AudioComponentInstanceNew(comp, &audio_unit);
|
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
AudioStreamBasicDescription strdesc;
|
2017-08-17 21:35:55 +00:00
|
|
|
|
2017-08-22 21:27:17 +00:00
|
|
|
zeromem(&strdesc, sizeof(strdesc));
|
2017-08-17 21:35:55 +00:00
|
|
|
UInt32 size = sizeof(strdesc);
|
|
|
|
result = AudioUnitGetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kOutputBus, &strdesc, &size);
|
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
|
|
|
|
|
|
|
switch (strdesc.mChannelsPerFrame) {
|
|
|
|
case 2: // Stereo
|
2017-08-22 21:27:17 +00:00
|
|
|
case 4: // Surround 3.1
|
2017-08-17 21:35:55 +00:00
|
|
|
case 6: // Surround 5.1
|
|
|
|
case 8: // Surround 7.1
|
|
|
|
channels = strdesc.mChannelsPerFrame;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Unknown number of channels, default to stereo
|
|
|
|
channels = 2;
|
|
|
|
break;
|
2017-08-22 21:27:17 +00:00
|
|
|
}
|
2017-08-17 21:35:55 +00:00
|
|
|
|
2017-08-29 19:47:44 +00:00
|
|
|
mix_rate = GLOBAL_DEF("audio/mix_rate", DEFAULT_MIX_RATE);
|
2017-08-17 21:35:55 +00:00
|
|
|
|
|
|
|
zeromem(&strdesc, sizeof(strdesc));
|
2014-02-10 01:10:30 +00:00
|
|
|
strdesc.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
strdesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
|
|
|
|
strdesc.mChannelsPerFrame = channels;
|
2017-08-17 21:35:55 +00:00
|
|
|
strdesc.mSampleRate = mix_rate;
|
2014-02-10 01:10:30 +00:00
|
|
|
strdesc.mFramesPerPacket = 1;
|
|
|
|
strdesc.mBitsPerChannel = 16;
|
2017-07-17 01:29:47 +00:00
|
|
|
strdesc.mBytesPerFrame = strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
|
|
|
|
strdesc.mBytesPerPacket = strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-17 21:35:55 +00:00
|
|
|
result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc));
|
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-29 19:47:44 +00:00
|
|
|
int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
|
|
|
|
// Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels)
|
|
|
|
buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
|
2017-07-19 14:05:51 +00:00
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
#ifdef OSX_ENABLED
|
2017-08-29 19:47:44 +00:00
|
|
|
result = AudioUnitSetProperty(audio_unit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, kOutputBus, &buffer_frames, sizeof(UInt32));
|
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
2017-09-29 18:22:42 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-29 19:47:44 +00:00
|
|
|
buffer_size = buffer_frames * channels;
|
2017-08-17 21:35:55 +00:00
|
|
|
samples_in.resize(buffer_size);
|
2017-08-29 19:47:44 +00:00
|
|
|
|
|
|
|
if (OS::get_singleton()->is_stdout_verbose()) {
|
2017-08-22 21:27:17 +00:00
|
|
|
print_line("CoreAudio: detected " + itos(channels) + " channels");
|
|
|
|
print_line("CoreAudio: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
|
2017-08-29 19:47:44 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-17 21:35:55 +00:00
|
|
|
AURenderCallbackStruct callback;
|
2014-02-10 01:10:30 +00:00
|
|
|
zeromem(&callback, sizeof(AURenderCallbackStruct));
|
2017-09-29 18:22:42 +00:00
|
|
|
callback.inputProc = &AudioDriverCoreAudio::output_callback;
|
2014-02-10 01:10:30 +00:00
|
|
|
callback.inputProcRefCon = this;
|
2017-08-17 21:35:55 +00:00
|
|
|
result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback));
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
|
|
|
|
|
|
|
result = AudioUnitInitialize(audio_unit);
|
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
|
|
|
|
2017-07-17 01:29:47 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
Error AudioDriverCoreAudio::finishDevice() {
|
2017-07-17 01:29:47 +00:00
|
|
|
OSStatus result;
|
|
|
|
|
|
|
|
if (active) {
|
|
|
|
result = AudioOutputUnitStop(audio_unit);
|
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
|
|
|
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = AudioUnitUninitialize(audio_unit);
|
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
Error AudioDriverCoreAudio::init() {
|
2017-07-17 01:29:47 +00:00
|
|
|
OSStatus result;
|
|
|
|
|
2017-07-30 19:35:08 +00:00
|
|
|
mutex = Mutex::create();
|
2017-07-17 01:29:47 +00:00
|
|
|
active = false;
|
|
|
|
channels = 2;
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
#ifdef OSX_ENABLED
|
2017-07-17 01:29:47 +00:00
|
|
|
outputDeviceAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
|
|
|
outputDeviceAddress.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
outputDeviceAddress.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
|
|
|
result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &outputDeviceAddress, &outputDeviceAddressCB, this);
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
2017-09-29 18:22:42 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-17 01:29:47 +00:00
|
|
|
return initDevice();
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
Error AudioDriverCoreAudio::reopen() {
|
2017-07-17 01:29:47 +00:00
|
|
|
bool restart = false;
|
|
|
|
|
|
|
|
lock();
|
|
|
|
|
|
|
|
if (active) {
|
|
|
|
restart = true;
|
|
|
|
}
|
|
|
|
|
2017-08-17 21:35:55 +00:00
|
|
|
Error err = finishDevice();
|
2017-07-17 01:29:47 +00:00
|
|
|
if (err != OK) {
|
|
|
|
ERR_PRINT("finishDevice failed");
|
|
|
|
unlock();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = initDevice();
|
|
|
|
if (err != OK) {
|
|
|
|
ERR_PRINT("initDevice failed");
|
|
|
|
unlock();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (restart) {
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
OSStatus AudioDriverCoreAudio::output_callback(void *inRefCon,
|
2017-03-05 15:44:50 +00:00
|
|
|
AudioUnitRenderActionFlags *ioActionFlags,
|
|
|
|
const AudioTimeStamp *inTimeStamp,
|
|
|
|
UInt32 inBusNumber, UInt32 inNumberFrames,
|
|
|
|
AudioBufferList *ioData) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
AudioDriverCoreAudio *ad = (AudioDriverCoreAudio *)inRefCon;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-30 19:35:08 +00:00
|
|
|
if (!ad->active || !ad->try_lock()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
|
2017-07-30 19:35:08 +00:00
|
|
|
AudioBuffer *abuf = &ioData->mBuffers[i];
|
2014-02-10 01:10:30 +00:00
|
|
|
zeromem(abuf->mData, abuf->mDataByteSize);
|
|
|
|
};
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
|
|
|
|
|
2017-07-30 19:35:08 +00:00
|
|
|
AudioBuffer *abuf = &ioData->mBuffers[i];
|
|
|
|
int frames_left = inNumberFrames;
|
2017-03-05 15:44:50 +00:00
|
|
|
int16_t *out = (int16_t *)abuf->mData;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
while (frames_left) {
|
|
|
|
|
|
|
|
int frames = MIN(frames_left, ad->buffer_frames);
|
2017-11-25 09:42:20 +00:00
|
|
|
ad->audio_server_process(frames, ad->samples_in.ptrw());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-30 19:35:08 +00:00
|
|
|
for (int j = 0; j < frames * ad->channels; j++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-30 19:35:08 +00:00
|
|
|
out[j] = ad->samples_in[j] >> 16;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
frames_left -= frames;
|
|
|
|
out += frames * ad->channels;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-07-30 19:35:08 +00:00
|
|
|
ad->unlock();
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
void AudioDriverCoreAudio::start() {
|
2017-07-17 01:29:47 +00:00
|
|
|
if (!active) {
|
|
|
|
OSStatus result = AudioOutputUnitStart(audio_unit);
|
|
|
|
if (result != noErr) {
|
|
|
|
ERR_PRINT("AudioOutputUnitStart failed");
|
|
|
|
} else {
|
|
|
|
active = true;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
int AudioDriverCoreAudio::get_mix_rate() const {
|
2017-08-29 19:47:44 +00:00
|
|
|
return mix_rate;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
AudioDriver::SpeakerMode AudioDriverCoreAudio::get_speaker_mode() const {
|
2017-08-22 21:27:17 +00:00
|
|
|
return get_speaker_mode_by_total_channels(channels);
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
void AudioDriverCoreAudio::lock() {
|
2017-07-17 01:29:47 +00:00
|
|
|
if (mutex)
|
2014-11-02 14:31:01 +00:00
|
|
|
mutex->lock();
|
|
|
|
};
|
2017-07-30 19:35:08 +00:00
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
void AudioDriverCoreAudio::unlock() {
|
2017-07-17 01:29:47 +00:00
|
|
|
if (mutex)
|
2014-11-02 14:31:01 +00:00
|
|
|
mutex->unlock();
|
|
|
|
};
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
bool AudioDriverCoreAudio::try_lock() {
|
2017-07-30 19:35:08 +00:00
|
|
|
if (mutex)
|
|
|
|
return mutex->try_lock() == OK;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
void AudioDriverCoreAudio::finish() {
|
|
|
|
OSStatus result;
|
|
|
|
|
2017-07-17 01:29:47 +00:00
|
|
|
finishDevice();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
#ifdef OSX_ENABLED
|
|
|
|
result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &outputDeviceAddress, &outputDeviceAddressCB, this);
|
2017-07-17 01:29:47 +00:00
|
|
|
if (result != noErr) {
|
|
|
|
ERR_PRINT("AudioObjectRemovePropertyListener failed");
|
|
|
|
}
|
2017-09-29 18:22:42 +00:00
|
|
|
#endif
|
2016-01-29 10:18:27 +00:00
|
|
|
|
2017-08-17 21:35:55 +00:00
|
|
|
AURenderCallbackStruct callback;
|
|
|
|
zeromem(&callback, sizeof(AURenderCallbackStruct));
|
|
|
|
result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback));
|
|
|
|
if (result != noErr) {
|
|
|
|
ERR_PRINT("AudioUnitSetProperty failed");
|
|
|
|
}
|
|
|
|
|
2017-07-30 19:35:08 +00:00
|
|
|
if (mutex) {
|
|
|
|
memdelete(mutex);
|
|
|
|
mutex = NULL;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
AudioDriverCoreAudio::AudioDriverCoreAudio() {
|
2017-08-17 21:35:55 +00:00
|
|
|
active = false;
|
2017-07-30 19:35:08 +00:00
|
|
|
mutex = NULL;
|
2017-08-17 21:35:55 +00:00
|
|
|
|
2017-08-29 19:47:44 +00:00
|
|
|
mix_rate = 0;
|
2017-08-17 21:35:55 +00:00
|
|
|
channels = 2;
|
2017-08-29 19:47:44 +00:00
|
|
|
|
|
|
|
buffer_size = 0;
|
|
|
|
buffer_frames = 0;
|
|
|
|
|
2017-08-17 21:35:55 +00:00
|
|
|
samples_in.clear();
|
2014-11-02 14:31:01 +00:00
|
|
|
};
|
|
|
|
|
2017-09-29 18:22:42 +00:00
|
|
|
AudioDriverCoreAudio::~AudioDriverCoreAudio(){};
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif
|