/** * boolean LoadALData() * * <p>This function will load our sample data from the disk using the Alut utility and send the * data into OpenAL as a buffer. A source is then also created to play that buffer. */ int loadALData() { // Load wav data into a buffer. AL10.alGenBuffers(buffer); if (AL10.alGetError() != AL10.AL_NO_ERROR) return AL10.AL_FALSE; // Loads the wave file from your file system /*java.io.FileInputStream fin = null; try { fin = new java.io.FileInputStream("FancyPants.wav"); } catch (java.io.FileNotFoundException ex) { ex.printStackTrace(); return AL10.AL_FALSE; } WaveData waveFile = WaveData.create(fin); try { fin.close(); } catch (java.io.IOException ex) { }*/ // Loads the wave file from this class's package in your classpath WaveData waveFile = WaveData.create("FancyPants.wav"); AL10.alBufferData(buffer.get(0), waveFile.format, waveFile.data, waveFile.samplerate); waveFile.dispose(); // Bind the buffer with the source. AL10.alGenSources(source); if (AL10.alGetError() != AL10.AL_NO_ERROR) return AL10.AL_FALSE; AL10.alSourcei(source.get(0), AL10.AL_BUFFER, buffer.get(0)); AL10.alSourcef(source.get(0), AL10.AL_PITCH, 1.0f); AL10.alSourcef(source.get(0), AL10.AL_GAIN, 1.0f); AL10.alSource(source.get(0), AL10.AL_POSITION, sourcePos); AL10.alSource(source.get(0), AL10.AL_VELOCITY, sourceVel); // Do another error check and return. if (AL10.alGetError() == AL10.AL_NO_ERROR) return AL10.AL_TRUE; return AL10.AL_FALSE; }
/** Runs the actual test, using supplied arguments */ protected void execute(String[] args) { if (args.length < 1) { System.out.println("no argument supplied, assuming Footsteps.wav"); args = new String[] {"Footsteps.wav"}; } try { setDisplayMode(); Display.create(); } catch (Exception e) { e.printStackTrace(); } int lastError; Vector3f sourcePosition = new Vector3f(); Vector3f listenerPosition = new Vector3f(); // initialize keyboard try { Keyboard.create(); } catch (Exception e) { e.printStackTrace(); exit(-1); } // create 1 buffer and 1 source IntBuffer buffers = BufferUtils.createIntBuffer(1); IntBuffer sources = BufferUtils.createIntBuffer(1); // al generate buffers and sources buffers.position(0).limit(1); AL10.alGenBuffers(buffers); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } sources.position(0).limit(1); AL10.alGenSources(sources); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } // load wave data WaveData wavefile = WaveData.create(args[0]); // copy to buffers AL10.alBufferData(buffers.get(0), wavefile.format, wavefile.data, wavefile.samplerate); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } // unload file again wavefile.dispose(); // set up source input AL10.alSourcei(sources.get(0), AL10.AL_BUFFER, buffers.get(0)); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } AL10.alSourcef(sources.get(0), AL10.AL_REFERENCE_DISTANCE, 1024.0f); AL10.alSourcef(sources.get(0), AL10.AL_ROLLOFF_FACTOR, 0.5f); // lets loop the sound AL10.alSourcei(sources.get(0), AL10.AL_LOOPING, AL10.AL_TRUE); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } // play source 0 AL10.alSourcePlay(sources.get(0)); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } System.out.println( "Move source with arrow keys\nMove listener with right shift and arrowkeys\nExit with ESC"); while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { Display.update(); Keyboard.poll(); if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { if (Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) { listenerPosition.x -= MOVEMENT; AL10.alListener3f( AL10.AL_POSITION, listenerPosition.x, listenerPosition.y, listenerPosition.z); System.out.println("listenerx: " + listenerPosition.x); } else { sourcePosition.x -= MOVEMENT; AL10.alSource3f( sources.get(0), AL10.AL_POSITION, sourcePosition.x, sourcePosition.y, sourcePosition.z); System.out.println("sourcex: " + sourcePosition.x); } } if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { if (Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) { listenerPosition.x += MOVEMENT; AL10.alListener3f( AL10.AL_POSITION, listenerPosition.x, listenerPosition.y, listenerPosition.z); System.out.println("listenerx: " + listenerPosition.x); } else { sourcePosition.x += MOVEMENT; AL10.alSource3f( sources.get(0), AL10.AL_POSITION, sourcePosition.x, sourcePosition.y, sourcePosition.z); System.out.println("sourcex: " + sourcePosition.x); } } if (Display.isCloseRequested()) { break; } try { Thread.sleep(100); } catch (InterruptedException inte) { } } // stop source 0 AL10.alSourceStop(sources.get(0)); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } // delete buffers and sources sources.position(0).limit(1); AL10.alDeleteSources(sources); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } buffers.position(0).limit(1); AL10.alDeleteBuffers(buffers); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } // shutdown alExit(); }
public Sound(String filename, int type, boolean loop, float volume, float pitch) { String[] split = filename.split("/"); System.out.println("loading " + split[split.length - 1]); try { AL10.alGetError(); } catch (UnsatisfiedLinkError e) { Logger.info("Seems no AL sound device has been created... creating now", "Utility.loadWav"); try { AL.create(); } catch (LWJGLException e1) { Logger.log(e1, "Utility.loadWav", "Unable to create AL sound device"); return; } } this.buf = AL10.alGenBuffers(); if (AL10.alGetError() != AL10.AL_NO_ERROR) { Logger.error(Utility.getALErrorString(AL10.alGetError()), "Sound.Sound"); } if (filename.endsWith(".wav")) { try { WaveData waveFile = WaveData.create(new BufferedInputStream(new FileInputStream(filename))); System.out.println(waveFile == null); AL10.alBufferData(this.buf, waveFile.format, waveFile.data, waveFile.samplerate); waveFile.dispose(); } catch (FileNotFoundException e) { Logger.log(e, "Sound.Sound"); return; } } else if (filename.endsWith(".ogg") || filename.endsWith(".vorbis")) { try { OggDecoder oggDecoder = new OggDecoder(); OggData oggData = oggDecoder.getData(new BufferedInputStream(new FileInputStream(filename))); AL10.alBufferData( this.buf, oggData.channels > 1 ? AL10.AL_FORMAT_STEREO16 : AL10.AL_FORMAT_MONO16, oggData.data, oggData.rate); } catch (FileNotFoundException e) { Logger.log(e, "Sound.Sound"); return; } catch (IOException e) { Logger.log(e, "Sound.Sound"); return; } } this.src = AL10.alGenSources(); if (AL10.alGetError() != AL10.AL_NO_ERROR) { Logger.error(Utility.getALErrorString(AL10.alGetError()), "Utility.loadWav"); return; } AL10.alSourcei(this.src, AL10.AL_BUFFER, this.buf); AL10.alSourcef(this.src, AL10.AL_PITCH, pitch); AL10.alSourcef(this.src, AL10.AL_GAIN, volume); FloatBuffer sourcePos = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[] {0.0f, 0.0f, 0.0f}).rewind(); FloatBuffer sourceVel = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[] {0.0f, 0.0f, 0.0f}).rewind(); AL10.alSource(this.src, AL10.AL_POSITION, sourcePos); AL10.alSource(this.src, AL10.AL_VELOCITY, sourceVel); this.type = type; this.setLoop(loop); this.setPitch(pitch); this.setVolume(volume); Globals.addVolumeChangedListener(this); }