2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_driver_osx.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#ifdef OSX_ENABLED
|
|
|
|
|
|
|
|
#include "audio_driver_osx.h"
|
|
|
|
|
2017-07-23 16:40:38 +00:00
|
|
|
static OSStatus outputDeviceAddressCB(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, void *__nullable inClientData) {
|
|
|
|
AudioDriverOSX *driver = (AudioDriverOSX *)inClientData;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-23 16:40:38 +00:00
|
|
|
driver->reopen();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-23 16:40:38 +00:00
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error AudioDriverOSX::initDevice() {
|
2014-02-10 01:10:30 +00:00
|
|
|
AudioStreamBasicDescription strdesc;
|
|
|
|
strdesc.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
strdesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
|
|
|
|
strdesc.mChannelsPerFrame = channels;
|
|
|
|
strdesc.mSampleRate = 44100;
|
|
|
|
strdesc.mFramesPerPacket = 1;
|
|
|
|
strdesc.mBitsPerChannel = 16;
|
2017-07-23 16:40:38 +00:00
|
|
|
strdesc.mBytesPerFrame = strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
|
|
|
|
strdesc.mBytesPerPacket = strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-23 16:40:38 +00:00
|
|
|
OSStatus result;
|
2014-02-10 01:10:30 +00:00
|
|
|
AURenderCallbackStruct callback;
|
|
|
|
AudioComponentDescription desc;
|
|
|
|
AudioComponent comp = NULL;
|
|
|
|
const AudioUnitElement output_bus = 0;
|
|
|
|
const AudioUnitElement bus = output_bus;
|
|
|
|
const AudioUnitScope scope = kAudioUnitScope_Input;
|
|
|
|
|
|
|
|
zeromem(&desc, sizeof(desc));
|
|
|
|
desc.componentType = kAudioUnitType_Output;
|
2017-07-23 16:40:38 +00:00
|
|
|
desc.componentSubType = kAudioUnitSubType_HALOutput;
|
2014-02-10 01:10:30 +00:00
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
|
2017-07-23 16:40:38 +00:00
|
|
|
comp = AudioComponentFindNext(NULL, &desc);
|
|
|
|
ERR_FAIL_COND_V(comp == NULL, FAILED);
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
result = AudioComponentInstanceNew(comp, &audio_unit);
|
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
|
|
|
|
2017-07-23 16:40:38 +00:00
|
|
|
result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, scope, bus, &strdesc, sizeof(strdesc));
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND_V(result != noErr, FAILED);
|
|
|
|
|
|
|
|
zeromem(&callback, sizeof(AURenderCallbackStruct));
|
|
|
|
callback.inputProc = &AudioDriverOSX::output_callback;
|
|
|
|
callback.inputProcRefCon = this;
|
2017-07-23 16:40:38 +00:00
|
|
|
result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, scope, bus, &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-23 16:40:38 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error AudioDriverOSX::finishDevice() {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error AudioDriverOSX::init() {
|
|
|
|
OSStatus result;
|
|
|
|
|
|
|
|
active = false;
|
|
|
|
channels = 2;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
const int samples = 1024;
|
|
|
|
samples_in = memnew_arr(int32_t, samples); // whatever
|
|
|
|
buffer_frames = samples / channels;
|
|
|
|
|
2017-07-23 16:40:38 +00:00
|
|
|
return initDevice();
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-07-23 16:40:38 +00:00
|
|
|
Error AudioDriverOSX::reopen() {
|
|
|
|
Error err;
|
|
|
|
bool restart = false;
|
|
|
|
|
|
|
|
lock();
|
|
|
|
|
|
|
|
if (active) {
|
|
|
|
restart = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = finishDevice();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
OSStatus AudioDriverOSX::output_callback(void *inRefCon,
|
2017-03-18 23:36:26 +00:00
|
|
|
AudioUnitRenderActionFlags *ioActionFlags,
|
|
|
|
const AudioTimeStamp *inTimeStamp,
|
|
|
|
UInt32 inBusNumber, UInt32 inNumberFrames,
|
|
|
|
AudioBufferList *ioData) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
AudioBuffer *abuf;
|
2017-03-18 23:36:26 +00:00
|
|
|
AudioDriverOSX *ad = (AudioDriverOSX *)inRefCon;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-02 14:31:01 +00:00
|
|
|
bool mix = true;
|
|
|
|
|
|
|
|
if (!ad->active)
|
|
|
|
mix = false;
|
|
|
|
else if (ad->mutex) {
|
|
|
|
mix = ad->mutex->try_lock() == OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!mix) {
|
2014-02-10 01:10:30 +00:00
|
|
|
for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
|
|
|
|
abuf = &ioData->mBuffers[i];
|
|
|
|
zeromem(abuf->mData, abuf->mDataByteSize);
|
|
|
|
};
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
int frames_left;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < ioData->mNumberBuffers; i++) {
|
|
|
|
|
|
|
|
abuf = &ioData->mBuffers[i];
|
|
|
|
frames_left = inNumberFrames;
|
2017-03-18 23:36:26 +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);
|
2014-11-02 14:31:01 +00:00
|
|
|
//ad->lock();
|
2014-02-10 01:10:30 +00:00
|
|
|
ad->audio_server_process(frames, ad->samples_in);
|
2014-11-02 14:31:01 +00:00
|
|
|
//ad->unlock();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < frames * ad->channels; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
out[i] = ad->samples_in[i] >> 16;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
frames_left -= frames;
|
|
|
|
out += frames * ad->channels;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-11-02 14:31:01 +00:00
|
|
|
if (ad->mutex)
|
|
|
|
ad->mutex->unlock();
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
void AudioDriverOSX::start() {
|
2017-07-23 16:40:38 +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
|
|
|
};
|
|
|
|
|
|
|
|
int AudioDriverOSX::get_mix_rate() const {
|
|
|
|
return 44100;
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioDriverSW::OutputFormat AudioDriverOSX::get_output_format() const {
|
|
|
|
return OUTPUT_STEREO;
|
|
|
|
};
|
|
|
|
|
2014-11-02 14:31:01 +00:00
|
|
|
void AudioDriverOSX::lock() {
|
2017-07-23 16:40:38 +00:00
|
|
|
if (mutex)
|
2014-11-02 14:31:01 +00:00
|
|
|
mutex->lock();
|
|
|
|
};
|
|
|
|
void AudioDriverOSX::unlock() {
|
2017-07-23 16:40:38 +00:00
|
|
|
if (mutex)
|
2014-11-02 14:31:01 +00:00
|
|
|
mutex->unlock();
|
|
|
|
};
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void AudioDriverOSX::finish() {
|
2017-07-23 16:40:38 +00:00
|
|
|
OSStatus result;
|
|
|
|
|
|
|
|
finishDevice();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-23 16:40:38 +00:00
|
|
|
result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &outputDeviceAddress, &outputDeviceAddressCB, this);
|
|
|
|
if (result != noErr) {
|
|
|
|
ERR_PRINT("AudioObjectRemovePropertyListener failed");
|
|
|
|
}
|
2016-01-29 10:18:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
memdelete_arr(samples_in);
|
|
|
|
};
|
|
|
|
|
2014-11-02 14:31:01 +00:00
|
|
|
AudioDriverOSX::AudioDriverOSX() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
mutex = Mutex::create(); //NULL;
|
2014-11-02 14:31:01 +00:00
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
AudioDriverOSX::~AudioDriverOSX(){
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif
|