Skip to content

Commit

Permalink
Merge pull request libretro#5921 from frangarcj/rthreads_audio
Browse files Browse the repository at this point in the history
Rthreads audio
  • Loading branch information
inactive123 authored Dec 28, 2017
2 parents c7c71cb + 599d08f commit f330c0e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 123 deletions.
1 change: 1 addition & 0 deletions Makefile.psp1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ LIBS = $(WHOLE_START) -lretro_psp1 $(WHOLE_END) -lstdc++ -lpspgu -lpspgum -lm -l

ifeq ($(HAVE_THREADS), 1)
RARCH_DEFINES += -DHAVE_THREADS
LIBS += -lpthread-psp
endif

ifeq ($(HAVE_FILE_LOGGER), 1)
Expand Down
189 changes: 66 additions & 123 deletions audio/drivers/psp_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include <stdio.h>
#include <string.h>

#include <rthreads/rthreads.h>
#include <queues/fifo_queue.h>

#ifdef VITA
#include <psp2/kernel/processmgr.h>
#include <psp2/kernel/threadmgr.h>
Expand All @@ -46,29 +49,21 @@ typedef struct psp_audio
volatile uint16_t read_pos;
volatile uint16_t write_pos;

#ifdef VITA
char lock[32] __attribute__ ((aligned (8)));
char cond_lock[32] __attribute__ ((aligned (8)));
char cond[32] __attribute__ ((aligned (8)));
#else
SceUID lock;
SceUID cond;
#endif
sthread_t *worker_thread;
slock_t *fifo_lock;
scond_t *cond;
slock_t *cond_lock;

} psp_audio_t;

#define AUDIO_OUT_COUNT 512u
#define AUDIO_BUFFER_SIZE (1u<<13u)
#define AUDIO_BUFFER_SIZE_MASK (AUDIO_BUFFER_SIZE-1)

#ifdef VITA
#define PSP_THREAD_STOPPED SCE_THREAD_STOPPED
#else
#define sceKernelGetThreadInfo sceKernelReferThreadStatus
#endif

static int audioMainLoop(SceSize args, void* argp)
static void audioMainLoop(void *data)
{
psp_audio_t* psp = *((psp_audio_t**)argp);
psp_audio_t* psp = (psp_audio_t*)data;

#ifdef VITA
int port = sceAudioOutOpenPort(
SCE_AUDIO_OUT_PORT_TYPE_MAIN, AUDIO_OUT_COUNT,
Expand All @@ -83,11 +78,7 @@ static int audioMainLoop(SceSize args, void* argp)
uint16_t read_pos = psp->read_pos;
uint16_t read_pos_2 = psp->read_pos;

#ifdef VITA
sceKernelLockLwMutex((struct SceKernelLwMutexWork*)&psp->lock, 1, 0);
#else
sceKernelWaitSema(psp->lock, 1, 0);
#endif
slock_lock(psp->fifo_lock);

cond = ((uint16_t)(psp->write_pos - read_pos) & AUDIO_BUFFER_SIZE_MASK)
< (AUDIO_OUT_COUNT * 2);
Expand All @@ -99,18 +90,16 @@ static int audioMainLoop(SceSize args, void* argp)
psp->read_pos = read_pos;
}

#ifdef VITA
sceKernelUnlockLwMutex((struct SceKernelLwMutexWork*)&psp->lock, 1);
sceKernelSignalLwCond((struct SceKernelLwCondWork*)&psp->cond);
slock_unlock(psp->fifo_lock);
slock_lock(psp->cond_lock);
scond_signal(psp->cond);
slock_unlock(psp->cond_lock);

#ifdef VITA
sceAudioOutOutput(port,
cond ? (psp->zeroBuffer)
: (psp->buffer + read_pos_2));
#else
sceKernelSignalSema(psp->lock, 1);
if(!cond){
sceKernelSignalSema(psp->cond, 1);
}
sceAudioSRCOutputBlocking(PSP_AUDIO_VOLUME_MAX, cond ? (psp->zeroBuffer)
: (psp->buffer + read_pos));
#endif
Expand All @@ -120,9 +109,9 @@ static int audioMainLoop(SceSize args, void* argp)
sceAudioOutReleasePort(port);
#else
sceAudioSRCChRelease();
sceKernelExitThread(0);
#endif
return 0;

return;
}

