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> <version>1.0.0.Final</version>
<executions> <executions>
<execution> <execution>
<?m2e execute onConfiguration,onIncremental?>
<id>add-module-infos</id> <id>add-module-infos</id>
<phase>generate-resources</phase> <phase>generate-resources</phase>
<goals> <goals>

View File

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

View File

@ -47,14 +47,25 @@ public class Media {
totalDuration = STBVorbis.stb_vorbis_stream_length_in_seconds(decoder); totalDuration = STBVorbis.stb_vorbis_stream_length_in_seconds(decoder);
sampleRate = info.sample_rate(); sampleRate = info.sample_rate();
isStereo = info.channels() == 2; isStereo = info.channels() == 2;
if (isStereo) {
totalSamples *= 2;
}
tempSampleBuffer = MemoryUtil.memAllocShort(totalSamples); tempSampleBuffer = MemoryUtil.memAllocShort(2048);
STBVorbis.stb_vorbis_get_samples_short_interleaved(decoder, isStereo?2:1, tempSampleBuffer); 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); 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(); sampleBuffer.rewind();
} catch (RuntimeException ex) { } catch (RuntimeException ex) {
throw ex; throw ex;