Skip to content

Commit

Permalink
platform ps2+psp, fix audio overrun when rate != 44.1KHz
Browse files Browse the repository at this point in the history
  • Loading branch information
irixxxx committed Oct 15, 2024
1 parent a1fc78f commit 4be2e8c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions platform/ps2/emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static void writeSound(int len)
{
int l;

if (samples_made - samples_done < samples_block * (SOUND_BLOCK_COUNT-2) - 4) {
if (samples_made - samples_done < samples_block * (SOUND_BLOCK_COUNT-2) - 8) {
samples_made += len / 2;
sndBuffer_ptr += len / 2;
l = sndBuffer_ptr - sndBuffer;
Expand All @@ -130,7 +130,8 @@ static void writeSound(int len)
sndBuffer_endptr = sndBuffer_ptr;
if (l > sizeof(sndBuffer)/2)
lprintf("snd ovrn %d %d\n", len, sndBuffer_ptr - sndBuffer);
if (l > samples_block * (SOUND_BLOCK_COUNT-2)) {
// leave space for block with 1 overhanging sample @11025Hz
if (l >= samples_block * (SOUND_BLOCK_COUNT-1) - 8) {
sndBuffer_endptr = sndBuffer_ptr;
sndBuffer_ptr = sndBuffer;
}
Expand Down Expand Up @@ -227,6 +228,8 @@ static int sound_thread(void *argp)
while (queued < 2*samples_block) {
// compute sample chunk size
int buflen = samples_made - samples_done;
if (snd_playptr >= sndBuffer_endptr)
snd_playptr -= sndBuffer_endptr - sndBuffer;
if (buflen > sndBuffer_endptr - snd_playptr)
buflen = sndBuffer_endptr - snd_playptr;
if (buflen > 3*samples_block - queued)
Expand All @@ -238,8 +241,6 @@ static int sound_thread(void *argp)

samples_done += buflen;
snd_playptr += buflen;
if (snd_playptr >= sndBuffer_endptr)
snd_playptr -= sndBuffer_endptr - sndBuffer;
} else {
buflen = (3*samples_block - queued) & ~1;
while (buflen > sizeof(nulBuffer)/2) {
Expand Down
4 changes: 2 additions & 2 deletions platform/psp/emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,9 @@ static SceUID sound_sem = -1;

static void writeSound(int len)
{
// make sure there is (samples_block+2) free space in the buffer after
// make sure there is enough free space in the buffer after
// this frame, else the next frame may overwrite old stored samples.
if (samples_made - samples_done < samples_block * (SOUND_BLOCK_COUNT-2) - 4) {
if (samples_made - samples_done < samples_block * (SOUND_BLOCK_COUNT-2) - 8) {
sndBuffer_ptr += len / 2;
if (sndBuffer_ptr - sndBuffer > sizeof(sndBuffer)/2)
lprintf("snd ovrn %d %d\n", len, sndBuffer_ptr - sndBuffer);
Expand Down

0 comments on commit 4be2e8c

Please sign in to comment.