RetroArch/audio/coreaudio.c

301 lines
8.0 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2012-01-08 00:08:18 +00:00
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
* Copyright (C) 2011-2012 - Chris Moeller
2011-08-08 15:27:52 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-08-08 15:27:52 +00:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-08-08 15:27:52 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#include "general.h"
#include "fifo_buffer.h"
#include <stdlib.h>
2011-12-24 12:46:12 +00:00
#include "../boolean.h"
2011-08-08 15:27:52 +00:00
#include <pthread.h>
#include <CoreAudio/CoreAudio.h>
#include <CoreServices/CoreServices.h>
#include <AudioUnit/AudioUnit.h>
#include <AudioUnit/AUComponent.h>
typedef struct coreaudio
{
pthread_mutex_t lock;
pthread_cond_t cond;
ComponentInstance dev;
bool dev_alive;
fifo_buffer_t *buffer;
bool nonblock;
2012-02-26 00:22:07 +00:00
size_t buffer_size;
2011-08-08 15:27:52 +00:00
} coreaudio_t;
static void coreaudio_free(void *data)
{
2011-12-24 12:46:12 +00:00
coreaudio_t *dev = (coreaudio_t*)data;
2011-08-08 15:27:52 +00:00
if (!dev)
return;
if (dev->dev_alive)
{
AudioOutputUnitStop(dev->dev);
CloseComponent(dev->dev);
}
if (dev->buffer)
fifo_free(dev->buffer);
pthread_mutex_destroy(&dev->lock);
pthread_cond_destroy(&dev->cond);
free(dev);
}
static OSStatus audio_cb(void *userdata, AudioUnitRenderActionFlags *action_flags,
const AudioTimeStamp *time_stamp, UInt32 bus_number,
UInt32 number_frames, AudioBufferList *io_data)
{
2011-12-24 12:46:12 +00:00
coreaudio_t *dev = (coreaudio_t*)userdata;
2011-08-08 15:27:52 +00:00
(void)time_stamp;
(void)bus_number;
(void)number_frames;
if (!io_data)
return noErr;
if (io_data->mNumberBuffers != 1)
return noErr;
unsigned write_avail = io_data->mBuffers[0].mDataByteSize;
2011-08-09 20:07:11 +00:00
void *outbuf = io_data->mBuffers[0].mData;
2011-08-08 15:27:52 +00:00
pthread_mutex_lock(&dev->lock);
if (fifo_read_avail(dev->buffer) < write_avail)
{
*action_flags = kAudioUnitRenderAction_OutputIsSilence;
2011-08-09 20:07:11 +00:00
memset(outbuf, 0, write_avail); // Seems to be needed.
2011-08-08 15:27:52 +00:00
pthread_mutex_unlock(&dev->lock);
pthread_cond_signal(&dev->cond); // Technically possible to deadlock without.
return noErr;
}
2011-08-08 15:31:03 +00:00
fifo_read(dev->buffer, outbuf, write_avail);
2011-08-08 15:27:52 +00:00
pthread_mutex_unlock(&dev->lock);
pthread_cond_signal(&dev->cond);
return noErr;
}
2011-11-02 18:31:36 +00:00
static void *coreaudio_init(const char *device, unsigned rate, unsigned latency)
2011-08-08 15:27:52 +00:00
{
(void)device;
2011-12-24 12:46:12 +00:00
coreaudio_t *dev = (coreaudio_t*)calloc(1, sizeof(*dev));
2011-08-08 15:27:52 +00:00
if (!dev)
return NULL;
CFRunLoopRef run_loop = NULL;
AudioObjectPropertyAddress addr = {
kAudioHardwarePropertyRunLoop,
kAudioObjectPropertyScopeGlobal,
2011-08-08 15:31:03 +00:00
kAudioObjectPropertyElementMaster
2011-08-08 15:27:52 +00:00
};
pthread_mutex_init(&dev->lock, NULL);
pthread_cond_init(&dev->cond, NULL);
AudioObjectSetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL,
sizeof(CFRunLoopRef), &run_loop);
2011-12-24 12:46:12 +00:00
ComponentDescription desc = {0};
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_HALOutput;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
AudioStreamBasicDescription stream_desc = {0};
AudioStreamBasicDescription real_desc;
AudioChannelLayout layout = {0};
AURenderCallbackStruct cb = {0};
2011-08-08 15:27:52 +00:00
2011-12-24 12:46:12 +00:00
Component comp = NULL;
OSStatus res = noErr;
UInt32 i_size = 0;
size_t fifo_size;
comp = FindNextComponent(NULL, &desc);
2011-08-08 15:27:52 +00:00
if (comp == NULL)
goto error;
2011-12-24 12:46:12 +00:00
res = OpenAComponent(comp, &dev->dev);
2011-08-08 15:27:52 +00:00
if (res != noErr)
goto error;
dev->dev_alive = true;
2011-12-24 12:46:12 +00:00
stream_desc.mSampleRate = rate;
stream_desc.mBitsPerChannel = sizeof(float) * CHAR_BIT;
stream_desc.mChannelsPerFrame = 2;
stream_desc.mBytesPerPacket = 2 * sizeof(float);
stream_desc.mBytesPerFrame = 2 * sizeof(float);
stream_desc.mFramesPerPacket = 1;
stream_desc.mFormatID = kAudioFormatLinearPCM;
stream_desc.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked | (is_little_endian() ? 0 : kAudioFormatFlagIsBigEndian);
2011-08-08 15:27:52 +00:00
res = AudioUnitSetProperty(dev->dev, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, 0, &stream_desc, sizeof(stream_desc));
if (res != noErr)
goto error;
2011-12-24 12:46:12 +00:00
i_size = sizeof(real_desc);
2011-08-08 15:27:52 +00:00
res = AudioUnitGetProperty(dev->dev, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &real_desc, &i_size);
if (res != noErr)
goto error;
2012-04-21 21:25:32 +00:00
RARCH_LOG("[CoreAudio]: Using output sample rate of %.1f Hz\n", (float)real_desc.mSampleRate);
2011-08-08 15:27:52 +00:00
g_settings.audio.out_rate = real_desc.mSampleRate;
if (real_desc.mChannelsPerFrame != stream_desc.mChannelsPerFrame)
goto error;
if (real_desc.mBitsPerChannel != stream_desc.mBitsPerChannel)
goto error;
if (real_desc.mFormatFlags != stream_desc.mFormatFlags)
goto error;
if (real_desc.mFormatID != stream_desc.mFormatID)
goto error;
2011-12-24 12:46:12 +00:00
layout.mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelBitmap;
layout.mChannelBitmap = 0x03;
2011-08-08 15:27:52 +00:00
res = AudioUnitSetProperty(dev->dev, kAudioUnitProperty_AudioChannelLayout,
kAudioUnitScope_Input, 0, &layout, sizeof(layout));
if (res != noErr)
goto error;
2011-12-24 12:46:12 +00:00
cb.inputProc = audio_cb;
cb.inputProcRefCon = dev;
2011-08-08 15:27:52 +00:00
res = AudioUnitSetProperty(dev->dev, kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input, 0, &cb, sizeof(cb));
if (res != noErr)
goto error;
res = AudioUnitInitialize(dev->dev);
if (res != noErr)
goto error;
2011-12-24 12:46:12 +00:00
fifo_size = (latency * g_settings.audio.out_rate) / 1000;
2011-08-08 15:45:49 +00:00
fifo_size *= 2 * sizeof(float);
2012-02-26 00:22:07 +00:00
dev->buffer_size = fifo_size;
2011-08-08 15:27:52 +00:00
dev->buffer = fifo_new(fifo_size);
if (!dev->buffer)
goto error;
2012-04-21 21:25:32 +00:00
RARCH_LOG("[CoreAudio]: Using buffer size of %u bytes: (latency = %u ms)\n", (unsigned)fifo_size, latency);
2011-08-08 15:27:52 +00:00
res = AudioOutputUnitStart(dev->dev);
if (res != noErr)
goto error;
return dev;
error:
2012-04-21 21:25:32 +00:00
RARCH_ERR("[CoreAudio]: Failed to initialize driver ...\n");
2011-08-08 15:27:52 +00:00
coreaudio_free(dev);
return NULL;
}
2011-11-02 18:31:36 +00:00
static ssize_t coreaudio_write(void *data, const void *buf_, size_t size)
2011-08-08 15:27:52 +00:00
{
2011-12-24 12:46:12 +00:00
coreaudio_t *dev = (coreaudio_t*)data;
2011-08-08 15:27:52 +00:00
2011-12-24 12:46:12 +00:00
const uint8_t *buf = (const uint8_t*)buf_;
2011-08-08 15:27:52 +00:00
size_t written = 0;
while (size > 0)
{
pthread_mutex_lock(&dev->lock);
size_t write_avail = fifo_write_avail(dev->buffer);
if (write_avail > size)
write_avail = size;
fifo_write(dev->buffer, buf, write_avail);
buf += write_avail;
written += write_avail;
size -= write_avail;
if (dev->nonblock)
{
pthread_mutex_unlock(&dev->lock);
break;
}
if (write_avail == 0)
pthread_cond_wait(&dev->cond, &dev->lock);
pthread_mutex_unlock(&dev->lock);
}
return written;
}
static bool coreaudio_stop(void *data)
{
2011-12-24 12:46:12 +00:00
coreaudio_t *dev = (coreaudio_t*)data;
2011-08-08 15:27:52 +00:00
return AudioOutputUnitStop(dev->dev) == noErr;
}
static void coreaudio_set_nonblock_state(void *data, bool state)
{
2011-12-24 12:46:12 +00:00
coreaudio_t *dev = (coreaudio_t*)data;
2011-08-08 15:27:52 +00:00
dev->nonblock = state;
}
static bool coreaudio_start(void *data)
{
2011-12-24 12:46:12 +00:00
coreaudio_t *dev = (coreaudio_t*)data;
2011-08-08 15:27:52 +00:00
return AudioOutputUnitStart(dev->dev) == noErr;
}
2011-08-08 15:45:49 +00:00
static bool coreaudio_use_float(void *data)
{
(void)data;
return true;
}
2012-02-26 00:22:07 +00:00
static size_t coreaudio_write_avail(void *data)
{
coreaudio_t *dev = (coreaudio_t*)data;
pthread_mutex_lock(&dev->lock);
size_t avail = fifo_write_avail(dev->buffer);
pthread_mutex_unlock(&dev->lock);
return avail;
}
static size_t coreaudio_buffer_size(void *data)
{
coreaudio_t *dev = (coreaudio_t*)data;
return dev->buffer_size;
}
2011-08-08 15:31:03 +00:00
const audio_driver_t audio_coreaudio = {
2011-12-24 12:46:12 +00:00
coreaudio_init,
coreaudio_write,
coreaudio_stop,
coreaudio_start,
coreaudio_set_nonblock_state,
coreaudio_free,
coreaudio_use_float,
2012-02-26 00:22:07 +00:00
"coreaudio",
coreaudio_write_avail,
coreaudio_buffer_size,
2011-08-08 15:27:52 +00:00
};
2011-12-24 12:46:12 +00:00