Fix incomplete music decoding bug

This commit is contained in:
Brendan Robert 2024-04-09 23:28:47 -05:00
parent b3ec0df929
commit 4946dbd1e9
3 changed files with 18 additions and 7 deletions

View File

@ -113,6 +113,7 @@
<version>1.0.0.Final</version>
<executions>
<execution>
<?m2e execute onConfiguration,onIncremental?>
<id>add-module-infos</id>
<phase>generate-resources</phase>
<goals>

View File

@ -189,7 +189,6 @@ public class SoundMixer extends Device {
buffers.add(buffer);
return buffer;
}
public static class SoundBuffer {
public static int MAX_BUFFER_ID;
private ShortBuffer currentBuffer;

View File

@ -47,14 +47,25 @@ public class Media {
totalDuration = STBVorbis.stb_vorbis_stream_length_in_seconds(decoder);
sampleRate = info.sample_rate();
isStereo = info.channels() == 2;
if (isStereo) {
totalSamples *= 2;
}
tempSampleBuffer = MemoryUtil.memAllocShort(totalSamples);
STBVorbis.stb_vorbis_get_samples_short_interleaved(decoder, isStereo?2:1, tempSampleBuffer);
tempSampleBuffer = MemoryUtil.memAllocShort(2048);
sampleBuffer = ShortBuffer.allocate(totalSamples);
int sampleCount = 1;
int currentOffset = 0;
while (sampleCount > 0) {
sampleCount = STBVorbis.stb_vorbis_get_samples_short_interleaved(decoder, isStereo?2:1, tempSampleBuffer);
if (sampleCount == 0) {
break;
}
// copy sample buffer into byte buffer so we can deallocate, then transfer the buffer contents
sampleBuffer.put(currentOffset, tempSampleBuffer, 0, sampleCount * (isStereo ? 2 : 1));
tempSampleBuffer.rewind();
currentOffset += sampleCount * (isStereo ? 2 : 1);
}
STBVorbis.stb_vorbis_close(decoder);
tempSampleBuffer.rewind();
// copy sample buffer into byte buffer so we can deallocate, then transfer the buffer contents
sampleBuffer = ShortBuffer.allocate(totalSamples*2);
sampleBuffer.put(tempSampleBuffer);
sampleBuffer.rewind();
} catch (RuntimeException ex) {
throw ex;