From: Carsten W. <ca...@us...> - 2005-12-25 18:16:46
|
Update of /cvsroot/jake2/jake2/src/jake2/sound/lwjgl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28775/src/jake2/sound/lwjgl Modified Files: Tag: render-refactoring LWJGLSoundImpl.java Channel.java Log Message: merge with current HEAD Index: Channel.java =================================================================== RCS file: /cvsroot/jake2/jake2/src/jake2/sound/lwjgl/Channel.java,v retrieving revision 1.6 retrieving revision 1.6.6.1 diff -C2 -d -r1.6 -r1.6.6.1 *** Channel.java 8 May 2005 13:37:46 -0000 1.6 --- Channel.java 25 Dec 2005 18:16:38 -0000 1.6.6.1 *************** *** 31,41 **** import jake2.client.CL_ents; import jake2.game.entity_state_t; ! import jake2.sound.sfx_t; ! import jake2.sound.sfxcache_t; import jake2.util.Lib; import jake2.util.Math3D; ! import java.nio.FloatBuffer; ! import java.nio.IntBuffer; import java.util.Hashtable; import java.util.Iterator; --- 31,40 ---- import jake2.client.CL_ents; import jake2.game.entity_state_t; ! import jake2.qcommon.Com; ! import jake2.sound.*; import jake2.util.Lib; import jake2.util.Math3D; ! import java.nio.*; import java.util.Hashtable; import java.util.Iterator; *************** *** 64,68 **** private static boolean isInitialized = false; ! private static int numChannels; // sound attributes --- 63,71 ---- private static boolean isInitialized = false; ! private static int numChannels; ! ! // stream handling ! private static boolean streamingEnabled = false; ! private static int streamQueue = 0; // sound attributes *************** *** 105,109 **** // create channels int sourceId; - int error; for (int i = 0; i < MAX_CHANNELS; i++) { --- 108,111 ---- *************** *** 111,114 **** --- 113,117 ---- sourceId = tmp.get(0); + // can't generate more sources if (sourceId <= 0) break; *************** *** 146,149 **** --- 149,228 ---- } + static void enableStreaming() { + if (streamingEnabled) return; + + // use the last source + numChannels--; + streamingEnabled = true; + streamQueue = 0; + + int source = channels[numChannels].sourceId; + AL10.alSourcei (source, AL10.AL_SOURCE_RELATIVE, AL10.AL_TRUE); + AL10.alSourcef(source, AL10.AL_GAIN, 1.0f); + channels[numChannels].volumeChanged = true; + + Com.DPrintf("streaming enabled\n"); + } + + static void disableStreaming() { + if (!streamingEnabled) return; + unqueueStreams(); + int source = channels[numChannels].sourceId; + AL10.alSourcei (source, AL10.AL_SOURCE_ABSOLUTE, AL10.AL_TRUE); + + // free the last source + numChannels++; + streamingEnabled = false; + Com.DPrintf("streaming disabled\n"); + } + + static void unqueueStreams() { + if (!streamingEnabled) return; + int source = channels[numChannels].sourceId; + + // stop streaming + AL10.alSourceStop(source); + int count = AL10.alGetSourcei(source, AL10.AL_BUFFERS_QUEUED); + Com.DPrintf("unqueue " + count + " buffers\n"); + while (count-- > 0) { + AL10.alSourceUnqueueBuffers(source, tmp); + } + streamQueue = 0; + } + + static void updateStream(ByteBuffer samples, int count, int format, int rate) { + enableStreaming(); + int source = channels[numChannels].sourceId; + int processed = AL10.alGetSourcei(source, AL10.AL_BUFFERS_PROCESSED); + + boolean playing = (AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING); + boolean interupted = !playing && streamQueue > 2; + + IntBuffer buffer = tmp; + if (interupted) { + unqueueStreams(); + buffer.put(0, buffers.get(Sound.MAX_SFX + streamQueue++)); + Com.DPrintf("queue " + (streamQueue - 1) + '\n'); + } else if (processed < 2) { + // check queue overrun + if (streamQueue >= Sound.STREAM_QUEUE) return; + buffer.put(0, buffers.get(Sound.MAX_SFX + streamQueue++)); + Com.DPrintf("queue " + (streamQueue - 1) + '\n'); + } else { + // reuse the buffer + AL10.alSourceUnqueueBuffers(source, buffer); + } + + samples.position(0); + samples.limit(count); + AL10.alBufferData(buffer.get(0), format, samples, rate); + AL10.alSourceQueueBuffers(source, buffer); + + if (streamQueue > 1 && !playing) { + Com.DPrintf("start sound\n"); + AL10.alSourcePlay(source); + } + } + static void addPlaySounds() { while (Channel.assign(PlaySound.nextPlayableSound())); Index: LWJGLSoundImpl.java =================================================================== RCS file: /cvsroot/jake2/jake2/src/jake2/sound/lwjgl/LWJGLSoundImpl.java,v retrieving revision 1.7 retrieving revision 1.7.6.1 diff -C2 -d -r1.7 -r1.7.6.1 *** LWJGLSoundImpl.java 27 Apr 2005 12:21:24 -0000 1.7 --- LWJGLSoundImpl.java 25 Dec 2005 18:16:38 -0000 1.7.6.1 *************** *** 15,21 **** import jake2.util.Vargs; ! import java.nio.ByteBuffer; ! import java.nio.FloatBuffer; ! import java.nio.IntBuffer; import org.lwjgl.LWJGLException; --- 15,20 ---- import jake2.util.Vargs; ! import java.nio.*; ! import java.util.Random; import org.lwjgl.LWJGLException; *************** *** 40,46 **** private cvar_t s_volume; ! private static final int MAX_SFX = Defines.MAX_SOUNDS * 2; ! ! private IntBuffer buffers = Lib.newIntBuffer(MAX_SFX); // singleton --- 39,44 ---- private cvar_t s_volume; ! // the last 4 buffers are used for cinematics streaming ! private IntBuffer buffers = Lib.newIntBuffer(MAX_SFX + STREAM_QUEUE); // singleton *************** *** 508,518 **** } ! /* (non-Javadoc) * @see jake2.sound.Sound#RawSamples(int, int, int, int, byte[]) */ ! public void RawSamples(int samples, int rate, int width, int channels, byte[] data) { ! // TODO implement RawSamples } ! /* =============================================================================== --- 506,543 ---- } ! private ShortBuffer streamBuffer = sfxDataBuffer.slice().order(ByteOrder.BIG_ENDIAN).asShortBuffer(); ! ! /* (non-Javadoc) * @see jake2.sound.Sound#RawSamples(int, int, int, int, byte[]) */ ! public void RawSamples(int samples, int rate, int width, int channels, ByteBuffer data) { ! int format; ! if (channels == 2) { ! format = (width == 2) ? AL10.AL_FORMAT_STEREO16 ! : AL10.AL_FORMAT_STEREO8; ! } else { ! format = (width == 2) ? AL10.AL_FORMAT_MONO16 ! : AL10.AL_FORMAT_MONO8; ! } ! ! // convert to signed 16 bit samples ! if (format == AL10.AL_FORMAT_MONO8) { ! ShortBuffer sampleData = streamBuffer; ! int value; ! for (int i = 0; i < samples; i++) { ! value = (data.get(i) & 0xFF) - 128; ! sampleData.put(i, (short) value); ! } ! format = AL10.AL_FORMAT_MONO16; ! width = 2; ! data = sfxDataBuffer.slice(); ! } ! ! Channel.updateStream(data, samples * channels * width, format, rate); } ! ! public void disableStreaming() { ! Channel.disableStreaming(); ! } /* =============================================================================== |