Guest User

Untitled

a guest
Apr 8th, 2019
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. package core;
  2.  
  3. import java.nio.ShortBuffer;
  4.  
  5. import org.lwjgl.openal.*;
  6. import static org.lwjgl.openal.AL10.*;
  7. import static org.lwjgl.openal.ALC10.*;
  8. import static org.lwjgl.system.MemoryStack.*;
  9.  
  10. public class SoundStreamer {
  11.  
  12.     private static boolean debug = true;
  13.     static int BUFFER_SIZE = 44100 * 1;
  14.     static int NUM_BUFFERS = 2;
  15.     static int SOUND_HZ = 256;
  16.  
  17.     private int totalBytes = 0;
  18.     private int[] buffers;
  19.     private int[] source;
  20.  
  21.     private int format;
  22.     private int rate;
  23.  
  24.     private float[] sourcePos = { 0.0f, 0.0f, 0.0f };
  25.     private float[] sourceVel = { 0.0f, 0.0f, 0.0f };
  26.     private float[] sourceDir = { 0.0f, 0.0f, 0.0f };
  27.  
  28.     private long sleepTime = 0;
  29.    
  30.     private long context;
  31.     private long device;
  32.  
  33.     private static void debugMsg(String str) {
  34.         if (debug) {
  35.             System.err.println(str);
  36.         }
  37.     }
  38.  
  39.     public SoundStreamer() {
  40.  
  41.         buffers = new int[NUM_BUFFERS];
  42.         source = new int[1];
  43.  
  44.     }
  45.  
  46.     public void initialize() {
  47.        
  48.         String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER);
  49.         device = alcOpenDevice(defaultDeviceName);
  50.         int[] attributes = {0};
  51.         context = alcCreateContext(device, attributes);
  52.        
  53.         alcMakeContextCurrent(context);
  54.  
  55.        
  56.         ALCCapabilities alcCapabilities = ALC.createCapabilities(device);
  57.         ALCapabilities  alCapabilities  = AL.createCapabilities(alcCapabilities);
  58.  
  59.    
  60.         int numChannels = 2;
  61.         int numBytesPerSample = 2;
  62.  
  63.         format = AL_FORMAT_STEREO16;
  64.  
  65.         rate = BUFFER_SIZE * 2; // Sample Rate
  66.  
  67.         sleepTime = (long) (1000.0 * BUFFER_SIZE / numBytesPerSample / numChannels / rate / 10.0);
  68.         sleepTime = (sleepTime + 10) / 10 * 10;
  69.  
  70.         System.err.println("#Buffers: " + NUM_BUFFERS);
  71.         System.err.println("Buffer size: " + BUFFER_SIZE);
  72.         System.err.println("Format: 0x" + Integer.toString(format, 16));
  73.         System.err.println("Sleep time: " + sleepTime);
  74.  
  75.         alGenBuffers(buffers);
  76.         check();
  77.         alGenSources(source);
  78.         check();
  79.  
  80.         alSourcefv(source[0], AL_POSITION, sourcePos);
  81.         alSourcefv(source[0], AL_VELOCITY, sourceVel);
  82.         alSourcefv(source[0], AL_DIRECTION, sourceDir);
  83.  
  84.         alSourcef(source[0], AL_ROLLOFF_FACTOR, 0.0f);
  85.         alSourcei(source[0], AL_SOURCE_RELATIVE, AL_TRUE);
  86.  
  87.     }
  88.  
  89.     public void release() {
  90.         alSourceStop(source[0]);
  91.         empty();
  92.         alDeleteSources(source);
  93.        
  94.         alcDestroyContext(context);
  95.         alcCloseDevice(device);
  96.        
  97.     }
  98.  
  99.     public boolean playback() {
  100.         if (playing()) {
  101.             return true;
  102.         }
  103.  
  104.         debugMsg("Playback(): stream all buffers");
  105.         for (int bufferCount = 0; bufferCount < NUM_BUFFERS; bufferCount++) {
  106.             if (!stream(buffers[bufferCount])) {
  107.                 return false;
  108.             }
  109.         }
  110.  
  111.         debugMsg("playback(): queue all buffers & play source");
  112.         alSourceQueueBuffers(source[0], buffers);
  113.         alSourcePlay(source[0]);
  114.  
  115.         return true;
  116.  
  117.     }
  118.  
  119.     public boolean playing() {
  120.         int[] state = new int[1];
  121.  
  122.         alGetSourcei(source[0], AL_SOURCE_STATE, state);
  123.         return (state[0] == AL_PLAYING);
  124.     }
  125.  
  126.     public boolean update() {
  127.         int processed[] = new int[1];
  128.         boolean active = true;
  129.  
  130.         alGetSourcei(source[0], AL_BUFFERS_PROCESSED, processed);
  131.  
  132.         while (processed[0] > 0) {
  133.             int[] buffer = new int[1];
  134.  
  135.             alSourceUnqueueBuffers(source[0], buffer);
  136.             check();
  137.             debugMsg("update(): buffer unqueued => " + buffer[0]);
  138.  
  139.             active = stream(buffer[0]);
  140.             debugMsg("update(): buffer queued => " + buffer[0]);
  141.             alSourceQueueBuffers(source[0], buffer);
  142.             check();
  143.  
  144.             processed[0]--;
  145.         }
  146.  
  147.         return active;
  148.     }
  149.  
  150.    
  151.     private int fillBuffer(short[] pcm) {
  152.    
  153.         for(int i = 0; i< BUFFER_SIZE; i++) {
  154.            
  155.              pcm[i*2] = (short)(Math.sin(2 * Math.PI * SOUND_HZ * i / BUFFER_SIZE) * 4200);
  156.              pcm[i*2+1] = (short)(-1 * Math.sin(2 * Math.PI * SOUND_HZ * i / BUFFER_SIZE) * 4200); // antiphase
  157.         }
  158.        
  159.         return pcm.length;
  160.     }
  161.    
  162.    
  163.     public boolean stream(int buffer) {
  164.         short[] pcm = new short[BUFFER_SIZE*2];
  165.  
  166.         int size = 0;
  167.  
  168.         if ( (size = fillBuffer(pcm)) <= 0) {
  169.             return false;
  170.         }
  171.  
  172.         totalBytes += size;
  173.         debugMsg("stream(): buffer data => " + buffer + " totalBytes:" + totalBytes);
  174.  
  175.        
  176.         ShortBuffer data = ShortBuffer.wrap(pcm, 0, BUFFER_SIZE*2);
  177.         alBufferData(buffer, format, data, BUFFER_SIZE*2);
  178.         check();
  179.  
  180.         return true;
  181.  
  182.     }
  183.  
  184.     protected void empty() {
  185.         int[] queued = new int[1];
  186.         alGetSourcei(source[0], AL_BUFFERS_QUEUED, queued);
  187.  
  188.         while (queued[0] > 0) {
  189.             int[] buffer = new int[1];
  190.             alSourceUnqueueBuffers(source[0], buffer);
  191.  
  192.             check();
  193.             queued[0]--;
  194.         }
  195.     }
  196.  
  197.     protected void check() {
  198.         if (alGetError() != AL_NO_ERROR)
  199.             throw new RuntimeException("OpenAL error raised...");
  200.     }
  201. }
Add Comment
Please, Sign In to add comment