Skip to content

Commit

Permalink
XNA_Song: Detect end-of-stream via the song length, not the decode le…
Browse files Browse the repository at this point in the history
…ngth.

This is important for songs that are _exactly_ X seconds, because the decode
length will always be the buffer size, so it would never detect the end of the
stream until it decoded exactly 0, which wouldn't trip a buffer submission and
therefore FAUDIO_END_OF_STREAM would never get sent.
  • Loading branch information
flibitijibibo committed Sep 19, 2024
1 parent 0c7ee1c commit e23e2c5
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/XNA_Song.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
static float songVolume = 1.0f;
static FAudio *songAudio = NULL;
static FAudioMasteringVoice *songMaster = NULL;
static unsigned int songLength = 0;
static unsigned int songOffset = 0;

static FAudioSourceVoice *songVoice = NULL;
static FAudioVoiceCallback callbacks;
Expand Down Expand Up @@ -121,9 +123,6 @@ static void XNA_SongSubmitBuffer(FAudioVoiceCallback *callback, void *pBufferCon
activeVorbisSongInfo.sample_rate * activeVorbisSongInfo.channels
);
buffer.AudioBytes = decoded * activeVorbisSongInfo.channels * sizeof(float);
buffer.Flags = (decoded < activeVorbisSongInfo.sample_rate) ?
FAUDIO_END_OF_STREAM :
0;
}
else if (activeQoaSong != NULL)
{
Expand All @@ -133,16 +132,15 @@ static void XNA_SongSubmitBuffer(FAudioVoiceCallback *callback, void *pBufferCon
(short*) songCache
);
buffer.AudioBytes = decoded * qoaChannels * sizeof(short);
buffer.Flags = (decoded < qoaSamplesPerChannelPerFrame) ?
FAUDIO_END_OF_STREAM :
0;
}

if (decoded == 0)
{
return;
}

songOffset += decoded;
buffer.Flags = (songOffset >= songLength) ? FAUDIO_END_OF_STREAM : 0;
buffer.pAudioData = songCache;
buffer.PlayBegin = 0;
buffer.PlayLength = decoded;
Expand Down Expand Up @@ -222,6 +220,9 @@ FAUDIOAPI float XNA_PlaySong(const char *name)
format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
format.cbSize = 0;

songOffset = 0;
songLength = stb_vorbis_stream_length_in_samples(activeVorbisSong);
}
else /* It's not vorbis, try qoa!*/
{
Expand All @@ -242,6 +243,9 @@ FAUDIOAPI float XNA_PlaySong(const char *name)
format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
format.cbSize = 0;

songOffset = 0;
songLength = qoaTotalSamplesPerChannel;
}

/* Allocate decode cache */
Expand Down

0 comments on commit e23e2c5

Please sign in to comment.