Skip to content

Commit e23e2c5

Browse files
committed
XNA_Song: Detect end-of-stream via the song length, not the decode length.
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.
1 parent 0c7ee1c commit e23e2c5

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/XNA_Song.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
static float songVolume = 1.0f;
9292
static FAudio *songAudio = NULL;
9393
static FAudioMasteringVoice *songMaster = NULL;
94+
static unsigned int songLength = 0;
95+
static unsigned int songOffset = 0;
9496

9597
static FAudioSourceVoice *songVoice = NULL;
9698
static FAudioVoiceCallback callbacks;
@@ -121,9 +123,6 @@ static void XNA_SongSubmitBuffer(FAudioVoiceCallback *callback, void *pBufferCon
121123
activeVorbisSongInfo.sample_rate * activeVorbisSongInfo.channels
122124
);
123125
buffer.AudioBytes = decoded * activeVorbisSongInfo.channels * sizeof(float);
124-
buffer.Flags = (decoded < activeVorbisSongInfo.sample_rate) ?
125-
FAUDIO_END_OF_STREAM :
126-
0;
127126
}
128127
else if (activeQoaSong != NULL)
129128
{
@@ -133,16 +132,15 @@ static void XNA_SongSubmitBuffer(FAudioVoiceCallback *callback, void *pBufferCon
133132
(short*) songCache
134133
);
135134
buffer.AudioBytes = decoded * qoaChannels * sizeof(short);
136-
buffer.Flags = (decoded < qoaSamplesPerChannelPerFrame) ?
137-
FAUDIO_END_OF_STREAM :
138-
0;
139135
}
140136

141137
if (decoded == 0)
142138
{
143139
return;
144140
}
145141

142+
songOffset += decoded;
143+
buffer.Flags = (songOffset >= songLength) ? FAUDIO_END_OF_STREAM : 0;
146144
buffer.pAudioData = songCache;
147145
buffer.PlayBegin = 0;
148146
buffer.PlayLength = decoded;
@@ -222,6 +220,9 @@ FAUDIOAPI float XNA_PlaySong(const char *name)
222220
format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
223221
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
224222
format.cbSize = 0;
223+
224+
songOffset = 0;
225+
songLength = stb_vorbis_stream_length_in_samples(activeVorbisSong);
225226
}
226227
else /* It's not vorbis, try qoa!*/
227228
{
@@ -242,6 +243,9 @@ FAUDIOAPI float XNA_PlaySong(const char *name)
242243
format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
243244
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
244245
format.cbSize = 0;
246+
247+
songOffset = 0;
248+
songLength = qoaTotalSamplesPerChannel;
245249
}
246250

247251
/* Allocate decode cache */

0 commit comments

Comments
 (0)