From: Carsten W. <ca...@us...> - 2005-12-25 18:11:59
|
Update of /cvsroot/jake2/jake2/src/jake2/sound In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27365/src/jake2/sound Modified Files: Tag: render-refactoring WaveLoader.java Sound.java S.java DummyDriver.java Log Message: merge with current HEAD Index: S.java =================================================================== RCS file: /cvsroot/jake2/jake2/src/jake2/sound/S.java,v retrieving revision 1.11 retrieving revision 1.11.4.1 diff -C2 -d -r1.11 -r1.11.4.1 *** S.java 26 May 2005 16:56:33 -0000 1.11 --- S.java 25 Dec 2005 18:11:21 -0000 1.11.4.1 *************** *** 31,34 **** --- 31,35 ---- import jake2.qcommon.Cvar; + import java.nio.ByteBuffer; import java.util.Vector; *************** *** 42,46 **** static Vector drivers = new Vector(3); ! static { // dummy driver (no sound) --- 43,50 ---- static Vector drivers = new Vector(3); ! ! /** ! * Searches for and initializes all known sound drivers. ! */ static { // dummy driver (no sound) *************** *** 73,76 **** --- 77,83 ---- }; + /** + * Registers a new Sound Implementor. + */ public static void register(Sound driver) { if (driver == null) { *************** *** 82,85 **** --- 89,95 ---- } + /** + * Switches to the specific sound driver. + */ public static void useDriver(String driverName) { Sound driver = null; *************** *** 96,99 **** --- 106,112 ---- } + /** + * Initializes the sound module. + */ public static void Init() { *************** *** 132,211 **** } ! /* ! ===================== ! S_BeginRegistration ! ===================== ! */ public static void BeginRegistration() { impl.BeginRegistration(); } ! /* ! ===================== ! S_RegisterSound ! ===================== ! */ public static sfx_t RegisterSound(String sample) { return impl.RegisterSound(sample); } ! /* ! ===================== ! S_EndRegistration ! ===================== ! */ public static void EndRegistration() { impl.EndRegistration(); } ! /* ! ================== ! S_StartLocalSound ! ================== ! */ public static void StartLocalSound(String sound) { impl.StartLocalSound(sound); } ! /* ! ==================== ! S_StartSound ! ! Validates the parms and ques the sound up ! if pos is NULL, the sound will be dynamically sourced from the entity ! Entchannel 0 will never override a playing sound ! ==================== ! */ public static void StartSound(float[] origin, int entnum, int entchannel, sfx_t sfx, float fvol, float attenuation, float timeofs) { impl.StartSound(origin, entnum, entchannel, sfx, fvol, attenuation, timeofs); } ! /* ! ============ ! S_Update ! ! Called once each time through the main loop ! ============ ! */ public static void Update(float[] origin, float[] forward, float[] right, float[] up) { impl.Update(origin, forward, right, up); } ! /* ! ============ ! S_RawSamples ! ! Cinematic streaming and voice over network ! ============ ! */ ! public static void RawSamples(int samples, int rate, int width, int channels, byte[] data) { impl.RawSamples(samples, rate, width, channels, data); } ! /* ! ================== ! S_StopAllSounds ! ================== ! */ public static void StopAllSounds() { impl.StopAllSounds(); --- 145,210 ---- } ! /** ! * Called before the sounds are to be loaded and registered. ! */ public static void BeginRegistration() { impl.BeginRegistration(); } ! /** ! * Registers and loads a sound. ! */ public static sfx_t RegisterSound(String sample) { return impl.RegisterSound(sample); } ! /** ! * Called after all sounds are registered and loaded. ! */ public static void EndRegistration() { impl.EndRegistration(); } ! /** ! * Starts a local sound. ! */ public static void StartLocalSound(String sound) { impl.StartLocalSound(sound); } ! /** ! * StartSound - Validates the parms and ques the sound up ! * if pos is NULL, the sound will be dynamically sourced from the entity ! * Entchannel 0 will never override a playing sound ! */ public static void StartSound(float[] origin, int entnum, int entchannel, sfx_t sfx, float fvol, float attenuation, float timeofs) { impl.StartSound(origin, entnum, entchannel, sfx, fvol, attenuation, timeofs); } ! /** ! * Updates the sound renderer according to the changes in the environment, ! * called once each time through the main loop. ! */ public static void Update(float[] origin, float[] forward, float[] right, float[] up) { impl.Update(origin, forward, right, up); } ! /** ! * Cinematic streaming and voice over network. ! */ ! public static void RawSamples(int samples, int rate, int width, int channels, ByteBuffer data) { impl.RawSamples(samples, rate, width, channels, data); } + + /** + * Switches off the sound streaming. + */ + public static void disableStreaming() { + impl.disableStreaming(); + } ! /** ! * Stops all sounds. ! */ public static void StopAllSounds() { impl.StopAllSounds(); *************** *** 216,219 **** --- 215,221 ---- } + /** + * Returns a string array containing all sound driver names. + */ public static String[] getDriverNames() { String[] names = new String[drivers.size()]; *************** *** 223,225 **** --- 225,237 ---- return names; } + + /** + * This is used, when resampling to this default sampling rate is activated + * in the wavloader. It is placed here that sound implementors can override + * this one day. + */ + public static int getDefaultSampleRate() + { + return 44100; + } } \ No newline at end of file Index: Sound.java =================================================================== RCS file: /cvsroot/jake2/jake2/src/jake2/sound/Sound.java,v retrieving revision 1.1 retrieving revision 1.1.16.1 diff -C2 -d -r1.1 -r1.1.16.1 *** Sound.java 8 Jul 2004 20:56:49 -0000 1.1 --- Sound.java 25 Dec 2005 18:11:21 -0000 1.1.16.1 *************** *** 27,30 **** --- 27,34 ---- package jake2.sound; + import jake2.Defines; + + import java.nio.ByteBuffer; + /** * Sound *************** *** 33,37 **** */ public interface Sound { ! String getName(); --- 37,43 ---- */ public interface Sound { ! ! static final int MAX_SFX = Defines.MAX_SOUNDS * 2; ! static final int STREAM_QUEUE = 8; String getName(); *************** *** 94,99 **** ============ */ ! void RawSamples(int samples, int rate, int width, int channels, byte[] data); /* ================== --- 100,106 ---- ============ */ ! void RawSamples(int samples, int rate, int width, int channels, ByteBuffer data); + void disableStreaming(); /* ================== Index: DummyDriver.java =================================================================== RCS file: /cvsroot/jake2/jake2/src/jake2/sound/DummyDriver.java,v retrieving revision 1.1 retrieving revision 1.1.16.1 diff -C2 -d -r1.1 -r1.1.16.1 *** DummyDriver.java 8 Jul 2004 20:56:49 -0000 1.1 --- DummyDriver.java 25 Dec 2005 18:11:21 -0000 1.1.16.1 *************** *** 27,30 **** --- 27,32 ---- package jake2.sound; + import java.nio.ByteBuffer; + /** * DummyDriver *************** *** 94,101 **** * @see jake2.sound.Sound#RawSamples(int, int, int, int, byte[]) */ ! public void RawSamples(int samples, int rate, int width, int channels, byte[] data) { } ! /* (non-Javadoc) * @see jake2.sound.Sound#StopAllSounds() */ --- 96,106 ---- * @see jake2.sound.Sound#RawSamples(int, int, int, int, byte[]) */ ! public void RawSamples(int samples, int rate, int width, int channels, ByteBuffer data) { } ! public void disableStreaming() { ! } ! ! /* (non-Javadoc) * @see jake2.sound.Sound#StopAllSounds() */ Index: WaveLoader.java =================================================================== RCS file: /cvsroot/jake2/jake2/src/jake2/sound/WaveLoader.java,v retrieving revision 1.5 retrieving revision 1.5.2.1 diff -C2 -d -r1.5 -r1.5.2.1 *** WaveLoader.java 27 Jun 2005 08:46:15 -0000 1.5 --- WaveLoader.java 25 Dec 2005 18:11:21 -0000 1.5.2.1 *************** *** 41,59 **** public class WaveLoader { ! private static AudioFormat sampleFormat; ! static { ! if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) { ! sampleFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 22050, 16, 1, 2, 22050, false); ! } else { ! sampleFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 22050, 16, 1, 2, 22050, true); ! } ! ! } ! ! /* ! ============== ! S_LoadSound ! ============== ! */ public static sfxcache_t LoadSound(sfx_t s) { if (s.name.charAt(0) == '*') --- 41,60 ---- public class WaveLoader { ! /** ! * The ResampleSfx can squeeze and stretch samples to a default sample rate. ! * Since Joal and lwjgl sound drivers support this, we don't need it and the samples ! * can keep their original sample rate. Use this switch for reactivating resampling. ! */ ! private static boolean DONT_DO_A_RESAMPLING_FOR_JOAL_AND_LWJGL = true; ! ! /** ! * This is the maximum sample length in bytes which has to be replaced by ! * a configurable variable. ! */ ! private static int maxsamplebytes = 2048 * 1024; ! ! /** ! * Loads a sound from a wav file. ! */ public static sfxcache_t LoadSound(sfx_t s) { if (s.name.charAt(0) == '*') *************** *** 84,115 **** return null; } int size = data.length; wavinfo_t info = GetWavinfo(s.name, data, size); ! ! AudioInputStream in = null; ! AudioInputStream out = null; ! try { ! in = AudioSystem.getAudioInputStream(new ByteArrayInputStream(data)); ! if (in.getFormat().getSampleSizeInBits() == 8) { ! in = convertTo16bit(in); ! } ! out = AudioSystem.getAudioInputStream(sampleFormat, in); ! int l = (int)out.getFrameLength(); ! sc = s.cache = new sfxcache_t(l*2); ! sc.length = l; ! int c = out.read(sc.data, 0, l * 2); ! out.close(); ! in.close(); ! } catch (Exception e) { ! Com.Printf("Couldn't load " + namebuffer + "\n"); return null; } ! sc.loopstart = info.loopstart * ((int)sampleFormat.getSampleRate() / info.rate); ! sc.speed = (int)sampleFormat.getSampleRate(); ! sc.width = sampleFormat.getSampleSizeInBits() / 8; ! sc.stereo = 0; data = null; --- 85,124 ---- return null; } + int size = data.length; wavinfo_t info = GetWavinfo(s.name, data, size); ! ! if (info.channels != 1) ! { ! Com.Printf(s.name + " is a stereo sample - ignoring\n"); return null; } + + float stepscale; + if (DONT_DO_A_RESAMPLING_FOR_JOAL_AND_LWJGL) + stepscale = 1; + else + stepscale = (float)info.rate / S.getDefaultSampleRate(); ! int len = (int) (info.samples / stepscale); ! len = len * info.width * info.channels; ! ! // TODO: handle max sample bytes with a cvar ! if (len >= maxsamplebytes) ! { ! Com.Printf(s.name + " is too long: " + len + " bytes?! ignoring.\n"); ! return null; ! } + sc = s.cache = new sfxcache_t(len); + + sc.length = info.samples; + sc.loopstart = info.loopstart; + sc.speed = info.rate; + sc.width = info.width; + sc.stereo = info.channels; + + ResampleSfx(s, sc.speed, sc.width, data, info.dataofs); data = null; *************** *** 117,142 **** } - static AudioInputStream convertTo16bit(AudioInputStream in) throws IOException { - AudioFormat format = in.getFormat(); - int length = (int)in.getFrameLength(); - byte[] samples = new byte[2*length]; - - for (int i = 0; i < length; i++) { - in.read(samples, 2*i+1, 1); - samples[2*i+1] -= 128; - } - in.close(); ! AudioFormat newformat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), 16, format.getChannels(), 2, format.getFrameRate(), false); ! return new AudioInputStream(new ByteArrayInputStream(samples), newformat, length); ! } ! ! /* ! =============================================================================== ! WAV loading - =============================================================================== - */ static byte[] data_b; --- 126,196 ---- } ! /** ! * Converts sample data with respect to the endianess and adjusts ! * the sample rate of a loaded sample, see flag DONT_DO_A_RESAMPLING_FOR_JOAL_AND_LWJGL. ! */ ! public static void ResampleSfx (sfx_t sfx, int inrate, int inwidth, byte data[], int offset) ! { ! int outcount; ! int srcsample; ! int i; ! int sample, samplefrac, fracstep; ! sfxcache_t sc; ! ! sc = sfx.cache; ! ! if (sc == null) ! return; ! // again calculate the stretching factor. ! // this is usually 0.5, 1, or 2 ! ! float stepscale; ! if (DONT_DO_A_RESAMPLING_FOR_JOAL_AND_LWJGL) ! stepscale = 1; ! else ! stepscale = (float)inrate / S.getDefaultSampleRate(); ! outcount = (int) (sc.length/stepscale); ! sc.length = outcount; ! ! if (sc.loopstart != -1) ! sc.loopstart = (int) (sc.loopstart / stepscale); ! ! // if resampled, sample has now the default sample rate ! if (DONT_DO_A_RESAMPLING_FOR_JOAL_AND_LWJGL == false) ! sc.speed = S.getDefaultSampleRate(); ! ! sc.width = inwidth; ! sc.stereo = 0; ! samplefrac = 0; ! fracstep = (int) (stepscale * 256); ! ! for (i=0 ; i<outcount ; i++) ! { ! srcsample = samplefrac >> 8; ! samplefrac += fracstep; ! ! if (inwidth == 2) ! { ! sample = (int) ((data[offset + srcsample * 2] & 0xff) + (data[offset + srcsample * 2 + 1] << 8)); ! } ! else ! { ! sample = ((data[offset + srcsample] &0xff) - 128) << 8; ! } ! ! if (sc.width == 2) ! { ! sc.data[i*2] = (byte) (sample & 0xff); ! sc.data[i*2+1] = (byte) ((sample>>>8) & 0xff); ! } ! else ! { ! sc.data[i] = (byte) (sample >> 8); ! } ! } ! } static byte[] data_b; |