Path: blob/master/SLICK_HOME/src/org/newdawn/slick/openal/AudioImpl.java
1461 views
package org.newdawn.slick.openal;12import org.lwjgl.openal.AL10;3import org.lwjgl.openal.AL11;45/**6* A sound that can be played through OpenAL7*8* @author Kevin Glass9* @author Nathan Sweet <[email protected]>10*/11public class AudioImpl implements Audio {12/** The store from which this sound was loaded */13private SoundStore store;14/** The buffer containing the sound */15private int buffer;16/** The index of the source being used to play this sound */17private int index = -1;1819/** The length of the audio */20private float length;2122/**23* Create a new sound24*25* @param store The sound store from which the sound was created26* @param buffer The buffer containing the sound data27*/28AudioImpl(SoundStore store, int buffer) {29this.store = store;30this.buffer = buffer;3132int bytes = AL10.alGetBufferi(buffer, AL10.AL_SIZE);33int bits = AL10.alGetBufferi(buffer, AL10.AL_BITS);34int channels = AL10.alGetBufferi(buffer, AL10.AL_CHANNELS);35int freq = AL10.alGetBufferi(buffer, AL10.AL_FREQUENCY);3637int samples = bytes / (bits / 8);38length = (samples / (float) freq) / channels;39}4041/**42* Get the ID of the OpenAL buffer holding this data (if any). This method43* is not valid with streaming resources.44*45* @return The ID of the OpenAL buffer holding this data46*/47public int getBufferID() {48return buffer;49}5051/**52*53*/54protected AudioImpl() {5556}5758/**59* @see org.newdawn.slick.openal.Audio#stop()60*/61public void stop() {62if (index != -1) {63store.stopSource(index);64}65}6667/**68* @see org.newdawn.slick.openal.Audio#isPlaying()69*/70public boolean isPlaying() {71if (index != -1) {72return store.isPlaying(index);73}7475return false;76}7778/**79* @see org.newdawn.slick.openal.Audio#playAsSoundEffect(float, float, boolean)80*/81public int playAsSoundEffect(float pitch, float gain, boolean loop) {82index = store.playAsSound(buffer, pitch, gain, loop);83return store.getSource(index);84}858687/**88* @see org.newdawn.slick.openal.Audio#playAsSoundEffect(float, float, boolean, float, float, float)89*/90public int playAsSoundEffect(float pitch, float gain, boolean loop, float x, float y, float z) {91index = store.playAsSoundAt(buffer, pitch, gain, loop, x, y, z);92return store.getSource(index);93}9495/**96* @see org.newdawn.slick.openal.Audio#playAsMusic(float, float, boolean)97*/98public int playAsMusic(float pitch, float gain, boolean loop) {99store.playAsMusic(buffer, pitch, gain, loop);100index = 0;101return store.getSource(0);102}103104/**105* Pause the music currently being played106*/107public static void pauseMusic() {108SoundStore.get().pauseLoop();109}110111/**112* Restart the music currently being paused113*/114public static void restartMusic() {115SoundStore.get().restartLoop();116}117118/**119* @see org.newdawn.slick.openal.Audio#setPosition(float)120*/121public boolean setPosition(float position) {122position = position % length;123124AL10.alSourcef(store.getSource(index), AL11.AL_SEC_OFFSET, position);125if (AL10.alGetError() != 0) {126return false;127}128return true;129}130131/**132* @see org.newdawn.slick.openal.Audio#getPosition()133*/134public float getPosition() {135return AL10.alGetSourcef(store.getSource(index), AL11.AL_SEC_OFFSET);136}137}138139140