Skip to content

Commit

Permalink
Added support for loading .ogg audio files
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed May 9, 2024
1 parent dc3ea6f commit fb8c20d
Show file tree
Hide file tree
Showing 36 changed files with 67 additions and 22 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ dependencies {
include "net.lenni0451.commons:swing:1.5.1"
include "org.lwjgl:lwjgl:3.3.3"
include "org.lwjgl:lwjgl-openal:3.3.3"
include "org.lwjgl:lwjgl-stb:3.3.3"
["natives-windows", "natives-windows-arm64", "natives-linux", "natives-linux-arm64", "natives-macos", "natives-macos-arm64"].each {
include "org.lwjgl:lwjgl:3.3.3:$it"
include "org.lwjgl:lwjgl-openal:3.3.3:$it"
include "org.lwjgl:lwjgl-stb:3.3.3:$it"
}
}

Expand Down
32 changes: 16 additions & 16 deletions src/main/java/net/raphimc/noteblocktool/audio/SoundMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ public class SoundMap {
public static final Map<Instrument, String> SOUNDS = new EnumMap<>(Instrument.class);

static {
SOUNDS.put(Instrument.HARP, "/noteblock_sounds/harp.wav");
SOUNDS.put(Instrument.BASS, "/noteblock_sounds/bass.wav");
SOUNDS.put(Instrument.BASS_DRUM, "/noteblock_sounds/bd.wav");
SOUNDS.put(Instrument.SNARE, "/noteblock_sounds/snare.wav");
SOUNDS.put(Instrument.HAT, "/noteblock_sounds/hat.wav");
SOUNDS.put(Instrument.GUITAR, "/noteblock_sounds/guitar.wav");
SOUNDS.put(Instrument.FLUTE, "/noteblock_sounds/flute.wav");
SOUNDS.put(Instrument.BELL, "/noteblock_sounds/bell.wav");
SOUNDS.put(Instrument.CHIME, "/noteblock_sounds/icechime.wav");
SOUNDS.put(Instrument.XYLOPHONE, "/noteblock_sounds/xylobone.wav");
SOUNDS.put(Instrument.IRON_XYLOPHONE, "/noteblock_sounds/iron_xylophone.wav");
SOUNDS.put(Instrument.COW_BELL, "/noteblock_sounds/cow_bell.wav");
SOUNDS.put(Instrument.DIDGERIDOO, "/noteblock_sounds/didgeridoo.wav");
SOUNDS.put(Instrument.BIT, "/noteblock_sounds/bit.wav");
SOUNDS.put(Instrument.BANJO, "/noteblock_sounds/banjo.wav");
SOUNDS.put(Instrument.PLING, "/noteblock_sounds/pling.wav");
SOUNDS.put(Instrument.HARP, "/noteblock_sounds/harp.ogg");
SOUNDS.put(Instrument.BASS, "/noteblock_sounds/bass.ogg");
SOUNDS.put(Instrument.BASS_DRUM, "/noteblock_sounds/bd.ogg");
SOUNDS.put(Instrument.SNARE, "/noteblock_sounds/snare.ogg");
SOUNDS.put(Instrument.HAT, "/noteblock_sounds/hat.ogg");
SOUNDS.put(Instrument.GUITAR, "/noteblock_sounds/guitar.ogg");
SOUNDS.put(Instrument.FLUTE, "/noteblock_sounds/flute.ogg");
SOUNDS.put(Instrument.BELL, "/noteblock_sounds/bell.ogg");
SOUNDS.put(Instrument.CHIME, "/noteblock_sounds/icechime.ogg");
SOUNDS.put(Instrument.XYLOPHONE, "/noteblock_sounds/xylobone.ogg");
SOUNDS.put(Instrument.IRON_XYLOPHONE, "/noteblock_sounds/iron_xylophone.ogg");
SOUNDS.put(Instrument.COW_BELL, "/noteblock_sounds/cow_bell.ogg");
SOUNDS.put(Instrument.DIDGERIDOO, "/noteblock_sounds/didgeridoo.ogg");
SOUNDS.put(Instrument.BIT, "/noteblock_sounds/bit.ogg");
SOUNDS.put(Instrument.BANJO, "/noteblock_sounds/banjo.ogg");
SOUNDS.put(Instrument.PLING, "/noteblock_sounds/pling.ogg");
}

public static Map<Instrument, int[]> loadInstrumentSamples(final AudioFormat targetFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
import net.raphimc.noteblocktool.audio.SoundMap;
import net.raphimc.noteblocktool.audio.soundsystem.SoundSystem;
import net.raphimc.noteblocktool.util.SampleOutputStream;
import net.raphimc.noteblocktool.util.SoundSampleUtil;
import org.lwjgl.openal.*;
import org.lwjgl.system.MemoryUtil;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.EnumMap;
Expand Down Expand Up @@ -129,7 +128,7 @@ private OpenALSoundSystem(final int maxSounds, final AudioFormat captureAudioFor
this.checkError("Could not set listener orientation");

for (Map.Entry<Instrument, String> entry : SoundMap.SOUNDS.entrySet()) {
this.instrumentBuffers.put(entry.getKey(), this.loadWav(OpenALSoundSystem.class.getResourceAsStream(entry.getValue())));
this.instrumentBuffers.put(entry.getKey(), this.loadAudioFile(OpenALSoundSystem.class.getResourceAsStream(entry.getValue())));
}

this.scheduler.scheduleAtFixedRate(this::tick, 0, 100, TimeUnit.MILLISECONDS);
Expand Down Expand Up @@ -253,11 +252,11 @@ private void tick() {
});
}

private int loadWav(final InputStream inputStream) {
private int loadAudioFile(final InputStream inputStream) {
final int buffer = AL10.alGenBuffers();
this.checkError("Could not generate audio buffer");
try {
final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(inputStream));
final AudioInputStream audioInputStream = SoundSampleUtil.readAudioFile(inputStream);
final AudioFormat audioFormat = audioInputStream.getFormat();

final byte[] audioBytes = ByteStreams.toByteArray(audioInputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
package net.raphimc.noteblocktool.util;

import com.google.common.io.ByteStreams;
import org.lwjgl.PointerBuffer;
import org.lwjgl.stb.STBVorbis;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
Expand All @@ -27,11 +31,51 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;

public class SoundSampleUtil {

private static final byte[] OGG_MAGIC = new byte[]{(byte) 'O', (byte) 'g', (byte) 'g', (byte) 'S'};

public static AudioInputStream readAudioFile(final InputStream inputStream) throws UnsupportedAudioFileException, IOException {
final BufferedInputStream bis = new BufferedInputStream(inputStream);
final byte[] magic = new byte[4];
bis.mark(magic.length);
bis.read(magic);
bis.reset();
if (Arrays.equals(magic, OGG_MAGIC)) {
final byte[] data = ByteStreams.toByteArray(bis);
final ByteBuffer dataBuffer = (ByteBuffer) MemoryUtil.memAlloc(data.length).put(data).flip();
try (MemoryStack memoryStack = MemoryStack.stackPush()) {
final IntBuffer channels = memoryStack.callocInt(1);
final IntBuffer sampleRate = memoryStack.callocInt(1);
final PointerBuffer samples = memoryStack.callocPointer(1);

final int samplesCount = STBVorbis.stb_vorbis_decode_memory(dataBuffer, channels, sampleRate, samples);
if (samplesCount == -1) {
MemoryUtil.memFree(dataBuffer);
throw new RuntimeException("Failed to decode ogg file");
}

final ByteBuffer samplesBuffer = samples.getByteBuffer(samplesCount * 2);
final byte[] samplesArray = new byte[samplesCount * 2];
samplesBuffer.get(samplesArray);

MemoryUtil.memFree(dataBuffer);
MemoryUtil.memFree(samplesBuffer);

final AudioFormat audioFormat = new AudioFormat(sampleRate.get(), 16, channels.get(), true, false);
return new AudioInputStream(new ByteArrayInputStream(samplesArray), audioFormat, samplesArray.length);
}
} else {
return AudioSystem.getAudioInputStream(new BufferedInputStream(inputStream));
}
}

public static int[] readSamples(final InputStream inputStream, final AudioFormat targetFormat) throws UnsupportedAudioFileException, IOException {
AudioInputStream in = AudioSystem.getAudioInputStream(new BufferedInputStream(inputStream));
AudioInputStream in = readAudioFile(inputStream);
if (!in.getFormat().matches(targetFormat)) in = AudioSystem.getAudioInputStream(targetFormat, in);
final byte[] audioBytes = ByteStreams.toByteArray(in);
final SampleInputStream sis = new SampleInputStream(new ByteArrayInputStream(audioBytes), targetFormat);
Expand Down
Binary file added src/main/resources/noteblock_sounds/banjo.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/banjo.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/bass.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/bass.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/bd.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/bd.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/bell.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/bell.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/bit.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/bit.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/cow_bell.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/cow_bell.wav
Binary file not shown.
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/didgeridoo.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/flute.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/flute.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/guitar.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/guitar.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/harp.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/harp.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/hat.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/hat.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/icechime.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/icechime.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/pling.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/pling.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/snare.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/snare.wav
Binary file not shown.
Binary file added src/main/resources/noteblock_sounds/xylobone.ogg
Binary file not shown.
Binary file removed src/main/resources/noteblock_sounds/xylobone.wav
Binary file not shown.

0 comments on commit fb8c20d

Please sign in to comment.