Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
This is an OpenAL/lwjgl based SoundDevice for Nifty GUI. It was extracted out of Slick2D to be
independent of the whole Slick2D library and to be used with none Slick2D based renderes. All credits for the
original code go to Kevin Glass and all Slick2D contributers.
package de.lessvoid.nifty.sound.openal.slick;
import de.lessvoid.nifty.tools.resourceloader.NiftyResourceLoader;
import org.lwjgl.BufferUtils;
import org.lwjgl.Sys;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.AL10;
import org.lwjgl.openal.OpenALException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
/**
* Responsible for holding and playing the sounds used in the game.
*
* @author Kevin Glass
* @author Rockstar setVolume cleanup
*/
public class SoundStore {
private final Logger log = Logger.getLogger(SoundStore.class.getName());
/**
* The single instance of this class
*/
@Nonnull
private static SoundStore store = new SoundStore();
/**
* True if sound effects are turned on
*/
private boolean sounds;
/**
* True if music is turned on
*/
private boolean music;
/**
* True if sound initialisation succeeded
*/
private boolean soundWorks;
/**
* The number of sound sources enabled - default 8
*/
private int sourceCount;
/**
* The map of references to IDs of previously loaded sounds
*/
@Nonnull
private final Map loaded = new HashMap();
/**
* The ID of the buffer containing the music currently being played
*/
private int currentMusic = -1;
/**
* The OpenGL AL sound sources in use
*/
@Nullable
private IntBuffer sources;
/**
* True if the sound system has been initialise
*/
private boolean initiated = false;
/**
* The MODSound to be updated
*/
@Nullable
private MODSound mod;
/**
* The stream to be updated
*/
@Nullable
private OpenALStreamPlayer stream;
/**
* The global music volume setting
*/
private float musicVolume = 1.0f;
/**
* The global sound fx volume setting
*/
private float soundVolume = 1.0f;
/**
* The volume given for the last current music
*/
private float lastCurrentMusicVolume = 1.0f;
/**
* True if the music is paused
*/
private boolean paused;
/**
* True if we're returning deferred versions of resources
*/
private boolean deferred;
/**
* The buffer used to set the velocity of a source
*/
@Nonnull
private final FloatBuffer sourceVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
/**
* The buffer used to set the position of a source
*/
@Nonnull
private final FloatBuffer sourcePos = BufferUtils.createFloatBuffer(3);
/**
* The maximum number of sources
*/
private int maxSources = 64;
/**
* Create a new sound store
*/
private SoundStore() {
}
/**
* Clear out the sound store contents
*/
public void clear() {
store = new SoundStore();
}
/**
* Disable use of the Sound Store
*/
public void disable() {
initiated = true;
}
/**
* True if we should only record the request to load in the intention
* of loading the sound later
*
* @param deferred True if the we should load a token
*/
public void setDeferredLoading(boolean deferred) {
this.deferred = deferred;
}
/**
* Check if we're using deferred loading
*
* @return True if we're loading deferred sounds
*/
public boolean isDeferredLoading() {
return deferred;
}
/**
* Inidicate whether music should be playing
*
* @param music True if music should be played
*/
public void setMusicOn(boolean music) {
if (soundWorks) {
this.music = music;
if (music) {
restartLoop();
setMusicVolume(musicVolume);
} else {
pauseLoop();
}
}
}
/**
* Check if music should currently be playing
*
* @return True if music is currently playing
*/
public boolean isMusicOn() {
return music;
}
/**
* Set the music volume
*
* @param volume The volume for music
*/
public void setMusicVolume(float volume) {
if (volume < 0) {
volume = 0;
}
if (volume > 1) {
volume = 1;
}
musicVolume = volume;
if (soundWorks && sources != null) {
AL10.alSourcef(sources.get(0), AL10.AL_GAIN, lastCurrentMusicVolume * musicVolume);
}
}
/**
* Get the volume scalar of the music that is currently playing.
*
* @return The volume of the music currently playing
*/
public float getCurrentMusicVolume() {
return lastCurrentMusicVolume;
}
/**
* Set the music volume of the current playing music. Does NOT affect the global volume
*
* @param volume The volume for the current playing music
*/
public void setCurrentMusicVolume(float volume) {
if (volume < 0) {
volume = 0;
}
if (volume > 1) {
volume = 1;
}
if (soundWorks && sources != null) {
lastCurrentMusicVolume = volume;
AL10.alSourcef(sources.get(0), AL10.AL_GAIN, lastCurrentMusicVolume * musicVolume);
}
}
/**
* Set the sound volume
*
* @param volume The volume for sound fx
*/
public void setSoundVolume(float volume) {
if (volume < 0) {
volume = 0;
}
soundVolume = volume;
}
/**
* Check if sound works at all
*
* @return True if sound works at all
*/
public boolean soundWorks() {
return soundWorks;
}
/**
* Check if music is currently enabled
*
* @return True if music is currently enabled
*/
public boolean musicOn() {
return music;
}
/**
* Get the volume for sounds
*
* @return The volume for sounds
*/
public float getSoundVolume() {
return soundVolume;
}
/**
* Get the volume for music
*
* @return The volume for music
*/
public float getMusicVolume() {
return musicVolume;
}
/**
* Get the ID of a given source
*
* @param index The ID of a given source
* @return The ID of the given source
*/
public int getSource(int index) {
if (!soundWorks || sources == null) {
return -1;
}
if (index < 0) {
return -1;
}
return sources.get(index);
}
/**
* Indicate whether sound effects should be played
*
* @param sounds True if sound effects should be played
*/
public void setSoundsOn(boolean sounds) {
if (soundWorks) {
this.sounds = sounds;
}
}
/**
* Check if sound effects are currently enabled
*
* @return True if sound effects are currently enabled
*/
public boolean soundsOn() {
return sounds;
}
/**
* Set the maximum number of concurrent sound effects that will be
* attempted
*
* @param max The maximum number of sound effects/music to mix
*/
public void setMaxSources(int max) {
this.maxSources = max;
}
/**
* Initialise the sound effects stored. This must be called
* before anything else will work
*/
public void init() {
if (initiated) {
return;
}
log.fine("Initialising sounds..");
initiated = true;
AccessController.doPrivileged(new PrivilegedAction