RetroArch/audio/drivers/psp1_audio.c

243 lines
5.9 KiB
C
Raw Normal View History

2014-02-17 14:26:03 +01:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 18:17:42 +01:00
* Copyright (C) 2011-2015 - Daniel De Matteis
* Copyright (C) 2014-2015 - Ali Bouhlel
*
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-01-12 05:05:56 +01:00
#include "../../general.h"
#include "../../driver.h"
2014-02-17 14:26:03 +01:00
2014-02-17 16:30:17 +01:00
#include <pspkernel.h>
2014-02-17 14:26:03 +01:00
#include <pspaudio.h>
2014-02-17 16:30:17 +01:00
#include <stdint.h>
2014-02-17 14:26:03 +01:00
typedef struct psp1_audio
2014-02-17 14:26:03 +01:00
{
bool nonblocking;
2014-02-17 16:30:17 +01:00
uint32_t* buffer;
uint32_t* zeroBuffer;
SceUID thread;
int rate;
volatile bool running;
volatile uint16_t readPos;
volatile uint16_t writePos;
2014-02-17 14:26:03 +01:00
} psp1_audio_t;
#define AUDIO_OUT_COUNT 512u
#define AUDIO_BUFFER_SIZE (1u<<13u)
2014-02-17 14:26:03 +01:00
#define AUDIO_BUFFER_SIZE_MASK (AUDIO_BUFFER_SIZE-1)
static int audioMainLoop(SceSize args, void* argp)
2014-02-17 17:04:32 +01:00
{
psp1_audio_t* psp = *((psp1_audio_t**)argp);
2014-02-17 17:04:32 +01:00
sceAudioSRCChReserve(AUDIO_OUT_COUNT, psp->rate, 2);
2014-02-17 17:04:32 +01:00
while (psp->running)
2014-02-17 17:04:32 +01:00
{
2014-09-09 22:24:29 +02:00
/* Get a non-volatile copy. */
uint16_t readPos = psp->readPos;
2014-09-09 22:24:29 +02:00
if (((uint16_t)(psp->writePos - readPos) & AUDIO_BUFFER_SIZE_MASK)
< (AUDIO_OUT_COUNT * 2))
2014-02-17 17:04:32 +01:00
sceAudioSRCOutputBlocking(PSP_AUDIO_VOLUME_MAX, psp->zeroBuffer);
else
{
2014-09-09 22:24:29 +02:00
sceAudioSRCOutputBlocking(PSP_AUDIO_VOLUME_MAX,
psp->buffer + readPos);
readPos += AUDIO_OUT_COUNT;
readPos &= AUDIO_BUFFER_SIZE_MASK;
psp->readPos = readPos;
2014-02-17 17:04:32 +01:00
}
}
sceAudioSRCChRelease();
sceKernelExitThread(0);
return 0;
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,
unsigned rate, unsigned latency)
2014-02-17 14:26:03 +01:00
{
2015-04-09 07:01:31 +02:00
psp1_audio_t *psp = (psp1_audio_t*)calloc(1, sizeof(psp1_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;
2014-09-09 22:24:29 +02:00
/* Cache aligned, not necessary but helpful. */
psp->buffer = (uint32_t*)
memalign(64, AUDIO_BUFFER_SIZE * sizeof(uint32_t));
memset(psp->buffer, 0, AUDIO_BUFFER_SIZE * sizeof(uint32_t));
2014-09-09 22:24:29 +02:00
psp->zeroBuffer = (uint32_t*)
memalign(64, AUDIO_OUT_COUNT * sizeof(uint32_t));
memset(psp->zeroBuffer, 0, AUDIO_OUT_COUNT * sizeof(uint32_t));
2014-02-17 17:04:32 +01:00
psp->readPos = 0;
psp->writePos = 0;
psp->rate = rate;
2014-09-09 22:24:29 +02:00
psp->thread = sceKernelCreateThread
("audioMainLoop", audioMainLoop, 0x08, 0x10000, 0, NULL);
psp->nonblocking = false;
2014-02-17 17:04:32 +01:00
psp->running = true;
sceKernelStartThread(psp->thread, sizeof(psp1_audio_t*), &psp);
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-01-12 05:05:56 +01:00
SceUInt timeout = 100000;
2014-02-17 17:04:32 +01:00
psp1_audio_t* psp = (psp1_audio_t*)data;
if(!psp)
return;
2014-02-17 17:04:32 +01:00
2015-01-12 05:05:56 +01:00
psp->running = false;
sceKernelWaitThreadEnd(psp->thread, &timeout);
2014-02-17 17:04:32 +01:00
sceKernelDeleteThread(psp->thread);
free(psp->buffer);
free(psp->zeroBuffer);
free(psp);
2014-02-17 14:26:03 +01:00
}
static ssize_t psp_audio_write(void *data, const void *buf, size_t size)
{
2014-02-17 16:30:17 +01:00
uint16_t sampleCount;
2014-02-17 17:04:32 +01:00
psp1_audio_t* psp = (psp1_audio_t*)data;
uint16_t writePos = psp->writePos;
2014-02-17 17:04:32 +01:00
sampleCount= size / sizeof(uint32_t);
2014-09-09 22:24:29 +02:00
#if 0
if (psp->nonblocking)
{
/* TODO */
}
#endif
if((writePos + sampleCount) > AUDIO_BUFFER_SIZE)
2014-02-17 17:04:32 +01:00
{
2014-09-09 22:24:29 +02:00
memcpy(psp->buffer + writePos, buf,
(AUDIO_BUFFER_SIZE - writePos) * sizeof(uint32_t));
memcpy(psp->buffer, (uint32_t*) buf +
(AUDIO_BUFFER_SIZE - writePos),
(writePos + sampleCount - AUDIO_BUFFER_SIZE) * sizeof(uint32_t));
2014-02-17 14:26:03 +01:00
}
2014-02-17 17:04:32 +01:00
else
memcpy(psp->buffer + writePos, buf, size);
writePos += sampleCount;
writePos &= AUDIO_BUFFER_SIZE_MASK;
psp->writePos = writePos;
2014-02-17 16:30:17 +01:00
return sampleCount;
2014-02-17 14:26:03 +01:00
}
static bool psp_audio_alive(void *data)
{
psp1_audio_t* psp = (psp1_audio_t*)data;
2015-01-12 05:05:56 +01:00
if (!psp)
return false;
return psp->running;
}
2014-02-17 14:26:03 +01:00
static bool psp_audio_stop(void *data)
{
SceKernelThreadRunStatus runStatus;
2015-04-09 07:01:31 +02:00
SceUInt timeout = 100000;
2014-02-17 17:04:32 +01:00
psp1_audio_t* psp = (psp1_audio_t*)data;
2015-04-09 07:01:31 +02:00
runStatus.size = sizeof(SceKernelThreadRunStatus);
2014-09-09 22:24:29 +02:00
if (sceKernelReferThreadRunStatus(
psp->thread, &runStatus) < 0) /* Error */
return false;
if (runStatus.status == PSP_THREAD_STOPPED)
return false;
psp->running = false;
sceKernelWaitThreadEnd(psp->thread, &timeout);
2014-02-17 17:21:41 +01:00
return true;
2014-02-17 14:26:03 +01:00
}
static bool psp_audio_start(void *data)
{
SceKernelThreadRunStatus runStatus;
2014-02-17 17:04:32 +01:00
psp1_audio_t* psp = (psp1_audio_t*)data;
2015-04-09 07:01:31 +02:00
runStatus.size = sizeof(SceKernelThreadRunStatus);
2014-09-09 22:24:29 +02:00
if (sceKernelReferThreadRunStatus(
psp->thread, &runStatus) < 0) /* Error */
return false;
if (runStatus.status != PSP_THREAD_STOPPED)
return false;
psp->running = true;
sceKernelStartThread(psp->thread, sizeof(psp1_audio_t*), &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
{
2014-02-17 17:04:32 +01:00
psp1_audio_t* psp = (psp1_audio_t*)data;
2015-01-12 05:05:56 +01:00
if (psp)
psp->nonblocking = toggle;
2014-02-17 14:26:03 +01:00
}
static bool psp_audio_use_float(void *data)
{
(void)data;
return false;
}
static size_t psp_write_avail(void *data)
{
/* TODO */
psp1_audio_t* psp = (psp1_audio_t*)data;
2014-09-09 22:24:29 +02:00
return AUDIO_BUFFER_SIZE - ((uint16_t)
(psp->writePos - psp->readPos) & AUDIO_BUFFER_SIZE_MASK);
}
static size_t psp_buffer_size(void *data)
{
/* TODO */
return AUDIO_BUFFER_SIZE;
}
2014-09-11 07:06:20 +02:00
audio_driver_t audio_psp1 = {
2014-02-17 14:26:03 +01:00
psp_audio_init,
psp_audio_write,
psp_audio_stop,
psp_audio_start,
psp_audio_alive,
2014-02-17 14:26:03 +01:00
psp_audio_set_nonblock_state,
psp_audio_free,
psp_audio_use_float,
"psp1",
psp_write_avail,
psp_buffer_size,
2014-02-17 14:26:03 +01:00
};