103 lines
2.7 KiB
Java
103 lines
2.7 KiB
Java
package shootergame.audio;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.IntBuffer;
|
|
import java.nio.ShortBuffer;
|
|
|
|
import org.lwjgl.openal.AL10;
|
|
import org.lwjgl.openal.AL11;
|
|
import org.lwjgl.openal.ALC10;
|
|
import org.lwjgl.stb.STBVorbis;
|
|
import org.lwjgl.system.MemoryStack;
|
|
import org.lwjgl.system.libc.LibCStdlib;
|
|
|
|
import static org.lwjgl.openal.AL10.AL_BUFFER;
|
|
import static org.lwjgl.openal.AL10.AL_GAIN;
|
|
import static org.lwjgl.openal.AL10.AL_POSITION;
|
|
import static org.lwjgl.openal.AL10.alSource3f;
|
|
import static org.lwjgl.openal.AL10.alSourcePlay;
|
|
import static org.lwjgl.openal.AL10.alSourcef;
|
|
import static org.lwjgl.openal.AL10.alSourcei;
|
|
import static org.lwjgl.openal.AL11.*;
|
|
|
|
import shootergame.Main;
|
|
import shootergame.display.Camera;
|
|
import shootergame.resources.Resource;
|
|
import shootergame.util.math.MathHelpers;
|
|
import shootergame.util.math.vec.Vec2d;
|
|
import shootergame.util.math.vec.Vec3d;
|
|
|
|
public class AudioObject
|
|
{
|
|
int bufferPointer;
|
|
Resource resource;
|
|
int channels;
|
|
int sample_rate;
|
|
int output;
|
|
|
|
public AudioObject(Resource resource)
|
|
{
|
|
// Store the argument values
|
|
this.resource = resource;
|
|
}
|
|
|
|
public void init()
|
|
{
|
|
// Create some buffers
|
|
MemoryStack stack = MemoryStack.stackPush();
|
|
IntBuffer channels_buf = stack.mallocInt(1);
|
|
IntBuffer sample_rate_buf = stack.mallocInt(1);
|
|
|
|
// Get the audio
|
|
ByteBuffer resource_buffer = resource.getByteBuffer();
|
|
ShortBuffer audio = STBVorbis.stb_vorbis_decode_memory(resource_buffer, channels_buf, sample_rate_buf);
|
|
|
|
// Get the channels and the sample rate
|
|
channels = channels_buf.get();
|
|
sample_rate = sample_rate_buf.get();
|
|
|
|
//Find the correct OpenAL format
|
|
int format = -1;
|
|
if(channels == 1) {
|
|
format = AL_FORMAT_MONO16;
|
|
} else if(channels == 2) {
|
|
format = AL_FORMAT_STEREO16;
|
|
}
|
|
|
|
// Send the data to OpenAL
|
|
bufferPointer = alGenBuffers();
|
|
alBufferData(bufferPointer, format, audio, sample_rate);
|
|
|
|
// Free some c buffers
|
|
//LibCStdlib.free(audio);
|
|
//LibCStdlib.free(resource_buffer);
|
|
}
|
|
|
|
public void play(Vec3d pos, double volume)
|
|
{
|
|
// Get the player pos
|
|
Vec2d p_pos = Main.player.pos;
|
|
|
|
// Convert the angle to radians
|
|
double angle_r = Math.toRadians(Main.player.angle + 90);
|
|
|
|
// Calculate the translation
|
|
double x = ( pos.x - p_pos.x );
|
|
double y = ( pos.y - p_pos.y );
|
|
double z = pos.z;
|
|
|
|
// Calculate the rotation
|
|
Vec2d rotated = MathHelpers.rotate2(new Vec2d(x, y), angle_r);
|
|
x = rotated.x;
|
|
y = rotated.y;
|
|
|
|
// Play the sound with a new source
|
|
int source = AudioSources.getSource();
|
|
alSourceStop(source);
|
|
alSourcei(source, AL_BUFFER, bufferPointer);
|
|
alSourcef(source, AL_GAIN, (float)volume);
|
|
alSource3f(source, AL_POSITION, (float)x, (float)y, (float)z);
|
|
alSourcePlay(source);
|
|
}
|
|
}
|