diff options
Diffstat (limited to 'src/main/java/net/tylermurphy/Minecraft/Audio')
-rwxr-xr-x | src/main/java/net/tylermurphy/Minecraft/Audio/Sound.java | 82 | ||||
-rwxr-xr-x | src/main/java/net/tylermurphy/Minecraft/Audio/SoundManager.java | 45 |
2 files changed, 127 insertions, 0 deletions
diff --git a/src/main/java/net/tylermurphy/Minecraft/Audio/Sound.java b/src/main/java/net/tylermurphy/Minecraft/Audio/Sound.java new file mode 100755 index 0000000..58852da --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Audio/Sound.java @@ -0,0 +1,82 @@ +package net.tylermurphy.Minecraft.Audio;
+
+import org.lwjgl.openal.AL10;
+import org.lwjgl.system.*;
+
+import net.tylermurphy.Minecraft.Util.Constants;
+
+import java.nio.*;
+
+import static org.lwjgl.openal.AL10.*;
+import static org.lwjgl.stb.STBVorbis.*;
+import static org.lwjgl.system.MemoryStack.*;
+import static org.lwjgl.system.libc.LibCStdlib.*;
+
+public class Sound {
+
+ int bufferId;
+ int sourceId;
+
+ protected Sound(String fileName) {
+ ShortBuffer rawAudioBuffer;
+
+ int channels;
+ int sampleRate;
+
+ try (MemoryStack stack = stackPush()) {
+ IntBuffer channelsBuffer = stack.mallocInt(1);
+ IntBuffer sampleRateBuffer = stack.mallocInt(1);
+ rawAudioBuffer = stb_vorbis_decode_filename(Constants.OGG_LOCATION + fileName +".ogg", channelsBuffer, sampleRateBuffer);
+ channels = channelsBuffer.get(0);
+ sampleRate = sampleRateBuffer.get(0);
+ }
+
+ int format = -1;
+ if (channels == 1) {
+ format = AL_FORMAT_MONO16;
+ } else if (channels == 2) {
+ format = AL_FORMAT_STEREO16;
+ }
+
+ bufferId = alGenBuffers();
+ alBufferData(bufferId, format, rawAudioBuffer, sampleRate);
+ free(rawAudioBuffer);
+ sourceId = alGenSources();
+ alSourcei(sourceId, AL_BUFFER, bufferId);
+ }
+
+ public void play() {
+ stop();
+ alSourcePlay(sourceId);
+ }
+
+ public boolean isPlaying() {
+ return AL10.alGetSourcei(sourceId, AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING;
+ }
+
+ public void stop() {
+ alSourceStop(sourceId);
+ }
+
+ public void pause() {
+ alSourcePause(sourceId);
+ }
+
+ public void resume() {
+ alSourcePlay(sourceId);
+ }
+
+ public void setPosition(int x,int y,int z) {
+ AL10.alSource3f(sourceId, AL10.AL_POSITION, x, y, z);
+ }
+
+ public void setLooping(boolean loop) {
+ AL10.alSourcei(sourceId, AL10.AL_LOOPING, loop ? 1 : 0);
+ }
+
+ protected void cleanUp() {
+ alDeleteSources(sourceId);
+ alDeleteBuffers(bufferId);
+ }
+
+}
diff --git a/src/main/java/net/tylermurphy/Minecraft/Audio/SoundManager.java b/src/main/java/net/tylermurphy/Minecraft/Audio/SoundManager.java new file mode 100755 index 0000000..10b7d6d --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Audio/SoundManager.java @@ -0,0 +1,45 @@ +package net.tylermurphy.Minecraft.Audio;
+
+import java.util.ArrayList;
+
+import java.util.List;
+
+import org.lwjgl.openal.*;
+
+public class SoundManager {
+
+ private static long device;
+ private static long context;
+ private static List<Sound> sounds;
+
+ public static void init() {
+ String defaultDeviceName = ALC10.alcGetString(0, ALC10.ALC_DEFAULT_DEVICE_SPECIFIER);
+ device = ALC10.alcOpenDevice(defaultDeviceName);
+ int[] attributes = {0};
+ context = ALC10.alcCreateContext(device, attributes);
+ ALC10.alcMakeContextCurrent(context);
+ ALCCapabilities alcCapabilities = ALC.createCapabilities(device);
+ AL.createCapabilities(alcCapabilities);
+ sounds = new ArrayList<>();
+ AL11.alDistanceModel(AL11.AL_LINEAR_DISTANCE_CLAMPED);
+ }
+
+ public static Sound loadSound(String fileName) {
+ Sound sound = new Sound(fileName);
+ sounds.add(sound);
+ return sound;
+ }
+
+ public static void cleanUp() {
+ for(Sound sound : sounds) {
+ sound.cleanUp();
+ }
+ ALC10.alcDestroyContext(context);
+ ALC10.alcCloseDevice(device);
+ }
+
+ public static void setListenerData(float x, float y, float z) {
+ AL10.alListener3f(AL10.AL_POSITION, x, y, z);
+ AL10.alListener3f(AL10.AL_VELOCITY, 0, 0, 0);
+ }
+}
\ No newline at end of file |