static void *psp_audio_init(const char *device,
Expand All @@ -138,7 +127,6 @@ static void *psp_audio_init(const char *device,
(void)device;
(void)latency;


/* Cache aligned, not necessary but helpful. */
psp->buffer = (uint32_t*)
memalign(64, AUDIO_BUFFER_SIZE * sizeof(uint32_t));
Expand All @@ -150,79 +138,67 @@ static void *psp_audio_init(const char *device,
psp->read_pos = 0;
psp->write_pos = 0;
psp->rate = rate;
#if defined(VITA)

sceKernelCreateLwMutex((struct SceKernelLwMutexWork*)&psp->lock, "audio_get_lock", 0, 0, 0);
sceKernelCreateLwMutex((struct SceKernelLwMutexWork*)&psp->cond_lock, "audio_get_cond_lock", 0, 0, 0);
sceKernelCreateLwCond((struct SceKernelLwCondWork*)&psp->cond, "audio_get_cond", 0, (struct SceKernelLwMutexWork*)&psp->cond_lock, 0);
psp->thread = sceKernelCreateThread
("audioMainLoop", audioMainLoop, 0x10000100, 0x10000, 0, 0, NULL);
#else
psp->cond=sceKernelCreateSema("audio_start_sema", 0, 0, 1, NULL);
psp->lock=sceKernelCreateSema("audio_lock_sema", 0, 1, 1, NULL);
psp->thread = sceKernelCreateThread
("audioMainLoop", audioMainLoop, 0x08, 0x10000, 0, NULL);
#endif
psp->fifo_lock = slock_new();
psp->cond_lock = slock_new();
psp->cond = scond_new();

psp->nonblocking = false;
psp->running = true;
sceKernelStartThread(psp->thread, sizeof(psp_audio_t*), &psp);
psp->worker_thread = sthread_create(audioMainLoop, psp);

return psp;
}

static void psp_audio_free(void *data)
{
SceUInt timeout = 100000;
psp_audio_t* psp = (psp_audio_t*)data;
if(!psp)
return;

psp->running = false;
#if defined(VITA)
sceKernelWaitThreadEnd(psp->thread, NULL, &timeout);
sceKernelDeleteLwMutex((struct SceKernelLwMutexWork*)&psp->lock);
sceKernelDeleteLwMutex((struct SceKernelLwMutexWork*)&psp->cond_lock);
sceKernelDeleteLwCond((struct SceKernelLwCondWork*)&psp->cond);
#else
sceKernelWaitThreadEnd(psp->thread, &timeout);
sceKernelDeleteSema(psp->lock);
sceKernelDeleteSema(psp->cond);
#endif
if(psp->running){
if (psp->worker_thread)
{
psp->running = false;
sthread_join(psp->worker_thread);
}

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);
}
free(psp->buffer);
sceKernelDeleteThread(psp->thread);
psp->worker_thread = NULL;
free(psp->zeroBuffer);
free(psp);
}

static ssize_t psp_audio_write(void *data, const void *buf, size_t size)
{
psp_audio_t* psp = (psp_audio_t*)data;
psp_audio_t* psp = (psp_audio_t*)data;
uint16_t write_pos = psp->write_pos;
uint16_t sampleCount = size / sizeof(uint32_t);

if (!psp->running)
return -1;

if (psp->nonblocking)
{
//#ifdef VITA
if (AUDIO_BUFFER_SIZE - ((uint16_t)
(psp->write_pos - psp->read_pos) & AUDIO_BUFFER_SIZE_MASK) < size)
return 0;
//#endif
}

#ifdef VITA
while (AUDIO_BUFFER_SIZE - ((uint16_t)
(psp->write_pos - psp->read_pos) & AUDIO_BUFFER_SIZE_MASK) < size)
sceKernelWaitLwCond((struct SceKernelLwCondWork*)&psp->cond, 0);

sceKernelLockLwMutex((struct SceKernelLwMutexWork*)&psp->lock, 1, 0);
#else
slock_lock(psp->cond_lock);
while (AUDIO_BUFFER_SIZE - ((uint16_t)
(psp->write_pos - psp->read_pos) & AUDIO_BUFFER_SIZE_MASK) < size)
sceKernelWaitSema(psp->cond, 1, 0);
sceKernelWaitSema(psp->lock, 1, 0);
#endif
(psp->write_pos - psp->read_pos) & AUDIO_BUFFER_SIZE_MASK) < size)
scond_wait(psp->cond, psp->cond_lock);
slock_unlock(psp->cond_lock);

