RetroArch/audio/drivers/ps3_audio.c

249 lines
5.8 KiB
C
Raw Normal View History

2012-04-21 23:13:50 +02:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 13:40:32 +01:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
2012-04-21 23:13:50 +02:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-11-30 17:11:42 +01: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 23:13:50 +02:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-11-30 17:11:42 +01: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 23:31:57 +02:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-11-30 17:11:42 +01:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
2015-09-14 03:44:06 +02:00
#include <queues/fifo_queue.h>
2019-06-17 12:49:21 +02:00
#include "../../retroarch.h"
2015-09-14 03:44:06 +02:00
#include <defines/ps3_defines.h>
2014-09-09 22:24:29 +02:00
#define AUDIO_BLOCKS 8
#define AUDIO_CHANNELS 2
2011-11-30 17:11:42 +01:00
typedef struct
{
fifo_buffer_t *buffer;
sys_ppu_thread_t thread;
2011-12-02 16:21:01 +01:00
sys_lwmutex_t lock;
sys_lwmutex_t cond_lock;
sys_lwcond_t cond;
uint32_t audio_port;
bool nonblock;
bool started;
volatile bool quit_thread;
2011-11-30 17:11:42 +01:00
} ps3_audio_t;
2020-12-20 16:41:44 +01:00
#ifdef __PSL1GHT__
2012-07-01 18:22:57 +02:00
static void event_loop(void *data)
2020-12-20 16:41:44 +01:00
#else
static void event_loop(uint64_t data)
#endif
2011-11-30 17:11:42 +01:00
{
2020-12-20 16:41:44 +01:00
float out_tmp[AUDIO_BLOCK_SAMPLES * AUDIO_CHANNELS]
2015-01-12 05:05:56 +01:00
__attribute__((aligned(16)));
2011-11-30 17:11:42 +01:00
sys_event_queue_t id;
sys_ipc_key_t key;
sys_event_t event;
2016-08-27 16:04:02 +02:00
ps3_audio_t *aud = (ps3_audio_t*)(uintptr_t)data;
2011-11-30 17:11:42 +01:00
2020-12-20 16:41:44 +01:00
audioCreateNotifyEventQueue(&id, &key);
audioSetNotifyEventQueue(key);
2011-11-30 17:11:42 +01:00
while (!aud->quit_thread)
{
2020-12-20 16:41:44 +01:00
sysEventQueueReceive(id, &event, PS3_SYS_NO_TIMEOUT);
2011-12-02 15:50:51 +01:00
2020-12-20 16:41:44 +01:00
sysLwMutexLock(&aud->lock, PS3_SYS_NO_TIMEOUT);
if (FIFO_READ_AVAIL(aud->buffer) >= sizeof(out_tmp))
2011-12-02 15:50:51 +01:00
fifo_read(aud->buffer, out_tmp, sizeof(out_tmp));
else
memset(out_tmp, 0, sizeof(out_tmp));
2020-12-20 16:41:44 +01:00
sysLwMutexUnlock(&aud->lock);
sysLwCondSignal(&aud->cond);
2011-12-02 15:50:51 +01:00
2020-12-20 16:41:44 +01:00
audioAddData(aud->audio_port, out_tmp,
AUDIO_BLOCK_SAMPLES, 1.0);
2011-11-30 17:11:42 +01:00
}
2020-12-20 16:41:44 +01:00
audioRemoveNotifyEventQueue(key);
sysThreadExit(0);
2011-11-30 17:11:42 +01:00
}
2014-09-09 22:24:29 +02:00
static void *ps3_audio_init(const char *device,
unsigned rate, unsigned latency,
unsigned block_frames,
unsigned *new_rate)
2011-11-30 17:11:42 +01:00
{
2020-12-20 16:41:44 +01:00
audioPortParam params;
ps3_audio_t *data = NULL;
#ifdef __PSL1GHT__
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_attr_t cond_attr = {"\0"};
#else
sys_lwmutex_attr_t lock_attr;
sys_lwmutex_attr_t cond_lock_attr;
sys_lwcond_attr_t cond_attr;
sys_lwmutex_attribute_initialize(lock_attr);
sys_lwmutex_attribute_initialize(cond_lock_attr);
sys_lwcond_attribute_initialize(cond_attr);
#endif
data = calloc(1, sizeof(*data));
2016-09-08 11:41:58 +02:00
if (!data)
return NULL;
2015-01-12 05:05:56 +01:00
2020-12-20 16:41:44 +01:00
audioInit();
2011-11-30 17:11:42 +01:00
2020-12-20 16:41:44 +01:00
params.numChannels = AUDIO_CHANNELS;
params.numBlocks = AUDIO_BLOCKS;
params.param_attrib = 0;
#if 0
#ifdef HAVE_HEADSET
2015-03-21 04:43:18 +01:00
if(global->console.sound.mode == SOUND_MODE_HEADSET)
2020-12-20 16:41:44 +01:00
params.param_attrib = CELL_AUDIO_PORTATTR_OUT_SECONDARY;
#endif
#endif
2011-11-30 17:11:42 +01:00
2020-12-20 16:41:44 +01:00
if (audioPortOpen(&params, &data->audio_port) != CELL_OK)
2011-11-30 17:11:42 +01:00
{
2020-12-20 16:41:44 +01:00
audioQuit();
2011-12-02 15:50:51 +01:00
free(data);
2011-11-30 17:11:42 +01:00
return NULL;
}
2020-12-20 16:41:44 +01:00
data->buffer = fifo_new(AUDIO_BLOCK_SAMPLES *
2014-09-09 22:24:29 +02:00
AUDIO_CHANNELS * AUDIO_BLOCKS * sizeof(float));
2011-11-30 17:11:42 +01:00
2020-12-20 16:41:44 +01:00
sysLwMutexCreate(&data->lock, &lock_attr);
sysLwMutexCreate(&data->cond_lock, &cond_lock_attr);
sysLwCondCreate(&data->cond, &data->cond_lock, &cond_attr);
2011-11-30 17:11:42 +01:00
2020-12-20 16:41:44 +01:00
audioPortStart(data->audio_port);
data->started = true;
2020-12-20 16:41:44 +01:00
sysThreadCreate(&data->thread, event_loop,
#ifdef __PSL1GHT__
data,
2020-12-20 16:41:44 +01:00
#else
(uint64_t)data,
#endif
1500, 0x1000, SYS_THREAD_CREATE_JOINABLE, (char*)"sound");
2011-11-30 17:11:42 +01:00
return data;
}
static ssize_t ps3_audio_write(void *data, const void *buf, size_t size)
2011-11-30 17:11:42 +01:00
{
ps3_audio_t *aud = data;
if (aud->nonblock)
2011-11-30 17:11:42 +01:00
{
if (FIFO_WRITE_AVAIL(aud->buffer) < size)
2011-11-30 17:11:42 +01:00
return 0;
}
2015-01-12 05:05:56 +01:00
while (FIFO_WRITE_AVAIL(aud->buffer) < size)
2020-12-20 16:41:44 +01:00
sysLwCondWait(&aud->cond, 0);
2011-11-30 17:11:42 +01:00
2020-12-20 16:41:44 +01:00
sysLwMutexLock(&aud->lock, PS3_SYS_NO_TIMEOUT);
2011-11-30 17:11:42 +01:00
fifo_write(aud->buffer, buf, size);
2020-12-20 16:41:44 +01:00
sysLwMutexUnlock(&aud->lock);
2015-01-12 05:05:56 +01:00
2011-11-30 17:11:42 +01:00
return size;
}
2011-12-02 15:50:51 +01:00
static bool ps3_audio_stop(void *data)
2011-11-30 17:11:42 +01:00
{
2011-12-02 15:50:51 +01:00
ps3_audio_t *aud = data;
if (aud->started)
{
2020-12-20 16:41:44 +01:00
audioPortStop(aud->audio_port);
aud->started = false;
}
2011-11-30 17:11:42 +01:00
return true;
}
static bool ps3_audio_start(void *data, bool is_shutdown)
2011-11-30 17:11:42 +01:00
{
2011-12-02 15:50:51 +01:00
ps3_audio_t *aud = data;
if (!aud->started)
{
2020-12-20 16:41:44 +01:00
audioPortStart(aud->audio_port);
aud->started = true;
}
2011-12-02 15:55:49 +01:00
return true;
2011-11-30 17:11:42 +01:00
}
static bool ps3_audio_alive(void *data)
{
ps3_audio_t *aud = data;
2015-01-12 05:05:56 +01:00
if (!aud)
return false;
return aud->started;
}
2013-11-18 10:55:28 +01:00
static void ps3_audio_set_nonblock_state(void *data, bool toggle)
2011-11-30 17:11:42 +01:00
{
ps3_audio_t *aud = data;
2015-01-12 05:05:56 +01:00
if (aud)
aud->nonblock = toggle;
2011-11-30 17:11:42 +01:00
}
2011-12-02 15:50:51 +01:00
static void ps3_audio_free(void *data)
2011-11-30 17:11:42 +01:00
{
uint64_t val;
2011-11-30 17:11:42 +01:00
ps3_audio_t *aud = data;
aud->quit_thread = true;
2017-01-26 04:26:19 +01:00
ps3_audio_start(aud, false);
2020-12-20 16:41:44 +01:00
sysThreadJoin(aud->thread, &val);
2011-11-30 17:11:42 +01:00
ps3_audio_stop(aud);
2020-12-20 16:41:44 +01:00
audioPortClose(aud->audio_port);
audioQuit();
2011-11-30 17:11:42 +01:00
fifo_free(aud->buffer);
2020-12-20 16:41:44 +01:00
sysLwMutexDestroy(&aud->lock);
sysLwMutexDestroy(&aud->cond_lock);
sysLwCondDestroy(&aud->cond);
2011-11-30 17:11:42 +01:00
free(data);
}
2011-12-02 15:50:51 +01:00
static bool ps3_audio_use_float(void *data)
{
return true;
}
static size_t ps3_audio_write_avail(void *data)
{
2020-12-20 16:41:44 +01:00
/* TODO/FIXME - implement? */
return 0;
}
2014-09-11 07:06:20 +02:00
audio_driver_t audio_ps3 = {
ps3_audio_init,
ps3_audio_write,
ps3_audio_stop,
ps3_audio_start,
ps3_audio_alive,
ps3_audio_set_nonblock_state,
ps3_audio_free,
2013-11-18 10:55:28 +01:00
ps3_audio_use_float,
"ps3",
NULL,
NULL,
ps3_audio_write_avail,
NULL
2011-11-30 17:11:42 +01:00
};