Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package core;
- import java.nio.ShortBuffer;
- import org.lwjgl.openal.*;
- import static org.lwjgl.openal.AL10.*;
- import static org.lwjgl.openal.ALC10.*;
- import static org.lwjgl.system.MemoryStack.*;
- public class SoundStreamer {
- private static boolean debug = true;
- static int BUFFER_SIZE = 44100 * 1;
- static int NUM_BUFFERS = 2;
- static int SOUND_HZ = 256;
- private int totalBytes = 0;
- private int[] buffers;
- private int[] source;
- private int format;
- private int rate;
- private float[] sourcePos = { 0.0f, 0.0f, 0.0f };
- private float[] sourceVel = { 0.0f, 0.0f, 0.0f };
- private float[] sourceDir = { 0.0f, 0.0f, 0.0f };
- private long sleepTime = 0;
- private long context;
- private long device;
- private static void debugMsg(String str) {
- if (debug) {
- System.err.println(str);
- }
- }
- public SoundStreamer() {
- buffers = new int[NUM_BUFFERS];
- source = new int[1];
- }
- public void initialize() {
- String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER);
- device = alcOpenDevice(defaultDeviceName);
- int[] attributes = {0};
- context = alcCreateContext(device, attributes);
- alcMakeContextCurrent(context);
- ALCCapabilities alcCapabilities = ALC.createCapabilities(device);
- ALCapabilities alCapabilities = AL.createCapabilities(alcCapabilities);
- int numChannels = 2;
- int numBytesPerSample = 2;
- format = AL_FORMAT_STEREO16;
- rate = BUFFER_SIZE * 2; // Sample Rate
- sleepTime = (long) (1000.0 * BUFFER_SIZE / numBytesPerSample / numChannels / rate / 10.0);
- sleepTime = (sleepTime + 10) / 10 * 10;
- System.err.println("#Buffers: " + NUM_BUFFERS);
- System.err.println("Buffer size: " + BUFFER_SIZE);
- System.err.println("Format: 0x" + Integer.toString(format, 16));
- System.err.println("Sleep time: " + sleepTime);
- alGenBuffers(buffers);
- check();
- alGenSources(source);
- check();
- alSourcefv(source[0], AL_POSITION, sourcePos);
- alSourcefv(source[0], AL_VELOCITY, sourceVel);
- alSourcefv(source[0], AL_DIRECTION, sourceDir);
- alSourcef(source[0], AL_ROLLOFF_FACTOR, 0.0f);
- alSourcei(source[0], AL_SOURCE_RELATIVE, AL_TRUE);
- }
- public void release() {
- alSourceStop(source[0]);
- empty();
- alDeleteSources(source);
- alcDestroyContext(context);
- alcCloseDevice(device);
- }
- public boolean playback() {
- if (playing()) {
- return true;
- }
- debugMsg("Playback(): stream all buffers");
- for (int bufferCount = 0; bufferCount < NUM_BUFFERS; bufferCount++) {
- if (!stream(buffers[bufferCount])) {
- return false;
- }
- }
- debugMsg("playback(): queue all buffers & play source");
- alSourceQueueBuffers(source[0], buffers);
- alSourcePlay(source[0]);
- return true;
- }
- public boolean playing() {
- int[] state = new int[1];
- alGetSourcei(source[0], AL_SOURCE_STATE, state);
- return (state[0] == AL_PLAYING);
- }
- public boolean update() {
- int processed[] = new int[1];
- boolean active = true;
- alGetSourcei(source[0], AL_BUFFERS_PROCESSED, processed);
- while (processed[0] > 0) {
- int[] buffer = new int[1];
- alSourceUnqueueBuffers(source[0], buffer);
- check();
- debugMsg("update(): buffer unqueued => " + buffer[0]);
- active = stream(buffer[0]);
- debugMsg("update(): buffer queued => " + buffer[0]);
- alSourceQueueBuffers(source[0], buffer);
- check();
- processed[0]--;
- }
- return active;
- }
- private int fillBuffer(short[] pcm) {
- for(int i = 0; i< BUFFER_SIZE; i++) {
- pcm[i*2] = (short)(Math.sin(2 * Math.PI * SOUND_HZ * i / BUFFER_SIZE) * 4200);
- pcm[i*2+1] = (short)(-1 * Math.sin(2 * Math.PI * SOUND_HZ * i / BUFFER_SIZE) * 4200); // antiphase
- }
- return pcm.length;
- }
- public boolean stream(int buffer) {
- short[] pcm = new short[BUFFER_SIZE*2];
- int size = 0;
- if ( (size = fillBuffer(pcm)) <= 0) {
- return false;
- }
- totalBytes += size;
- debugMsg("stream(): buffer data => " + buffer + " totalBytes:" + totalBytes);
- ShortBuffer data = ShortBuffer.wrap(pcm, 0, BUFFER_SIZE*2);
- alBufferData(buffer, format, data, BUFFER_SIZE*2);
- check();
- return true;
- }
- protected void empty() {
- int[] queued = new int[1];
- alGetSourcei(source[0], AL_BUFFERS_QUEUED, queued);
- while (queued[0] > 0) {
- int[] buffer = new int[1];
- alSourceUnqueueBuffers(source[0], buffer);
- check();
- queued[0]--;
- }
- }
- protected void check() {
- if (alGetError() != AL_NO_ERROR)
- throw new RuntimeException("OpenAL error raised...");
- }
- }
Add Comment
Please, Sign In to add comment