slock_lock(psp->fifo_lock);
if((write_pos + sampleCount) > AUDIO_BUFFER_SIZE)
{
memcpy(psp->buffer + write_pos, buf,
Expand All @@ -238,15 +214,10 @@ static ssize_t psp_audio_write(void *data, const void *buf, size_t size)
write_pos &= AUDIO_BUFFER_SIZE_MASK;
psp->write_pos = write_pos;

#ifdef VITA
sceKernelUnlockLwMutex((struct SceKernelLwMutexWork*)&psp->lock, 1);

return size;
#else
sceKernelSignalSema(psp->lock, 1);
slock_unlock(psp->fifo_lock);
return size;

return size;
#endif

}

static bool psp_audio_alive(void *data)
Expand All @@ -260,51 +231,32 @@ static bool psp_audio_alive(void *data)

static bool psp_audio_stop(void *data)
{
SceKernelThreadInfo info;
SceUInt timeout = 100000;
psp_audio_t* psp = (psp_audio_t*)data;

if(psp && !psp->running)
return true;

info.size = sizeof(SceKernelThreadInfo);

if (sceKernelGetThreadInfo(
psp->thread, &info) < 0) /* Error */
return false;
if (psp){
psp->running = false;

if (info.status == PSP_THREAD_STOPPED)
return false;
if (!psp->worker_thread)
return true;

psp->running = false;
#if defined(VITA)
sceKernelWaitThreadEnd(psp->thread, NULL, &timeout);
#else
sceKernelWaitThreadEnd(psp->thread, &timeout);
#endif
sthread_join(psp->worker_thread);
psp->worker_thread = NULL;
}
return true;
}

static bool psp_audio_start(void *data, bool is_shutdown)
{
SceKernelThreadInfo info;
psp_audio_t* psp = (psp_audio_t*)data;

if(psp && psp->running)
return true;

info.size = sizeof(SceKernelThreadInfo);

if (sceKernelGetThreadInfo(
psp->thread, &info) < 0) /* Error */
return false;

if (info.status != PSP_THREAD_STOPPED)
return false;

psp->running = true;

sceKernelStartThread(psp->thread, sizeof(psp_audio_t*), &psp);
if (!psp->worker_thread)
{
psp->running = true;
psp->worker_thread = sthread_create(audioMainLoop, psp);
}

return true;
}
Expand All @@ -327,21 +279,12 @@ static size_t psp_write_avail(void *data)
size_t val;
psp_audio_t* psp = (psp_audio_t*)data;

#ifdef VITA
sceKernelLockLwMutex((struct SceKernelLwMutexWork*)&psp->lock, 1, 0);
#else
sceKernelWaitSema(psp->lock, 1, 0);
#endif

if (!psp||!psp->running)
return 0;
slock_lock(psp->fifo_lock);
val = AUDIO_BUFFER_SIZE - ((uint16_t)
(psp->write_pos - psp->read_pos) & AUDIO_BUFFER_SIZE_MASK);

#ifdef VITA
sceKernelUnlockLwMutex((struct SceKernelLwMutexWork*)&psp->lock, 1);
#else
sceKernelSignalSema(psp->lock, 1);
#endif

slock_unlock(psp->fifo_lock);
return val;
}

Expand Down

0 comments on commit f330c0e

Please sign in to comment.