2014-02-17 14:26:03 +01:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2017-01-22 13:40:32 +01:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
|
|
|
* Copyright (C) 2014-2017 - Ali Bouhlel
|
2014-02-22 02:57:17 +01:00
|
|
|
*
|
2014-02-17 14:26:03 +01:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* 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 RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-09-14 03:44:06 +02:00
|
|
|
#include <stdint.h>
|
2018-12-31 00:37:08 +01:00
|
|
|
#if defined(VITA) || defined(PSP)
|
2015-09-14 03:44:06 +02:00
|
|
|
#include <malloc.h>
|
2018-12-31 00:37:08 +01:00
|
|
|
#endif
|
2016-08-02 01:38:05 +02:00
|
|
|
#include <stdio.h>
|
2017-01-17 18:12:23 +01:00
|
|
|
#include <string.h>
|
2014-02-17 14:26:03 +01:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
#include <rthreads/rthreads.h>
|
|
|
|
#include <queues/fifo_queue.h>
|
|
|
|
|
2018-12-31 00:37:08 +01:00
|
|
|
#if defined(VITA)
|
2015-08-12 12:18:45 +02:00
|
|
|
#include <psp2/kernel/processmgr.h>
|
|
|
|
#include <psp2/kernel/threadmgr.h>
|
|
|
|
#include <psp2/kernel/sysmem.h>
|
|
|
|
#include <psp2/audioout.h>
|
2018-12-31 00:37:08 +01:00
|
|
|
#elif defined(PSP)
|
2014-02-17 16:30:17 +01:00
|
|
|
#include <pspkernel.h>
|
2014-02-17 14:26:03 +01:00
|
|
|
#include <pspaudio.h>
|
2018-12-31 00:37:08 +01:00
|
|
|
#elif defined(ORBIS)
|
|
|
|
#include <audioout.h>
|
|
|
|
#define SCE_AUDIO_OUT_PORT_TYPE_MAIN 0
|
|
|
|
#define SCE_AUDIO_OUT_MODE_STEREO 1
|
|
|
|
#define SceUID uint32_t
|
2015-08-12 12:18:45 +02:00
|
|
|
#endif
|
2015-09-14 03:44:06 +02:00
|
|
|
|
2019-06-17 12:49:21 +02:00
|
|
|
#include "../../retroarch.h"
|
2014-02-17 14:26:03 +01:00
|
|
|
|
2015-08-12 12:18:45 +02:00
|
|
|
typedef struct psp_audio
|
2014-02-17 14:26:03 +01:00
|
|
|
{
|
2020-01-04 10:32:03 +01:00
|
|
|
bool nonblock;
|
2016-08-02 01:38:05 +02:00
|
|
|
|
2017-12-26 16:50:33 +01:00
|
|
|
uint32_t* buffer;
|
|
|
|
uint32_t* zeroBuffer;
|
2014-02-22 19:23:40 +01:00
|
|
|
|
2017-12-26 16:50:33 +01:00
|
|
|
SceUID thread;
|
|
|
|
int rate;
|
2017-12-16 18:01:47 +01:00
|
|
|
|
2014-02-22 19:23:40 +01:00
|
|
|
volatile bool running;
|
2017-12-26 16:50:33 +01:00
|
|
|
volatile uint16_t read_pos;
|
|
|
|
volatile uint16_t write_pos;
|
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
sthread_t *worker_thread;
|
|
|
|
slock_t *fifo_lock;
|
|
|
|
scond_t *cond;
|
|
|
|
slock_t *cond_lock;
|
2016-09-08 16:11:44 +02:00
|
|
|
|
2015-08-12 12:18:45 +02:00
|
|
|
} psp_audio_t;
|
2014-02-17 14:26:03 +01:00
|
|
|
|
2014-07-14 02:33:18 +01:00
|
|
|
#define AUDIO_OUT_COUNT 512u
|
|
|
|
#define AUDIO_BUFFER_SIZE (1u<<13u)
|
2017-12-26 16:50:33 +01:00
|
|
|
#define AUDIO_BUFFER_SIZE_MASK (AUDIO_BUFFER_SIZE-1)
|
2015-09-16 10:39:30 +02:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
static void audioMainLoop(void *data)
|
2014-02-17 17:04:32 +01:00
|
|
|
{
|
2017-12-16 18:01:47 +01:00
|
|
|
psp_audio_t* psp = (psp_audio_t*)data;
|
|
|
|
|
2018-12-31 00:37:08 +01:00
|
|
|
#if defined(VITA)
|
2016-08-27 17:05:49 +02:00
|
|
|
int port = sceAudioOutOpenPort(
|
|
|
|
SCE_AUDIO_OUT_PORT_TYPE_MAIN, AUDIO_OUT_COUNT,
|
|
|
|
psp->rate, SCE_AUDIO_OUT_MODE_STEREO);
|
2018-12-31 00:37:08 +01:00
|
|
|
#elif defined(ORBIS)
|
|
|
|
int port = sceAudioOutOpen(0xff,
|
|
|
|
SCE_AUDIO_OUT_PORT_TYPE_MAIN, 0, AUDIO_OUT_COUNT,
|
|
|
|
psp->rate, SCE_AUDIO_OUT_MODE_STEREO);
|
2015-08-12 12:18:45 +02:00
|
|
|
#else
|
2014-02-22 02:57:17 +01:00
|
|
|
sceAudioSRCChReserve(AUDIO_OUT_COUNT, psp->rate, 2);
|
2015-08-12 12:18:45 +02:00
|
|
|
#endif
|
2014-02-17 17:04:32 +01:00
|
|
|
|
2014-02-22 02:57:17 +01:00
|
|
|
while (psp->running)
|
2014-02-17 17:04:32 +01:00
|
|
|
{
|
2017-12-26 16:50:33 +01:00
|
|
|
bool cond = false;
|
|
|
|
uint16_t read_pos = psp->read_pos;
|
|
|
|
uint16_t read_pos_2 = psp->read_pos;
|
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
slock_lock(psp->fifo_lock);
|
|
|
|
|
2017-12-26 16:50:33 +01:00
|
|
|
cond = ((uint16_t)(psp->write_pos - read_pos) & AUDIO_BUFFER_SIZE_MASK)
|
|
|
|
< (AUDIO_OUT_COUNT * 2);
|
|
|
|
|
|
|
|
if (!cond)
|
|
|
|
{
|
|
|
|
read_pos += AUDIO_OUT_COUNT;
|
|
|
|
read_pos &= AUDIO_BUFFER_SIZE_MASK;
|
|
|
|
psp->read_pos = read_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
slock_unlock(psp->fifo_lock);
|
|
|
|
slock_lock(psp->cond_lock);
|
|
|
|
scond_signal(psp->cond);
|
|
|
|
slock_unlock(psp->cond_lock);
|
2016-08-27 17:05:49 +02:00
|
|
|
|
2018-12-31 00:37:08 +01:00
|
|
|
#if defined(VITA) || defined(ORBIS)
|
2017-12-26 16:50:33 +01:00
|
|
|
sceAudioOutOutput(port,
|
|
|
|
cond ? (psp->zeroBuffer)
|
|
|
|
: (psp->buffer + read_pos_2));
|
2017-12-15 13:35:10 +01:00
|
|
|
#else
|
2017-12-26 16:50:33 +01:00
|
|
|
sceAudioSRCOutputBlocking(PSP_AUDIO_VOLUME_MAX, cond ? (psp->zeroBuffer)
|
|
|
|
: (psp->buffer + read_pos));
|
2016-08-02 01:38:05 +02:00
|
|
|
#endif
|
2014-02-17 17:04:32 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 00:37:08 +01:00
|
|
|
#if defined(VITA)
|
2015-08-12 14:47:39 +02:00
|
|
|
sceAudioOutReleasePort(port);
|
2018-12-31 00:37:08 +01:00
|
|
|
#elif defined(ORBIS)
|
|
|
|
sceAudioOutClose(port);
|
2015-08-12 14:47:39 +02:00
|
|
|
#else
|
2014-02-22 02:57:17 +01:00
|
|
|
sceAudioSRCChRelease();
|
2016-07-31 09:59:17 +02:00
|
|
|
#endif
|
2017-12-16 18:01:47 +01:00
|
|
|
|
|
|
|
return;
|
2014-02-17 14:26:03 +01:00
|
|
|
}
|
2014-02-17 17:04:32 +01:00
|
|
|
|
2014-09-09 22:24:29 +02:00
|
|
|
static void *psp_audio_init(const char *device,
|
2017-01-11 07:25:42 +01:00
|
|
|
unsigned rate, unsigned latency,
|
|
|
|
unsigned block_frames,
|
|
|
|
unsigned *new_rate)
|
2014-02-17 14:26:03 +01:00
|
|
|
{
|
2015-08-12 12:18:45 +02:00
|
|
|
psp_audio_t *psp = (psp_audio_t*)calloc(1, sizeof(psp_audio_t));
|
2014-02-17 17:04:32 +01:00
|
|
|
|
|
|
|
if (!psp)
|
|
|
|
return NULL;
|
|
|
|
|
2015-04-09 07:01:31 +02:00
|
|
|
(void)device;
|
|
|
|
(void)latency;
|
|
|
|
|
2018-12-31 12:32:21 +01:00
|
|
|
#ifdef ORBIS
|
|
|
|
psp->buffer = (uint32_t*)
|
|
|
|
malloc(AUDIO_BUFFER_SIZE * sizeof(uint32_t));
|
|
|
|
#else
|
2017-12-26 16:50:33 +01:00
|
|
|
/* Cache aligned, not necessary but helpful. */
|
|
|
|
psp->buffer = (uint32_t*)
|
|
|
|
memalign(64, AUDIO_BUFFER_SIZE * sizeof(uint32_t));
|
2018-12-31 12:32:21 +01:00
|
|
|
#endif
|
2017-12-26 16:50:33 +01:00
|
|
|
memset(psp->buffer, 0, AUDIO_BUFFER_SIZE * sizeof(uint32_t));
|
2018-12-31 12:32:21 +01:00
|
|
|
|
|
|
|
#ifdef ORBIS
|
|
|
|
psp->zeroBuffer = (uint32_t*)
|
|
|
|
malloc(AUDIO_OUT_COUNT * sizeof(uint32_t));
|
|
|
|
#else
|
2017-12-26 16:50:33 +01:00
|
|
|
psp->zeroBuffer = (uint32_t*)
|
|
|
|
memalign(64, AUDIO_OUT_COUNT * sizeof(uint32_t));
|
2018-12-31 12:32:21 +01:00
|
|
|
#endif
|
2017-12-26 16:50:33 +01:00
|
|
|
memset(psp->zeroBuffer, 0, AUDIO_OUT_COUNT * sizeof(uint32_t));
|
|
|
|
|
|
|
|
psp->read_pos = 0;
|
|
|
|
psp->write_pos = 0;
|
|
|
|
psp->rate = rate;
|
2016-08-02 01:38:05 +02:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
psp->fifo_lock = slock_new();
|
|
|
|
psp->cond_lock = slock_new();
|
|
|
|
psp->cond = scond_new();
|
2014-02-17 17:04:32 +01:00
|
|
|
|
2020-01-04 10:32:03 +01:00
|
|
|
psp->nonblock = false;
|
2014-02-22 02:57:17 +01:00
|
|
|
psp->running = true;
|
2017-12-16 18:01:47 +01:00
|
|
|
psp->worker_thread = sthread_create(audioMainLoop, psp);
|
2014-02-22 02:57:17 +01:00
|
|
|
|
2014-02-17 17:04:32 +01:00
|
|
|
return psp;
|
2014-02-17 14:26:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void psp_audio_free(void *data)
|
|
|
|
{
|
2015-08-12 12:18:45 +02:00
|
|
|
psp_audio_t* psp = (psp_audio_t*)data;
|
2014-02-21 19:24:46 +01:00
|
|
|
if(!psp)
|
|
|
|
return;
|
2014-02-17 17:04:32 +01:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
if(psp->running){
|
|
|
|
if (psp->worker_thread)
|
|
|
|
{
|
|
|
|
psp->running = false;
|
|
|
|
sthread_join(psp->worker_thread);
|
|
|
|
}
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
if (psp->cond)
|
|
|
|
scond_free(psp->cond);
|
|
|
|
if (psp->fifo_lock)
|
|
|
|
slock_free(psp->fifo_lock);
|
|
|
|
if (psp->cond_lock)
|
|
|
|
slock_free(psp->cond_lock);
|
|
|
|
}
|
2017-12-26 16:50:33 +01:00
|
|
|
free(psp->buffer);
|
|
|
|
psp->worker_thread = NULL;
|
|
|
|
free(psp->zeroBuffer);
|
2014-02-21 19:24:46 +01:00
|
|
|
free(psp);
|
2014-02-17 14:26:03 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 10:15:11 +02:00
|
|
|
static ssize_t psp_audio_write(void *data, const void *buf, size_t size)
|
2014-02-17 14:26:03 +01:00
|
|
|
{
|
2020-01-04 10:32:03 +01:00
|
|
|
psp_audio_t* psp = (psp_audio_t*)data;
|
2017-12-26 16:50:33 +01:00
|
|
|
uint16_t write_pos = psp->write_pos;
|
|
|
|
uint16_t sampleCount = size / sizeof(uint32_t);
|
2019-02-03 15:49:35 -08:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
if (!psp->running)
|
|
|
|
return -1;
|
2016-09-08 16:11:44 +02:00
|
|
|
|
2020-01-04 10:32:03 +01:00
|
|
|
if (psp->nonblock)
|
2016-08-02 01:38:05 +02:00
|
|
|
{
|
2017-12-26 16:50:33 +01:00
|
|
|
if (AUDIO_BUFFER_SIZE - ((uint16_t)
|
2020-01-04 10:32:03 +01:00
|
|
|
(psp->write_pos - psp->read_pos) & AUDIO_BUFFER_SIZE_MASK) < size)
|
|
|
|
return 0;
|
2017-12-26 16:50:33 +01:00
|
|
|
}
|
2016-08-02 01:38:05 +02:00
|
|
|
|
2017-12-26 16:50:33 +01:00
|
|
|
slock_lock(psp->cond_lock);
|
|
|
|
while (AUDIO_BUFFER_SIZE - ((uint16_t)
|
|
|
|
(psp->write_pos - psp->read_pos) & AUDIO_BUFFER_SIZE_MASK) < size)
|
|
|
|
scond_wait(psp->cond, psp->cond_lock);
|
|
|
|
slock_unlock(psp->cond_lock);
|
2016-08-27 17:05:49 +02:00
|
|
|
|
2017-12-26 16:50:33 +01:00
|
|
|
slock_lock(psp->fifo_lock);
|
|
|
|
if((write_pos + sampleCount) > AUDIO_BUFFER_SIZE)
|
2017-12-16 18:01:47 +01:00
|
|
|
{
|
2017-12-26 16:50:33 +01:00
|
|
|
memcpy(psp->buffer + write_pos, buf,
|
|
|
|
(AUDIO_BUFFER_SIZE - write_pos) * sizeof(uint32_t));
|
|
|
|
memcpy(psp->buffer, (uint32_t*) buf +
|
|
|
|
(AUDIO_BUFFER_SIZE - write_pos),
|
|
|
|
(write_pos + sampleCount - AUDIO_BUFFER_SIZE) * sizeof(uint32_t));
|
2017-12-16 18:01:47 +01:00
|
|
|
}
|
2017-12-26 16:50:33 +01:00
|
|
|
else
|
|
|
|
memcpy(psp->buffer + write_pos, buf, size);
|
|
|
|
|
|
|
|
write_pos += sampleCount;
|
|
|
|
write_pos &= AUDIO_BUFFER_SIZE_MASK;
|
|
|
|
psp->write_pos = write_pos;
|
|
|
|
|
|
|
|
slock_unlock(psp->fifo_lock);
|
|
|
|
return size;
|
|
|
|
|
2014-02-17 14:26:03 +01:00
|
|
|
}
|
|
|
|
|
2014-10-01 21:42:19 +02:00
|
|
|
static bool psp_audio_alive(void *data)
|
|
|
|
{
|
2015-08-12 12:18:45 +02:00
|
|
|
psp_audio_t* psp = (psp_audio_t*)data;
|
2015-01-12 05:05:56 +01:00
|
|
|
if (!psp)
|
|
|
|
return false;
|
|
|
|
return psp->running;
|
2014-10-01 21:42:19 +02:00
|
|
|
}
|
|
|
|
|
2014-02-17 14:26:03 +01:00
|
|
|
static bool psp_audio_stop(void *data)
|
|
|
|
{
|
2015-08-12 12:18:45 +02:00
|
|
|
psp_audio_t* psp = (psp_audio_t*)data;
|
2014-02-22 02:57:17 +01:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
if (psp){
|
|
|
|
psp->running = false;
|
2015-08-30 16:12:45 +02:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
if (!psp->worker_thread)
|
|
|
|
return true;
|
2014-02-22 02:57:17 +01:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
sthread_join(psp->worker_thread);
|
|
|
|
psp->worker_thread = NULL;
|
|
|
|
}
|
2014-02-17 17:21:41 +01:00
|
|
|
return true;
|
2014-02-17 14:26:03 +01:00
|
|
|
}
|
|
|
|
|
2017-01-22 18:05:07 +01:00
|
|
|
static bool psp_audio_start(void *data, bool is_shutdown)
|
2014-02-17 14:26:03 +01:00
|
|
|
{
|
2015-09-16 10:39:30 +02:00
|
|
|
psp_audio_t* psp = (psp_audio_t*)data;
|
2017-12-11 23:55:31 -08:00
|
|
|
|
|
|
|
if(psp && psp->running)
|
2017-04-28 00:29:51 +02:00
|
|
|
return true;
|
2015-08-30 16:12:45 +02:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
if (!psp->worker_thread)
|
|
|
|
{
|
|
|
|
psp->running = true;
|
|
|
|
psp->worker_thread = sthread_create(audioMainLoop, psp);
|
|
|
|
}
|
2014-02-17 17:21:41 +01:00
|
|
|
|
|
|
|
return true;
|
2014-02-17 14:26:03 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 17:21:41 +01:00
|
|
|
static void psp_audio_set_nonblock_state(void *data, bool toggle)
|
2014-02-17 14:26:03 +01:00
|
|
|
{
|
2015-08-12 12:18:45 +02:00
|
|
|
psp_audio_t* psp = (psp_audio_t*)data;
|
2015-01-12 05:05:56 +01:00
|
|
|
if (psp)
|
2020-01-04 10:32:03 +01:00
|
|
|
psp->nonblock = toggle;
|
2014-02-17 14:26:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool psp_audio_use_float(void *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-21 19:24:46 +01:00
|
|
|
static size_t psp_write_avail(void *data)
|
|
|
|
{
|
2016-08-02 01:38:05 +02:00
|
|
|
size_t val;
|
2017-04-27 00:44:35 +02:00
|
|
|
psp_audio_t* psp = (psp_audio_t*)data;
|
2016-08-02 01:38:05 +02:00
|
|
|
|
2017-12-16 18:01:47 +01:00
|
|
|
if (!psp||!psp->running)
|
|
|
|
return 0;
|
|
|
|
slock_lock(psp->fifo_lock);
|
2017-12-26 16:50:33 +01:00
|
|
|
val = AUDIO_BUFFER_SIZE - ((uint16_t)
|
|
|
|
(psp->write_pos - psp->read_pos) & AUDIO_BUFFER_SIZE_MASK);
|
2017-12-16 18:01:47 +01:00
|
|
|
slock_unlock(psp->fifo_lock);
|
2017-04-27 08:49:21 +02:00
|
|
|
return val;
|
2014-02-21 19:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t psp_buffer_size(void *data)
|
|
|
|
{
|
|
|
|
/* TODO */
|
2016-08-02 01:38:05 +02:00
|
|
|
return AUDIO_BUFFER_SIZE /** sizeof(uint32_t)*/;
|
2014-02-21 19:24:46 +01:00
|
|
|
}
|
|
|
|
|
2015-08-12 12:18:45 +02:00
|
|
|
audio_driver_t audio_psp = {
|
2014-02-17 14:26:03 +01:00
|
|
|
psp_audio_init,
|
|
|
|
psp_audio_write,
|
|
|
|
psp_audio_stop,
|
|
|
|
psp_audio_start,
|
2014-10-01 21:42:19 +02:00
|
|
|
psp_audio_alive,
|
2014-02-17 14:26:03 +01:00
|
|
|
psp_audio_set_nonblock_state,
|
|
|
|
psp_audio_free,
|
|
|
|
psp_audio_use_float,
|
2018-12-31 00:37:08 +01:00
|
|
|
#if defined(VITA)
|
2015-08-31 15:26:37 +02:00
|
|
|
"vita",
|
2018-12-31 00:37:08 +01:00
|
|
|
#elif defined(ORBIS)
|
|
|
|
"orbis",
|
2015-08-31 15:26:37 +02:00
|
|
|
#else
|
2015-08-12 12:18:45 +02:00
|
|
|
"psp",
|
2015-08-31 15:26:37 +02:00
|
|
|
#endif
|
2016-04-26 17:55:20 +02:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2014-02-22 19:23:40 +01:00
|
|
|
psp_write_avail,
|
2016-08-02 01:38:05 +02:00
|
|
|
psp_buffer_size
|
2014-02-17 14:26:03 +01:00
|
|
|
};
|