RetroArch/ps3/ps3_audio.c

239 lines
6.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 - Daniel De Matteis
2011-11-30 16:11:42 +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-11-30 16:11:42 +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-11-30 16:11:42 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-11-30 16:11:42 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "../driver.h"
2011-12-02 14:50:51 +00:00
#include "../general.h"
2011-11-30 16:11:42 +00:00
#include <stdlib.h>
#ifdef __PSL1GHT__
#include <audio/audio.h>
2012-07-01 16:22:57 +00:00
#include <sys/thread.h>
#else
2011-11-30 16:11:42 +00:00
#include <cell/audio.h>
#endif
2011-11-30 16:11:42 +00:00
#include <string.h>
2011-12-02 14:50:51 +00:00
#include "../fifo_buffer.h"
#include "sdk_defines.h"
#ifdef __PSL1GHT__
#include <sys/event_queue.h>
#include <lv2/mutex.h>
#include <lv2/cond.h>
//forward decl. for audioAddData
extern int audioAddData(uint32_t portNum, float *data, uint32_t frames, float volume);
#else
2011-11-30 16:11:42 +00:00
#include <sys/event.h>
#include <sys/synchronization.h>
#endif
2011-11-30 16:11:42 +00:00
#define AUDIO_BLOCKS 8 // 8 or 16. Guess what we choose? :)
#define AUDIO_CHANNELS 2 // All hail glorious stereo!
typedef struct
{
uint32_t audio_port;
bool nonblocking;
volatile bool quit_thread;
fifo_buffer_t *buffer;
sys_ppu_thread_t thread;
2011-12-02 15:21:01 +00:00
sys_lwmutex_t lock;
sys_lwmutex_t cond_lock;
sys_lwcond_t cond;
2011-11-30 16:11:42 +00:00
} ps3_audio_t;
2012-07-01 16:22:57 +00:00
#ifdef __PSL1GHT__
static void event_loop(void *data)
#else
static void event_loop(uint64_t data)
2012-07-01 16:22:57 +00:00
#endif
2011-11-30 16:11:42 +00:00
{
2012-07-01 16:22:57 +00:00
#ifdef __PSL1GHT__
ps3_audio_t *aud = data;
#else
void * ptr_data = (void*)(uintptr_t)data;
ps3_audio_t *aud = ptr_data;
2012-07-01 16:22:57 +00:00
#endif
2011-11-30 16:11:42 +00:00
sys_event_queue_t id;
sys_ipc_key_t key;
sys_event_t event;
pAudioCreateNotifyEventQueue(&id, &key);
pAudioSetNotifyEventQueue(key);
2011-11-30 16:11:42 +00:00
float out_tmp[CELL_AUDIO_BLOCK_SAMPLES * AUDIO_CHANNELS] __attribute__((aligned(16)));
while (!aud->quit_thread)
{
pSysEventQueueReceive(id, &event, SYS_NO_TIMEOUT);
2011-12-02 14:50:51 +00:00
pLwMutexLock(&aud->lock, SYS_NO_TIMEOUT);
2011-12-02 14:50:51 +00:00
if (fifo_read_avail(aud->buffer) >= sizeof(out_tmp))
fifo_read(aud->buffer, out_tmp, sizeof(out_tmp));
else
memset(out_tmp, 0, sizeof(out_tmp));
pLwMutexUnlock(&aud->lock);
pLwCondSignal(&aud->cond);
2011-12-02 14:50:51 +00:00
pAudioAddData(aud->audio_port, out_tmp, CELL_AUDIO_BLOCK_SAMPLES, 1.0);
2011-11-30 16:11:42 +00:00
}
pAudioRemoveNotifyEventQueue(key);
2012-07-01 16:22:57 +00:00
pThreadExit(0);
2011-11-30 16:11:42 +00:00
}
2011-12-02 14:50:51 +00:00
static void *ps3_audio_init(const char *device, unsigned rate, unsigned latency)
2011-11-30 16:11:42 +00:00
{
(void)latency;
(void)device;
2011-12-02 14:50:51 +00:00
(void)rate; // Always use 48kHz.
g_settings.audio.out_rate = 48000.0;
2011-11-30 16:11:42 +00:00
ps3_audio_t *data = calloc(1, sizeof(*data));
2011-12-02 14:50:51 +00:00
if (!data)
2011-11-30 16:11:42 +00:00
return NULL;
pAudioPortParam params;
pAudioInit();
params.numChannels = AUDIO_CHANNELS;
params.numBlocks = AUDIO_BLOCKS;
#ifdef HAVE_HEADSET
if(g_console.sound_mode == SOUND_MODE_HEADSET)
params.param_attrib = CELL_AUDIO_PORTATTR_OUT_SECONDARY;
else
#endif
params.param_attrib = 0;
2011-11-30 16:11:42 +00:00
if (pAudioPortOpen(&params, &data->audio_port) != CELL_OK)
2011-11-30 16:11:42 +00:00
{
pAudioQuit();
2011-12-02 14:50:51 +00:00
free(data);
2011-11-30 16:11:42 +00:00
return NULL;
}
2011-12-02 14:50:51 +00:00
data->buffer = fifo_new(CELL_AUDIO_BLOCK_SAMPLES * AUDIO_CHANNELS * AUDIO_BLOCKS * sizeof(float));
2011-11-30 16:11:42 +00:00
#ifdef __PSL1GHT__
2012-07-01 16:22:57 +00:00
sys_lwmutex_attr_t lock_attr = {SYS_LWMUTEX_ATTR_PROTOCOL, SYS_LWMUTEX_ATTR_RECURSIVE, "\0"};
sys_lwmutex_attr_t cond_lock_attr = {SYS_LWMUTEX_ATTR_PROTOCOL, SYS_LWMUTEX_ATTR_RECURSIVE, "\0"};
sys_lwcond_attribute_t cond_attr = {"\0"};
#else
sys_lwmutex_attribute_t lock_attr;
sys_lwmutex_attribute_t cond_lock_attr;
2011-12-02 15:21:01 +00:00
sys_lwcond_attribute_t cond_attr;
#endif
2011-12-02 15:21:01 +00:00
#ifndef __PSL1GHT__
pLwMutexAttributeInitialize(lock_attr);
pLwMutexAttributeInitialize(cond_lock_attr);
2011-12-02 15:21:01 +00:00
sys_lwcond_attribute_initialize(cond_attr);
#endif
2011-12-02 15:21:01 +00:00
pLwMutexCreate(&data->lock, &lock_attr);
pLwMutexCreate(&data->cond_lock, &cond_lock_attr);
pLwCondCreate(&data->cond, &data->cond_lock, &cond_attr);
2011-11-30 16:11:42 +00:00
pAudioPortStart(data->audio_port);
2012-07-01 16:22:57 +00:00
#ifdef __PSL1GHT__
pThreadCreate(&data->thread, event_loop, data, 1500, 0x1000, SYS_PPU_THREAD_CREATE_JOINABLE, (char*)"sound");
#else
pThreadCreate(&data->thread, event_loop, (uint64_t)data, 1500, 0x1000, SYS_PPU_THREAD_CREATE_JOINABLE, (char*)"sound");
#endif
2011-11-30 16:11:42 +00:00
return data;
}
2011-12-02 14:50:51 +00:00
static ssize_t ps3_audio_write(void *data, const void *buf, size_t size)
2011-11-30 16:11:42 +00:00
{
ps3_audio_t *aud = data;
if (aud->nonblocking)
{
if (fifo_write_avail(aud->buffer) < size)
return 0;
}
else
{
while (fifo_write_avail(aud->buffer) < size)
pLwCondWait(&aud->cond, 0);
2011-11-30 16:11:42 +00:00
}
pLwMutexLock(&aud->lock, SYS_NO_TIMEOUT);
2011-11-30 16:11:42 +00:00
fifo_write(aud->buffer, buf, size);
pLwMutexUnlock(&aud->lock);
2011-11-30 16:11:42 +00:00
return size;
}
2011-12-02 14:50:51 +00:00
static bool ps3_audio_stop(void *data)
2011-11-30 16:11:42 +00:00
{
2011-12-02 14:50:51 +00:00
ps3_audio_t *aud = data;
pAudioPortStop(aud->audio_port);
2011-11-30 16:11:42 +00:00
return true;
}
2011-12-02 14:50:51 +00:00
static bool ps3_audio_start(void *data)
2011-11-30 16:11:42 +00:00
{
2011-12-02 14:50:51 +00:00
ps3_audio_t *aud = data;
pAudioPortStart(aud->audio_port);
2011-12-02 14:55:49 +00:00
return true;
2011-11-30 16:11:42 +00:00
}
2011-12-02 14:50:51 +00:00
static void ps3_audio_set_nonblock_state(void *data, bool state)
2011-11-30 16:11:42 +00:00
{
ps3_audio_t *aud = data;
aud->nonblocking = state;
}
2011-12-02 14:50:51 +00:00
static void ps3_audio_free(void *data)
2011-11-30 16:11:42 +00:00
{
uint64_t val;
2011-11-30 16:11:42 +00:00
ps3_audio_t *aud = data;
aud->quit_thread = true;
pAudioPortStart(aud->audio_port);
2012-07-01 16:22:57 +00:00
pThreadJoin(aud->thread, &val);
2011-11-30 16:11:42 +00:00
pAudioPortStop(aud->audio_port);
pAudioPortClose(aud->audio_port);
pAudioQuit();
2011-11-30 16:11:42 +00:00
fifo_free(aud->buffer);
pLwMutexDestroy(&aud->lock);
pLwMutexDestroy(&aud->cond_lock);
pLwCondDestroy(&aud->cond);
2011-11-30 16:11:42 +00:00
free(data);
}
2011-12-02 14:50:51 +00:00
static bool ps3_audio_use_float(void *data)
{
(void)data;
return true;
}
2011-11-30 16:11:42 +00:00
const audio_driver_t audio_ps3 = {
2011-12-02 14:50:51 +00:00
.init = ps3_audio_init,
.write = ps3_audio_write,
.stop = ps3_audio_stop,
.start = ps3_audio_start,
.set_nonblock_state = ps3_audio_set_nonblock_state,
.use_float = ps3_audio_use_float,
.free = ps3_audio_free,
.ident = "ps3"
2011-11-30 16:11:42 +00:00
};
2011-12-02 14:50:51 +00:00