From 6153578d89f77f7aca8ab53025aed2494f84e2fe Mon Sep 17 00:00:00 2001 From: Lenni0451 <20379977+Lenni0451@users.noreply.github.com> Date: Wed, 8 May 2024 16:24:36 +0200 Subject: [PATCH] Fixed sounds always being loaded as 16 bit --- .../audio/export/impl/JavaxAudioExporter.java | 17 ++++++++++++++--- .../audio/soundsystem/JavaxSoundSystem.java | 17 ++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/raphimc/noteblocktool/audio/export/impl/JavaxAudioExporter.java b/src/main/java/net/raphimc/noteblocktool/audio/export/impl/JavaxAudioExporter.java index 95529fd..6ca73b1 100644 --- a/src/main/java/net/raphimc/noteblocktool/audio/export/impl/JavaxAudioExporter.java +++ b/src/main/java/net/raphimc/noteblocktool/audio/export/impl/JavaxAudioExporter.java @@ -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; diff --git a/src/main/java/net/raphimc/noteblocktool/audio/soundsystem/JavaxSoundSystem.java b/src/main/java/net/raphimc/noteblocktool/audio/soundsystem/JavaxSoundSystem.java index 6496544..b6c4030 100644 --- a/src/main/java/net/raphimc/noteblocktool/audio/soundsystem/JavaxSoundSystem.java +++ b/src/main/java/net/raphimc/noteblocktool/audio/soundsystem/JavaxSoundSystem.java @@ -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;