From ad1144dc8c517d82af2d7606fb3e7a21a07bea72 Mon Sep 17 00:00:00 2001 From: Carsten Weisse Date: Wed, 27 Oct 2004 16:51:32 +0000 Subject: better joal sound driver bugfix (item #1026876) --- src/jake2/sound/joal/Channel.java | 328 ++++++++++++++++++++++++++++---- src/jake2/sound/joal/JOALSoundImpl.java | 284 ++------------------------- src/jake2/sound/joal/PlaySound.java | 153 +++++++++++++++ 3 files changed, 462 insertions(+), 303 deletions(-) create mode 100644 src/jake2/sound/joal/PlaySound.java (limited to 'src/jake2/sound') 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 ; isound 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.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"); + } + } +} -- cgit v1.2.3