diff options
author | Carsten Weisse <[email protected]> | 2004-10-27 16:51:32 +0000 |
---|---|---|
committer | Carsten Weisse <[email protected]> | 2004-10-27 16:51:32 +0000 |
commit | ad1144dc8c517d82af2d7606fb3e7a21a07bea72 (patch) | |
tree | 04331303e82d78cbee547d4918c9158087ea7cf9 /src/jake2 | |
parent | 337f732145b18ab28c3c1b38f986c32dd6de3eb7 (diff) |
better joal sound driver
bugfix (item #1026876)
Diffstat (limited to 'src/jake2')
-rw-r--r-- | src/jake2/sound/joal/Channel.java | 328 | ||||
-rw-r--r-- | src/jake2/sound/joal/JOALSoundImpl.java | 284 | ||||
-rw-r--r-- | src/jake2/sound/joal/PlaySound.java | 153 |
3 files changed, 462 insertions, 303 deletions
diff --git a/src/jake2/sound/joal/Channel.java b/src/jake2/sound/joal/Channel.java index ced496f..08aa584 100644 --- a/src/jake2/sound/joal/Channel.java +++ b/src/jake2/sound/joal/Channel.java @@ -3,7 +3,7 @@ * * Copyright (C) 2003 * - * $Id: Channel.java,v 1.1 2004-07-09 06:50:52 hzi Exp $ + * $Id: Channel.java,v 1.2 2004-10-27 16:51:32 cawe Exp $ */ /* Copyright (C) 1997-2001 Id Software, Inc. @@ -26,6 +26,18 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package jake2.sound.joal; +import java.util.*; + +import net.java.games.joal.AL; +import jake2.Defines; +import jake2.Globals; +import jake2.client.CL_ents; +import jake2.game.entity_state_t; +import jake2.qcommon.Com; +import jake2.sound.sfx_t; +import jake2.sound.sfxcache_t; +import jake2.util.Math3D; + /** * Channel * @@ -36,49 +48,299 @@ public class Channel { final static int LISTENER = 0; final static int FIXED = 1; final static int DYNAMIC = 2; - - int entnum; - int entchannel; - int bufferId; - float rolloff; - boolean autosound = false; - int sourceId; - boolean active = false; - boolean modified = false; - boolean bufferChanged = false; + final static int MAX_CHANNELS = 32; + final static float[] NULLVECTOR = {0, 0, 0}; + private static AL al; + private static Channel[] channels = new Channel[MAX_CHANNELS]; + private static int[] sources = new int[MAX_CHANNELS]; + // a reference of JOALSoundImpl.buffers + private static int[] buffers; + private static Map looptable = new Hashtable(MAX_CHANNELS); + + private static boolean isInitialized = false; + private static int numChannels; + // sound attributes - int type; - int entity; - float[] origin = {0, 0, 0}; - - Channel(int sourceId) { + private int type; + private int entnum; + private int entchannel; + private int bufferId; + private int sourceId; + // private float volume; + private float rolloff; + private float[] origin = {0, 0, 0}; + + // update flags + private boolean autosound = false; + private boolean active = false; + private boolean modified = false; + private boolean bufferChanged = false; + + private Channel(int sourceId) { this.sourceId = sourceId; clear(); } - void addListener() { - type = LISTENER; + private void clear() { + entnum = entchannel = bufferId = -1; + bufferChanged = false; + // volume = 1.0f; + rolloff = 0; + autosound = false; + active = false; + modified = false; + } + + static int init(AL al, int[] buffers, float masterVolume) { + Channel.al = al; + Channel.buffers = buffers; + // create channels + int sourceId; + int[] tmp = {0}; + int error; + for (int i = 0; i < MAX_CHANNELS; i++) { + + al.alGenSources(1, tmp); + sourceId = tmp[0]; + + if (sourceId <= 0) break; + + sources[i] = sourceId; + + channels[i] = new Channel(sourceId); + numChannels++; + + // set default values for AL sources + al.alSourcef (sourceId, AL.AL_GAIN, masterVolume); + al.alSourcef (sourceId, AL.AL_PITCH, 1.0f); + al.alSourcei (sourceId, AL.AL_SOURCE_ABSOLUTE, AL.AL_TRUE); + al.alSourcefv(sourceId, AL.AL_VELOCITY, NULLVECTOR); + al.alSourcei (sourceId, AL.AL_LOOPING, AL.AL_FALSE); + al.alSourcef (sourceId, AL.AL_REFERENCE_DISTANCE, 300.0f); + al.alSourcef (sourceId, AL.AL_MIN_GAIN, 0.0005f); + al.alSourcef (sourceId, AL.AL_MAX_GAIN, 1.0f); + } + isInitialized = true; + return numChannels; + } + + static void reset() { + for (int i = 0; i < numChannels; i++) { + al.alSourceStop(sources[i]); + al.alSourcei(sources[i], AL.AL_BUFFER, 0); + channels[i].clear(); + } + } + + static void shutdown() { + al.alDeleteSources(numChannels, sources); + numChannels = 0; + isInitialized = false; } + + static void addPlaySounds() { + while (Channel.assign(PlaySound.nextPlayableSound())); + } + + private static boolean assign(PlaySound ps) { + if (ps == null) return false; + Channel ch = null; + int i; + for (i = 0; i < numChannels; i++) { + ch = channels[i]; + + if (ps.entchannel != 0 && ch.entnum == ps.entnum && ch.entchannel == ps.entchannel) { + // always override sound from same entity + if (ch.bufferId != ps.bufferId) { + al.alSourceStop(ch.sourceId); + } + break; + } + + // don't let monster sounds override player sounds + if ((ch.entnum == Globals.cl.playernum+1) && (ps.entnum != Globals.cl.playernum+1) && ch.bufferId != -1) + continue; + + // looking for a free AL source + if (!ch.active) { + break; + } + } - void addFixed(float[] origin) { - type = FIXED; - this.origin = origin; + if (i == numChannels) + return false; + + ch.type = ps.type; + if (ps.type == Channel.FIXED) + Math3D.VectorCopy(ps.origin, ch.origin); + ch.entnum = ps.entnum; + ch.entchannel = ps.entchannel; + ch.bufferChanged = (ch.bufferId != ps.bufferId); + ch.bufferId = ps.bufferId; + ch.rolloff = ps.attenuation * 2; + //ch.volume = ps.volume; + ch.active = true; + ch.modified = true; + return true; } + + private static Channel pickForLoop(int bufferId, float attenuation) { + Channel ch; + for (int i = 0; i < numChannels; i++) { + ch = channels[i]; + // looking for a free AL source + if (!ch.active) { + ch.entnum = 0; + ch.entchannel = 0; + ch.bufferChanged = (ch.bufferId != bufferId); + ch.bufferId = bufferId; + ch.rolloff = attenuation * 2; + ch.active = true; + ch.modified = true; + return ch; + } + } + return null; + } - void addDynamic(int entity) { - type = DYNAMIC; - this.entity = entity; + static void playAllSounds(float[] listenerOrigin, float masterVolume) { + float[] sourceOrigin = {0, 0, 0}; + float[] entityOrigin = {0, 0, 0}; + Channel ch; + int sourceId; + int state; + + for (int i = 0; i < numChannels; i++) { + ch = channels[i]; + if (ch.active) { + sourceId = ch.sourceId; + switch (ch.type) { + case Channel.LISTENER: + Math3D.VectorCopy(listenerOrigin, sourceOrigin); + break; + case Channel.DYNAMIC: + CL_ents.GetEntitySoundOrigin(ch.entnum, entityOrigin); + convertVector(entityOrigin, sourceOrigin); + break; + case Channel.FIXED: + convertVector(ch.origin, sourceOrigin); + break; + } + + if (ch.modified) { + if (ch.bufferChanged) { + al.alSourcei(sourceId, AL.AL_BUFFER, ch.bufferId); + } +// al.alSourcef (sourceId, AL.AL_GAIN, masterVolume * ch.volume); + al.alSourcef (sourceId, AL.AL_GAIN, masterVolume); + al.alSourcef (sourceId, AL.AL_ROLLOFF_FACTOR, ch.rolloff); + al.alSourcefv(sourceId, AL.AL_POSITION, sourceOrigin); + al.alSourcePlay(sourceId); + ch.modified = false; + } else { + state = al.alGetSourcei(sourceId, AL.AL_SOURCE_STATE); + if (state == AL.AL_PLAYING) { + al.alSourcefv(sourceId, AL.AL_POSITION, sourceOrigin); + } else { + ch.clear(); + } + } + ch.autosound = false; + } + } } - void clear() { - entnum = -1; - entchannel = -1; - bufferId = -1; - bufferChanged = false; - rolloff = 0; - autosound = false; - active = false; - modified = false; + /* + * adddLoopSounds + * Entities with a ->sound field will generated looped sounds + * that are automatically started, stopped, and merged together + * as the entities are sent to the client + */ + static void addLoopSounds() { + + if ((Globals.cl_paused.value != 0.0f) || (Globals.cls.state != Globals.ca_active) || !Globals.cl.sound_prepped) { + removeUnusedLoopSounds(); + return; + } + + Channel ch; + sfx_t sfx; + sfxcache_t sc; + int num; + entity_state_t ent; + Object key; + int sound = 0; + + for (int i=0 ; i<Globals.cl.frame.num_entities ; i++) { + num = (Globals.cl.frame.parse_entities + i)&(Defines.MAX_PARSE_ENTITIES-1); + ent = Globals.cl_parse_entities[num]; + sound = ent.sound; + + if (sound == 0) continue; + + key = new Integer(ent.number); + ch = (Channel)looptable.get(key); + + if (ch != null) { + // keep on looping + ch.autosound = true; + Math3D.VectorCopy(ent.origin, ch.origin); + continue; + } + + sfx = Globals.cl.sound_precache[sound]; + if (sfx == null) + continue; // bad sound effect + + sc = sfx.cache; + if (sc == null) + continue; + + // allocate a channel + ch = Channel.pickForLoop(buffers[sfx.bufferId], 6); + if (ch == null) + break; + + ch.type = FIXED; + Math3D.VectorCopy(ent.origin, ch.origin); + ch.autosound = true; + + looptable.put(key, ch); + al.alSourcei(ch.sourceId, AL.AL_LOOPING, AL.AL_TRUE); + } + + removeUnusedLoopSounds(); + + } + + private static void removeUnusedLoopSounds() { + Channel ch; + // stop unused loopsounds + for (Iterator iter = looptable.values().iterator(); iter.hasNext();) { + ch = (Channel)iter.next(); + if (!ch.autosound) { + al.alSourceStop(ch.sourceId); + al.alSourcei(ch.sourceId, AL.AL_LOOPING, AL.AL_FALSE); + iter.remove(); + ch.clear(); + } + } + } + + + static void convertVector(float[] from, float[] to) { + to[0] = from[0]; + to[1] = from[2]; + to[2] = -from[1]; + } + + static void convertOrientation(float[] forward, float[] up, float[] orientation) { + orientation[0] = forward[0]; + orientation[1] = forward[2]; + orientation[2] = -forward[1]; + orientation[3] = up[0]; + orientation[4] = up[2]; + orientation[5] = -up[1]; } } diff --git a/src/jake2/sound/joal/JOALSoundImpl.java b/src/jake2/sound/joal/JOALSoundImpl.java index 5a1df21..de4615a 100644 --- a/src/jake2/sound/joal/JOALSoundImpl.java +++ b/src/jake2/sound/joal/JOALSoundImpl.java @@ -2,7 +2,7 @@ * JOALSoundImpl.java * Copyright (C) 2004 * - * $Id: JOALSoundImpl.java,v 1.7 2004-10-21 02:45:55 cawe Exp $ + * $Id: JOALSoundImpl.java,v 1.8 2004-10-27 16:51:32 cawe Exp $ */ package jake2.sound.joal; @@ -40,12 +40,7 @@ public final class JOALSoundImpl implements Sound { cvar_t s_volume; private static final int MAX_SFX = Defines.MAX_SOUNDS * 2; - private static final int MAX_CHANNELS = 32; - private int[] buffers = new int[MAX_SFX]; - private int[] sources = new int[MAX_CHANNELS]; - private Channel[] channels = null; - private int num_channels = 0; // singleton private JOALSoundImpl() { @@ -78,7 +73,8 @@ public final class JOALSoundImpl implements Sound { } al.alGenBuffers(MAX_SFX, buffers); s_volume = Cvar.Get("s_volume", "0.7", Defines.CVAR_ARCHIVE); - initChannels(); + int count = Channel.init(al, buffers, s_volume.value); + Com.Printf("... using " + count + " channels\n"); al.alDistanceModel(AL.AL_INVERSE_DISTANCE_CLAMPED); Cmd.AddCommand("play", new xcommand_t() { public void execute() { @@ -103,7 +99,6 @@ public final class JOALSoundImpl implements Sound { num_sfx = 0; - Com.Printf("sound sampling rate: 44100Hz\n"); StopAllSounds(); @@ -145,7 +140,6 @@ public final class JOALSoundImpl implements Sound { } } - void exitOpenAL() { // Get the current context. ALC.Context curContext = alc.alcGetCurrentContext(); @@ -158,41 +152,6 @@ public final class JOALSoundImpl implements Sound { alc.alcCloseDevice(curDevice); } - private void initChannels() { - - // create channels - channels = new Channel[MAX_CHANNELS]; - - int sourceId; - int[] tmp = {0}; - int error; - for (int i = 0; i < MAX_CHANNELS; i++) { - - al.alGenSources(1, tmp); - sourceId = tmp[0]; - - //if ((error = al.alGetError()) != AL.AL_NO_ERROR) break; - if (sourceId <= 0) break; - - sources[i] = sourceId; - - channels[i] = new Channel(sourceId); - num_channels++; - - // set default values for AL sources - al.alSourcef (sourceId, AL.AL_GAIN, s_volume.value); - al.alSourcef (sourceId, AL.AL_PITCH, 1.0f); - al.alSourcei (sourceId, AL.AL_SOURCE_ABSOLUTE, AL.AL_TRUE); - al.alSourcefv(sourceId, AL.AL_VELOCITY, NULLVECTOR); - al.alSourcei (sourceId, AL.AL_LOOPING, AL.AL_FALSE); - al.alSourcef (sourceId, AL.AL_REFERENCE_DISTANCE, 300.0f); - al.alSourcef (sourceId, AL.AL_MIN_GAIN, 0.0005f); - al.alSourcef (sourceId, AL.AL_MAX_GAIN, 1.0f); - } - Com.Printf("... using " + num_channels + " channels\n"); - } - - /* (non-Javadoc) * @see jake2.sound.SoundImpl#RegisterSound(jake2.sound.sfx_t) */ @@ -234,7 +193,7 @@ public final class JOALSoundImpl implements Sound { */ public void Shutdown() { StopAllSounds(); - al.alDeleteSources(sources.length, sources); + Channel.shutdown(); al.alDeleteBuffers(buffers.length, buffers); exitOpenAL(); @@ -250,13 +209,8 @@ public final class JOALSoundImpl implements Sound { known_sfx[i].clear(); } num_sfx = 0; - num_channels = 0; } - private final static float[] NULLVECTOR = {0, 0, 0}; - private float[] entityOrigin = {0, 0, 0}; - private float[] sourceOrigin = {0, 0, 0}; - /* (non-Javadoc) * @see jake2.sound.SoundImpl#StartSound(float[], int, int, jake2.sound.sfx_t, float, float, float) */ @@ -274,59 +228,9 @@ public final class JOALSoundImpl implements Sound { if (attenuation != Defines.ATTN_STATIC) attenuation *= 0.5f; - Channel ch = pickChannel(entnum, entchannel, buffers[sfx.bufferId], attenuation); - - if (ch == null) return; - - if (entnum == Globals.cl.playernum + 1) { - ch.addListener(); - } else if (origin != null) { - ch.addFixed(origin); - } else { - ch.addDynamic(entnum); - } + PlaySound.allocate(origin, entnum, entchannel, buffers[sfx.bufferId], fvol, attenuation, timeofs); } - - Channel pickChannel(int entnum, int entchannel, int bufferId, float rolloff) { - - Channel ch = null; - int state; - int i; - - for (i = 0; i < num_channels; i++) { - ch = channels[i]; - if (entchannel != 0 && ch.entnum == entnum && ch.entchannel == entchannel) { - // always override sound from same entity - break; - } - - // don't let monster sounds override player sounds - if ((ch.entnum == Globals.cl.playernum+1) && (entnum != Globals.cl.playernum+1) && ch.bufferId != -1) - continue; - - // looking for a free AL source - if (!ch.active) { - break; - } - } - - if (i == num_channels) - return null; - - ch.entnum = entnum; - ch.entchannel = entchannel; - if (ch.bufferId != bufferId) { - ch.bufferId = bufferId; - ch.bufferChanged = true; - } - ch.rolloff = rolloff * 2; - ch.active = true; - ch.modified = true; - - return ch; - } - private float[] listenerOrigin = {0, 0, 0}; private float[] listenerOrientation = {0, 0, 0, 0, 0, 0}; private IntBuffer eaxEnv = Lib.newIntBuffer(1); @@ -343,11 +247,10 @@ public final class JOALSoundImpl implements Sound { * @see jake2.sound.SoundImpl#Update(float[], float[], float[], float[]) */ public void Update(float[] origin, float[] forward, float[] right, float[] up) { - - convertVector(origin, listenerOrigin); + Channel.convertVector(origin, listenerOrigin); al.alListenerfv(AL.AL_POSITION, listenerOrigin); - convertOrientation(forward, up, listenerOrientation); + Channel.convertOrientation(forward, up, listenerOrientation); al.alListenerfv(AL.AL_ORIENTATION, listenerOrientation); if (eax != null) { @@ -370,174 +273,18 @@ public final class JOALSoundImpl implements Sound { eax.EAXSet(EAX_LISTENER, EAX.DSPROPERTY_EAXLISTENER_ENVIRONMENT | EAX.DSPROPERTY_EAXLISTENER_DEFERRED, 0, eaxEnv, 4); } } - - AddLoopSounds(origin); - playChannels(listenerOrigin); - } - - Map looptable = new Hashtable(MAX_CHANNELS); - - /* - ================== - S_AddLoopSounds - Entities with a ->sound field will generated looped sounds - that are automatically started, stopped, and merged together - as the entities are sent to the client - ================== - */ - void AddLoopSounds(float[] listener) { - - if (Globals.cl_paused.value != 0.0f) { - removeUnusedLoopSounds(); - return; - } - - if (Globals.cls.state != Globals.ca_active) { - removeUnusedLoopSounds(); - return; - } - - if (!Globals.cl.sound_prepped) { - removeUnusedLoopSounds(); - return; - } - - Channel ch; - sfx_t sfx; - sfxcache_t sc; - int num; - entity_state_t ent; - Object key; - int sound = 0; - - for (int i=0 ; i<Globals.cl.frame.num_entities ; i++) { - num = (Globals.cl.frame.parse_entities + i)&(Defines.MAX_PARSE_ENTITIES-1); - ent = Globals.cl_parse_entities[num]; - sound = ent.sound; - - if (sound == 0) continue; - - key = new Integer(ent.number); - ch = (Channel)looptable.get(key); - - if (ch != null) { - // keep on looping - ch.autosound = true; - ch.origin = ent.origin; - continue; - } - - sfx = Globals.cl.sound_precache[sound]; - if (sfx == null) - continue; // bad sound effect - - sc = sfx.cache; - if (sc == null) - continue; - - // allocate a channel - ch = pickChannel(0, 0, buffers[sfx.bufferId], 6); - if (ch == null) - break; - - ch.addFixed(ent.origin); - ch.autosound = true; - - looptable.put(key, ch); - al.alSourcei(ch.sourceId, AL.AL_LOOPING, AL.AL_TRUE); - } - - removeUnusedLoopSounds(); - - } - - void removeUnusedLoopSounds() { - Channel ch; - // stop unused loopsounds - for (Iterator iter = looptable.values().iterator(); iter.hasNext();) { - ch = (Channel)iter.next(); - if (!ch.autosound) { - al.alSourceStop(ch.sourceId); - al.alSourcei(ch.sourceId, AL.AL_LOOPING, AL.AL_FALSE); - iter.remove(); - ch.clear(); - } - } - } - - void playChannels(float[] listenerOrigin) { - - float[] sourceOrigin = {0, 0, 0}; - float[] entityOrigin = {0, 0, 0}; - Channel ch; - int sourceId; - int state; - - for (int i = 0; i < num_channels; i++) { - ch = channels[i]; - if (ch.active) { - sourceId = ch.sourceId; - - switch (ch.type) { - case Channel.LISTENER: - Math3D.VectorCopy(listenerOrigin, sourceOrigin); - break; - case Channel.DYNAMIC: - CL_ents.GetEntitySoundOrigin(ch.entity, entityOrigin); - convertVector(entityOrigin, sourceOrigin); - break; - case Channel.FIXED: - convertVector(ch.origin, sourceOrigin); - break; - } - - if (ch.modified) { - if (ch.bufferChanged) - al.alSourcei (sourceId, AL.AL_BUFFER, ch.bufferId); - - al.alSourcef (sourceId, AL.AL_GAIN, s_volume.value); - al.alSourcef (sourceId, AL.AL_ROLLOFF_FACTOR, ch.rolloff); - al.alSourcefv(sourceId, AL.AL_POSITION, sourceOrigin); - al.alSourcePlay(sourceId); - ch.modified = false; - } else { - state = al.alGetSourcei(ch.sourceId, AL.AL_SOURCE_STATE); - if (state == AL.AL_PLAYING) { - al.alSourcefv(sourceId, AL.AL_POSITION, sourceOrigin); - } else { - ch.clear(); - } - } - ch.autosound = false; - } - } + Channel.addLoopSounds(); + Channel.addPlaySounds(); + Channel.playAllSounds(listenerOrigin, s_volume.value); } /* (non-Javadoc) * @see jake2.sound.SoundImpl#StopAllSounds() */ public void StopAllSounds() { - for (int i = 0; i < num_channels; i++) { - al.alSourceStop(sources[i]); - al.alSourcei(sources[i], AL.AL_BUFFER, 0); - channels[i].clear(); - } - } - - static void convertVector(float[] from, float[] to) { - to[0] = from[0]; - to[1] = from[2]; - to[2] = -from[1]; - } - - static void convertOrientation(float[] forward, float[] up, float[] orientation) { - orientation[0] = forward[0]; - orientation[1] = forward[2]; - orientation[2] = -forward[1]; - orientation[3] = up[0]; - orientation[4] = up[2]; - orientation[5] = -up[1]; + PlaySound.reset(); + Channel.reset(); } /* (non-Javadoc) @@ -547,7 +294,6 @@ public final class JOALSoundImpl implements Sound { return "joal"; } - int s_registration_sequence; boolean s_registering; @@ -758,7 +504,7 @@ public final class JOALSoundImpl implements Sound { Com.Printf("S_StartLocalSound: can't cache " + sound + "\n"); return; } - StartSound(null, Globals.cl.playernum + 1, 0, sfx, 1, 1, 0); + StartSound(null, Globals.cl.playernum + 1, 0, sfx, 1, 1, 0.0f); } /* (non-Javadoc) @@ -788,7 +534,7 @@ public final class JOALSoundImpl implements Sound { name += ".wav"; sfx = RegisterSound(name); - StartSound(null, Globals.cl.playernum + 1, 0, sfx, 1.0f, 1.0f, 0.0f); + StartLocalSound(name); i++; } } @@ -824,11 +570,9 @@ public final class JOALSoundImpl implements Sound { } void SoundInfo_f() { - Com.Printf("%5d stereo\n", new Vargs(1).add(1)); Com.Printf("%5d samples\n", new Vargs(1).add(22050)); Com.Printf("%5d samplebits\n", new Vargs(1).add(16)); Com.Printf("%5d speed\n", new Vargs(1).add(44100)); } - } diff --git a/src/jake2/sound/joal/PlaySound.java b/src/jake2/sound/joal/PlaySound.java new file mode 100644 index 0000000..da0de95 --- /dev/null +++ b/src/jake2/sound/joal/PlaySound.java @@ -0,0 +1,153 @@ +/* + * Created on Oct 26, 2004 + * + * TODO To change the template for this generated file go to + * Window - Preferences - Java - Code Style - Code Templates + */ +package jake2.sound.joal; + +import java.util.Vector; + +import jake2.Globals; +import jake2.sound.sfx_t; +import jake2.util.Math3D; + +/** + * @author cwei + * + * TODO To change the template for this generated type comment go to + * Window - Preferences - Java - Code Style - Code Templates + */ +public class PlaySound { + + final static int MAX_PLAYSOUNDS = 128; + + // list with sentinel + private static PlaySound freeList; + private static PlaySound playableList; + + private static PlaySound[] backbuffer = new PlaySound[MAX_PLAYSOUNDS]; + static { + for (int i = 0; i < backbuffer.length; i++) { + backbuffer[i] = new PlaySound(); + } + // init the sentinels + freeList = new PlaySound(); + playableList = new PlaySound(); + // reset the lists + reset(); + } + + // sound attributes + int type; + int entnum; + int entchannel; + int bufferId; + // float volume; + float attenuation; + float[] origin = {0,0,0}; + + // begin time in ms + private long beginTime; + + // for linked list + private PlaySound prev, next; + + private PlaySound() { + prev = next = null; + this.clear(); + } + + private void clear() { + type = bufferId = entnum = entchannel = -1; + // volume = attenuation = beginTime = 0; + attenuation = beginTime = 0; + // Math3D.VectorClear(origin); + } + + static void reset() { + // init the sentinels + freeList.next = freeList.prev = freeList; + playableList.next = playableList.prev = playableList; + + // concat the the freeList + PlaySound ps; + for (int i = 0; i < backbuffer.length; i++) { + ps = backbuffer[i]; + ps.clear(); + ps.prev = freeList; + ps.next = freeList.next; + ps.prev.next = ps; + ps.next.prev = ps; + } + } + + static PlaySound nextPlayableSound() { + PlaySound ps = null; + while (true) { + ps = playableList.next; + if (ps == playableList || ps.beginTime > Globals.cl.time) + return null; + PlaySound.release(ps); + return ps; + } + } + + private static PlaySound get() { + PlaySound ps = freeList.next; + if (ps == freeList) + return null; + + ps.prev.next = ps.next; + ps.next.prev = ps.prev; + return ps; + } + + private static void add(PlaySound ps) { + + PlaySound sort = playableList.next; + + for (; sort != playableList && sort.beginTime < ps.beginTime; sort = sort.next); + ps.next = sort; + ps.prev = sort.prev; + ps.next.prev = ps; + ps.prev.next = ps; + } + + private static void release(PlaySound ps) { + ps.prev.next = ps.next; + ps.next.prev = ps.prev; + // add to free list + ps.next = freeList.next; + freeList.next.prev = ps; + ps.prev = freeList; + freeList.next = ps; + } + + static void allocate(float[] origin, int entnum, int entchannel, + int bufferId, float volume, float attenuation, float timeoffset) { + + PlaySound ps = PlaySound.get(); + + if (ps != null) { + // find the right sound type + if (entnum == Globals.cl.playernum + 1) { + ps.type = Channel.LISTENER; + } else if (origin != null) { + ps.type = Channel.FIXED; + Math3D.VectorCopy(origin, ps.origin); + } else { + ps.type = Channel.DYNAMIC; + } + ps.entnum = entnum; + ps.entchannel = entchannel; + ps.bufferId = bufferId; + // ps.volume = volume; + ps.attenuation = attenuation; + ps.beginTime = Globals.cl.time + (long)(timeoffset * 1000); + PlaySound.add(ps); + } else { + System.err.println("PlaySounds out of Limit"); + } + } +} |