ps2, more audio fixes

This commit is contained in:
kub 2024-02-17 19:37:11 +01:00
parent fd604aa720
commit e2b9687b3f

View file

@ -78,15 +78,16 @@ static int32_t vsync_callback_id;
static uint8_t vsync; /* 0 (Disabled), 1 (Enabled), 2 (Dynamic) */ static uint8_t vsync; /* 0 (Disabled), 1 (Enabled), 2 (Dynamic) */
/* sound stuff */ /* sound stuff */
#define SOUND_BLOCK_COUNT 6 #define SOUND_BLOCK_COUNT 7
#define SOUND_BUFFER_CHUNK (2*54000/50) // max.rate/min.frames, stereo #define SOUND_BUFFER_CHUNK (2*54000/50) // max.rate/min.frames in stereo
static short __attribute__((aligned(4))) sndBuffer[SOUND_BUFFER_CHUNK*SOUND_BLOCK_COUNT]; static short __attribute__((aligned(4))) sndBuffer[SOUND_BUFFER_CHUNK*SOUND_BLOCK_COUNT];
static short __attribute__((aligned(4))) nulBuffer[SOUND_BUFFER_CHUNK]; static short __attribute__((aligned(4))) nulBuffer[SOUND_BUFFER_CHUNK];
static short *snd_playptr = NULL, *sndBuffer_endptr = NULL; static short *snd_playptr = NULL, *sndBuffer_endptr = NULL;
static int samples_made = 0, samples_done = 0, samples_block = 0; static int samples_made, samples_done, samples_block;
static int sound_thread_exit = 0;
static int32_t sound_sem = -1; static int sound_thread_exit = 0, sound_stopped = 1;
static int32_t sound_sem = -1, sound_mutex = -1;
static uint8_t stack[0x4000] __attribute__((aligned(16))); static uint8_t stack[0x4000] __attribute__((aligned(16)));
extern void *_gp; extern void *_gp;
@ -113,22 +114,41 @@ static void writeSound(int len)
{ {
int ret, l; int ret, l;
if (samples_made - samples_done < samples_block * (SOUND_BLOCK_COUNT-3)) {
samples_made += len / 2;
PicoIn.sndOut += len / 2; PicoIn.sndOut += len / 2;
} else
lprintf("ovfl %d\n", samples_made - samples_done);
if (sndBuffer_endptr < PicoIn.sndOut)
sndBuffer_endptr = PicoIn.sndOut;
l = PicoIn.sndOut - sndBuffer; l = PicoIn.sndOut - sndBuffer;
if (l > sizeof(sndBuffer)/2) if (l > sizeof(sndBuffer)/2)
lprintf("ovfl %d %d\n", len, PicoIn.sndOut - sndBuffer); lprintf("ovrn %d %d\n", len, PicoIn.sndOut - sndBuffer);
if (l > samples_block * (SOUND_BLOCK_COUNT-2)) { if (l > samples_block * (SOUND_BLOCK_COUNT-2)) {
sndBuffer_endptr = PicoIn.sndOut; sndBuffer_endptr = PicoIn.sndOut;
PicoIn.sndOut = sndBuffer; PicoIn.sndOut = sndBuffer;
} }
if (sndBuffer_endptr < PicoIn.sndOut)
sndBuffer_endptr = PicoIn.sndOut;
samples_made += len / 2;
// signal the snd thread // signal the snd thread
// ret = SignalSema(sound_sem); SignalSema(sound_sem);
// if (ret < 0) lprintf("snd signal ret %08x\n", ret); }
static void resetSound()
{
struct audsrv_fmt_t format;
int stereo = (PicoIn.opt&8)>>3;
int ret;
format.bits = 16;
format.freq = PicoIn.sndRate;
format.channels = stereo ? 2 : 1;
ret = audsrv_set_format(&format);
if (ret < 0) {
lprintf("audsrv_set_format() failed: %i\n", ret);
emu_status_msg("sound init failed (%i), snd disabled", ret);
currentConfig.EmuOpt &= ~EOPT_EN_SOUND;
}
} }
static int sound_thread(void *argp) static int sound_thread(void *argp)
@ -137,26 +157,11 @@ static int sound_thread(void *argp)
while (!sound_thread_exit) while (!sound_thread_exit)
{ {
int ret = 0; int ret = WaitSema(sound_mutex);
if (ret < 0) lprintf("sthr: WaitSema mutex failed (%d)\n", ret);
// curb the sample queue to prevent it from filling
while (samples_made - samples_done > 4*samples_block) {
short *sndOut = PicoIn.sndOut, *sndEnd = sndBuffer_endptr;
int buflen = sndEnd - snd_playptr;
if (sndOut > snd_playptr)
buflen = sndOut - snd_playptr;
if (buflen > samples_made - samples_done - 4*samples_block)
buflen = samples_made - samples_done - 4*samples_block;
samples_done += buflen;
snd_playptr += buflen;
if (snd_playptr >= sndBuffer_endptr)
snd_playptr -= sndBuffer_endptr - sndBuffer;
}
// queue samples to audsrv, minimum 2 frames // queue samples to audsrv, minimum 2 frames
// if there aren't enough samlpes, queue silence // if there aren't enough samples, queue silence
int queued = audsrv_queued()/2; int queued = audsrv_queued()/2;
while (queued < 2*samples_block) { while (queued < 2*samples_block) {
short *sndOut = PicoIn.sndOut, *sndEnd = sndBuffer_endptr; short *sndOut = PicoIn.sndOut, *sndEnd = sndBuffer_endptr;
@ -167,8 +172,8 @@ static int sound_thread(void *argp)
buflen = sndOut - snd_playptr; buflen = sndOut - snd_playptr;
if (buflen > samples_made - samples_done) if (buflen > samples_made - samples_done)
buflen = samples_made - samples_done; buflen = samples_made - samples_done;
if (buflen > 4*samples_block - queued) if (buflen > 3*samples_block - queued)
buflen = 4*samples_block - queued; buflen = 3*samples_block - queued;
// play audio // play audio
if (buflen > 0) { if (buflen > 0) {
@ -179,17 +184,23 @@ static int sound_thread(void *argp)
if (snd_playptr >= sndBuffer_endptr) if (snd_playptr >= sndBuffer_endptr)
snd_playptr -= sndBuffer_endptr - sndBuffer; snd_playptr -= sndBuffer_endptr - sndBuffer;
} else { } else {
buflen = 3*samples_block - queued; buflen = (3*samples_block - queued) & ~1;
while (buflen > sizeof(nulBuffer)/2) {
audsrv_play_audio((char *)nulBuffer, sizeof(nulBuffer));
buflen -= sizeof(nulBuffer)/2;
}
ret = audsrv_play_audio((char *)nulBuffer, buflen*2); ret = audsrv_play_audio((char *)nulBuffer, buflen*2);
} }
if (ret != buflen*2 && ret >= 0) lprintf("sthr: play ret: %i, buflen: %i\n", ret, buflen*2); if (ret != buflen*2 && ret > 0) lprintf("sthr: play ret: %i, buflen: %i\n", ret, buflen*2);
if (ret < 0) lprintf("sthr: play: ret %08x; pos %i/%i\n", ret, samples_done, samples_made); if (ret < 0) lprintf("sthr: play: ret %08x; pos %i/%i\n", ret, samples_done, samples_made);
if (ret == 0) resetSound();
queued = audsrv_queued()/2; queued = audsrv_queued()/2;
} }
SignalSema(sound_mutex);
ret = WaitSema(sound_sem); ret = WaitSema(sound_sem);
if (ret < 0) lprintf("sthr: WaitSema failed (%d)\n", ret); if (ret < 0) lprintf("sthr: WaitSema sound failed (%d)\n", ret);
} }
lprintf("sthr: exit\n"); lprintf("sthr: exit\n");
@ -209,6 +220,11 @@ static void sound_init(void)
sema.option = (u32) "sndsem"; sema.option = (u32) "sndsem";
if ((sound_sem = CreateSema(&sema)) < 0) if ((sound_sem = CreateSema(&sema)) < 0)
return; return;
sema.max_count = 1;
sema.init_count = 1;
sema.option = (u32) "sndmutex";
if ((sound_mutex = CreateSema(&sema)) < 0)
return;
audsrv_init(); audsrv_init();
thread.func = &sound_thread; thread.func = &sound_thread;
@ -234,7 +250,6 @@ void pemu_sound_start(void) {
static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0; static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
static int mp3_init_done; static int mp3_init_done;
int ret, stereo; int ret, stereo;
struct audsrv_fmt_t format;
samples_made = samples_done = 0; samples_made = samples_done = 0;
@ -250,6 +265,9 @@ void pemu_sound_start(void) {
} }
} }
ret = WaitSema(sound_mutex);
if (ret < 0) lprintf("WaitSema mutex failed (%d)\n", ret);
if (PicoIn.sndRate > 52000 && PicoIn.sndRate < 54000) if (PicoIn.sndRate > 52000 && PicoIn.sndRate < 54000)
PicoIn.sndRate = YM2612_NATIVE_RATE(); PicoIn.sndRate = YM2612_NATIVE_RATE();
ret = POPT_EN_FM|POPT_EN_PSG|POPT_EN_STEREO; ret = POPT_EN_FM|POPT_EN_PSG|POPT_EN_STEREO;
@ -257,36 +275,28 @@ void pemu_sound_start(void) {
PsndRerate(Pico.m.frame_count ? 1 : 0); PsndRerate(Pico.m.frame_count ? 1 : 0);
} }
stereo = (PicoIn.opt&8)>>3; stereo = (PicoIn.opt&8)>>3;
samples_block = PicoIn.sndRate * (stereo ? 2 : 1) / (Pico.m.pal ? 50 : 60); samples_block = PicoIn.sndRate * (stereo ? 2 : 1) / (Pico.m.pal ? 50 : 60);
lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n", lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n",
PicoIn.sndRate, Pico.snd.len, stereo, Pico.m.pal, samples_block); PicoIn.sndRate, Pico.snd.len, stereo, Pico.m.pal, samples_block);
format.bits = 16; resetSound();
format.freq = PicoIn.sndRate;
format.channels = 2;
ret = audsrv_set_format(&format);
audsrv_set_volume(MAX_VOLUME);
if (ret < 0) {
lprintf("audsrv_set_format() failed: %i\n", ret);
emu_status_msg("sound init failed (%i), snd disabled", ret);
currentConfig.EmuOpt &= ~EOPT_EN_SOUND;
} else {
PicoIn.writeSound = writeSound; PicoIn.writeSound = writeSound;
snd_playptr = PicoIn.sndOut = sndBuffer_endptr = sndBuffer; snd_playptr = PicoIn.sndOut = sndBuffer_endptr = sndBuffer;
PsndRate_old = PicoIn.sndRate; PsndRate_old = PicoIn.sndRate;
PicoOpt_old = PicoIn.opt; PicoOpt_old = PicoIn.opt;
pal_old = Pico.m.pal; pal_old = Pico.m.pal;
}
sound_stopped = 0;
audsrv_play_audio((char *)snd_playptr, 2*2); audsrv_play_audio((char *)snd_playptr, 2*2);
SignalSema(sound_mutex);
} }
void pemu_sound_stop(void) void pemu_sound_stop(void)
{ {
sound_stopped = 1;
samples_made = samples_done = 0; samples_made = samples_done = 0;
plat_sleep_ms(200);
} }
static void sound_deinit(void) static void sound_deinit(void)
@ -303,6 +313,7 @@ static void sound_deinit(void)
static int vsync_handler(void) static int vsync_handler(void)
{ {
iSignalSema(vsync_sema_id); iSignalSema(vsync_sema_id);
if (sound_stopped)
iSignalSema(sound_sem); iSignalSema(sound_sem);
ExitHandler(); ExitHandler();