Skip to content

Commit

Permalink
Fixed sounds always being loaded as 16 bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni0451 committed May 8, 2024
1 parent 9097d53 commit 6153578
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,20 @@ private int[] readSound(final AudioFormat format, final InputStream is) {
final int sampleSize = format.getSampleSizeInBits() / 8;
final int[] samples = new int[audioBytes.length / sampleSize];
for (int i = 0; i < samples.length; i++) {
final byte[] sampleBytes = new byte[sampleSize];
System.arraycopy(audioBytes, i * sampleSize, sampleBytes, 0, sampleSize);
samples[i] = ByteBuffer.wrap(sampleBytes).order(ByteOrder.LITTLE_ENDIAN).getShort();
ByteBuffer buffer = ByteBuffer.wrap(audioBytes, i * sampleSize, sampleSize).order(ByteOrder.LITTLE_ENDIAN);
switch (format.getSampleSizeInBits()) {
case 8:
samples[i] = buffer.get();
break;
case 16:
samples[i] = buffer.getShort();
break;
case 32:
samples[i] = buffer.getInt();
break;
default:
throw new UnsupportedOperationException("Unsupported sample size: " + format.getSampleSizeInBits());
}
}

return samples;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,20 @@ private static int[] readSound(final InputStream is) {
final int sampleSize = FORMAT.getSampleSizeInBits() / 8;
final int[] samples = new int[audioBytes.length / sampleSize];
for (int i = 0; i < samples.length; i++) {
final byte[] sampleBytes = new byte[sampleSize];
System.arraycopy(audioBytes, i * sampleSize, sampleBytes, 0, sampleSize);
samples[i] = ByteBuffer.wrap(sampleBytes).order(ByteOrder.LITTLE_ENDIAN).getShort();
ByteBuffer buffer = ByteBuffer.wrap(audioBytes, i * sampleSize, sampleSize).order(ByteOrder.LITTLE_ENDIAN);
switch (FORMAT.getSampleSizeInBits()) {
case 8:
samples[i] = buffer.get();
break;
case 16:
samples[i] = buffer.getShort();
break;
case 32:
samples[i] = buffer.getInt();
break;
default:
throw new UnsupportedOperationException("Unsupported sample size: " + FORMAT.getSampleSizeInBits());
}
}

return samples;
Expand Down

0 comments on commit 6153578

Please sign in to comment.