Path: blob/master/SLICK_HOME/src/org/newdawn/slick/openal/DeferredSound.java
1461 views
package org.newdawn.slick.openal;12import java.io.IOException;3import java.io.InputStream;45import org.newdawn.slick.loading.DeferredResource;6import org.newdawn.slick.loading.LoadingList;7import org.newdawn.slick.util.Log;89/**10* A sound implementation that can load the actual sound file at a later11* point.12*13* @author kevin14*/15public class DeferredSound extends AudioImpl implements DeferredResource {16/** Indicate a OGG to be loaded */17public static final int OGG = 1;18/** Indicate a WAV to be loaded */19public static final int WAV = 2;20/** Indicate a MOD/XM to be loaded */21public static final int MOD = 3;22/** Indicate a AIF to be loaded */23public static final int AIF = 4;2425/** The type of sound to be loader */26private int type;27/** The location of the sound this proxy wraps */28private String ref;29/** The loaded sound if it's already been brought up */30private Audio target;31/** The input stream to load the sound this proxy wraps from (can be null) */32private InputStream in;3334/**35* Create a new sound on request to load36*37* @param ref The location of the sound to load38* @param type The type of sound to load39* @param in The input stream to load from40*/41public DeferredSound(String ref, InputStream in, int type) {42this.ref = ref;43this.type = type;4445// nasty hack to detect when we're loading from a stream46if (ref.equals(in.toString())) {47this.in = in;48}4950LoadingList.get().add(this);51}5253/**54* Check if the target has already been loaded55*/56private void checkTarget() {57if (target == null) {58throw new RuntimeException("Attempt to use deferred sound before loading");59}60}6162/**63* @see org.newdawn.slick.loading.DeferredResource#load()64*/65public void load() throws IOException {66boolean before = SoundStore.get().isDeferredLoading();67SoundStore.get().setDeferredLoading(false);68if (in != null) {69switch (type) {70case OGG:71target = SoundStore.get().getOgg(in);72break;73case WAV:74target = SoundStore.get().getWAV(in);75break;76case MOD:77target = SoundStore.get().getMOD(in);78break;79case AIF:80target = SoundStore.get().getAIF(in);81break;82default:83Log.error("Unrecognised sound type: "+type);84break;85}86} else {87switch (type) {88case OGG:89target = SoundStore.get().getOgg(ref);90break;91case WAV:92target = SoundStore.get().getWAV(ref);93break;94case MOD:95target = SoundStore.get().getMOD(ref);96break;97case AIF:98target = SoundStore.get().getAIF(ref);99break;100default:101Log.error("Unrecognised sound type: "+type);102break;103}104}105SoundStore.get().setDeferredLoading(before);106}107108/**109* @see org.newdawn.slick.openal.AudioImpl#isPlaying()110*/111public boolean isPlaying() {112checkTarget();113114return target.isPlaying();115}116117/**118* @see org.newdawn.slick.openal.AudioImpl#playAsMusic(float, float, boolean)119*/120public int playAsMusic(float pitch, float gain, boolean loop) {121checkTarget();122return target.playAsMusic(pitch, gain, loop);123}124125/**126* @see org.newdawn.slick.openal.AudioImpl#playAsSoundEffect(float, float, boolean)127*/128public int playAsSoundEffect(float pitch, float gain, boolean loop) {129checkTarget();130return target.playAsSoundEffect(pitch, gain, loop);131}132133/**134* Play this sound as a sound effect135*136* @param pitch The pitch of the play back137* @param gain The gain of the play back138* @param loop True if we should loop139* @param x The x position of the sound140* @param y The y position of the sound141* @param z The z position of the sound142*/143public int playAsSoundEffect(float pitch, float gain, boolean loop, float x, float y, float z) {144checkTarget();145return target.playAsSoundEffect(pitch, gain, loop, x, y, z);146}147148/**149* @see org.newdawn.slick.openal.AudioImpl#stop()150*/151public void stop() {152checkTarget();153target.stop();154}155156/**157* @see org.newdawn.slick.loading.DeferredResource#getDescription()158*/159public String getDescription() {160return ref;161}162163}164165166