public void dispose() { if (audio.noDevice) return; if (bufferID == -1) return; audio.freeBuffer(bufferID); alDeleteBuffers(bufferID); bufferID = -1; audio.forget(this); }
public void dispose() { if (buffers == null) return; if (sourceID != -1) { audio.freeSource(sourceID); sourceID = -1; } alDeleteBuffers(buffers); buffers = null; }
/** * Stops and releases all sources, clears each of the specified Audio buffers, destroys the OpenAL * context, and resets SoundStore for future use. * * <p>Calling SoundStore.get().init() will re-initialize the OpenAL context after a call to * destroyOpenAL (Note: AudioLoader.getXXX calls init for you). * * @author davedes (http://slick.ninjacave.com/forum/viewtopic.php?t=3920) */ private static void destroyOpenAL() { if (!trackExists()) return; stop(); try { // get Music object's (private) Audio object reference Field sound = player.getClass().getDeclaredField("sound"); sound.setAccessible(true); Audio audio = (Audio) (sound.get(player)); // first clear the sources allocated by SoundStore int max = SoundStore.get().getSourceCount(); IntBuffer buf = BufferUtils.createIntBuffer(max); for (int i = 0; i < max; i++) { int source = SoundStore.get().getSource(i); buf.put(source); // stop and detach any buffers at this source AL10.alSourceStop(source); AL10.alSourcei(source, AL10.AL_BUFFER, 0); } buf.flip(); AL10.alDeleteSources(buf); int exc = AL10.alGetError(); if (exc != AL10.AL_NO_ERROR) { throw new SlickException("Could not clear SoundStore sources, err: " + exc); } // delete any buffer data stored in memory, too... if (audio != null && audio.getBufferID() != 0) { buf = BufferUtils.createIntBuffer(1).put(audio.getBufferID()); buf.flip(); AL10.alDeleteBuffers(buf); exc = AL10.alGetError(); if (exc != AL10.AL_NO_ERROR) { throw new SlickException( "Could not clear buffer " + audio.getBufferID() + ", err: " + exc); } } // clear OpenAL AL.destroy(); // reset SoundStore so that next time we create a Sound/Music, it will reinit SoundStore.get().clear(); player = null; } catch (Exception e) { ErrorHandler.error("Failed to destroy OpenAL.", e, true); } }
/** * void killALData() * * <p>We have allocated memory for our buffers and sources which needs to be returned to the * system. This function frees that memory. */ void killALData() { AL10.alDeleteSources(source); AL10.alDeleteBuffers(buffer); }
/** 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 void finalize() { AL10.alDeleteSources(this.src); AL10.alDeleteBuffers(this.buf); }