From 1de6f8e46090a52fe8ca14d93207d6e0a4ca840f Mon Sep 17 00:00:00 2001 From: zeromus Date: Sun, 14 Sep 2008 06:34:08 +0000 Subject: [PATCH] add in some SPU interpolation code, conditionally compiled. enabled it in windows so we can see what it sounds like. --- desmume/ChangeLog | 1 + desmume/src/SPU.cpp | 47 +++++++++++++++++++++++-- desmume/src/SPU.h | 2 +- desmume/src/windows/DeSmuME_2005.vcproj | 6 ++-- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/desmume/ChangeLog b/desmume/ChangeLog index 02a4b104a..543f24972 100644 --- a/desmume/ChangeLog +++ b/desmume/ChangeLog @@ -13,6 +13,7 @@ - Convert to c++! - Added gfx3d module which emulates the whole GE as part of the core emu. Moved the windows/cocoa OGLRender to the emu core and replace ogl_collector. Now every platform shares the same 3d code. [zeromus] + - Add in some crude interpolation in the SPU (conditionally compiled) so that I can bear to listen to it. [zeromus] Mac OS X port: - Fixed: Filenames and paths with unicode characters now work. [Jeff] - Fixed: Load state from file button works again. [Jeff] diff --git a/desmume/src/SPU.cpp b/desmume/src/SPU.cpp index 7b7dc0f1a..a3690e6f8 100644 --- a/desmume/src/SPU.cpp +++ b/desmume/src/SPU.cpp @@ -221,6 +221,7 @@ void SPU_KeyOn(int channel) chan->buf8 = (s8*)&MMU.MMU_MEM[1][(chan->addr>>20)&0xFF][(chan->addr & MMU.MMU_MASK[1][(chan->addr >> 20) & 0xFF])]; chan->pcm16b = (s16)((chan->buf8[1] << 8) | chan->buf8[0]); + chan->pcm16b_last = 0; chan->index = chan->buf8[2] & 0x7F; chan->lastsampcnt = 7; chan->sampcnt = 8; @@ -694,18 +695,46 @@ void SPU_WriteLong(u32 addr, u32 val) T1WriteLong(MMU.ARM7_REG, addr, val); } +static s32 Interpolate(s32 a, s32 b, double ratio) +{ + ratio = ratio - (int)ratio; + return (1-ratio)*a + ratio*b; +} + ////////////////////////////////////////////////////////////////////////////// static INLINE void Fetch8BitData(channel_struct *chan, s32 *data) { +#ifdef SPU_INTERPOLATE + int loc = (int)chan->sampcnt; + s32 a = (s32)chan->buf8[loc] << 8; + if(loclength-1) { + double ratio = chan->sampcnt-loc; + s32 b = (s32)chan->buf8[loc+1] << 8; + a = (1-ratio)*a + ratio*b; + } + *data = a; +#else *data = (s32)chan->buf8[(int)chan->sampcnt] << 8; +#endif } ////////////////////////////////////////////////////////////////////////////// static INLINE void Fetch16BitData(channel_struct *chan, s32 *data) { - *data = (s32)chan->buf16[(int)chan->sampcnt]; +#ifdef SPU_INTERPOLATE + int loc = (int)chan->sampcnt; + s32 a = (s32)chan->buf16[loc]; + if(loclength-1) { + double ratio = chan->sampcnt-loc; + s32 b = (s32)chan->buf16[loc+1]; + a = (1-ratio)*a + ratio*b; + } + *data = a; +#else + *data = (s32)chan->buf16[(int)chan->sampcnt]; +#endif } ////////////////////////////////////////////////////////////////////////////// @@ -731,7 +760,11 @@ static INLINE void FetchADPCMData(channel_struct *chan, s32 *data) if (chan->lastsampcnt == (int)chan->sampcnt) { // No sense decoding, just return the last sample - *data = (s32)chan->pcm16b; +#ifdef SPU_INTERPOLATE + *data = Interpolate((s32)chan->pcm16b_last,(s32)chan->pcm16b,chan->sampcnt); +#else + *data = (s32)chan->pcm16b; +#endif return; } @@ -746,12 +779,20 @@ static INLINE void FetchADPCMData(channel_struct *chan, s32 *data) if (data4bit & 0x8) diff = -diff; +#ifdef SPU_INTERPOLATE + chan->pcm16b_last = chan->pcm16b; +#endif chan->pcm16b = (s16)MinMax(chan->pcm16b+diff, -0x8000, 0x7FFF); chan->index = MinMax(chan->index+indextbl[data4bit & 0x7], 0, 88); } chan->lastsampcnt = chan->sampcnt; - *data = (s32)chan->pcm16b; + +#ifdef SPU_INTERPOLATE + *data = Interpolate((s32)chan->pcm16b_last,(s32)chan->pcm16b,chan->sampcnt); +#else + *data = (s32)chan->pcm16b; +#endif } ////////////////////////////////////////////////////////////////////////////// diff --git a/desmume/src/SPU.h b/desmume/src/SPU.h index eff5845f5..0cb6da6e4 100644 --- a/desmume/src/SPU.h +++ b/desmume/src/SPU.h @@ -62,7 +62,7 @@ typedef struct double sampinc; // ADPCM specific int lastsampcnt; - s16 pcm16b; + s16 pcm16b, pcm16b_last; int index; } channel_struct; diff --git a/desmume/src/windows/DeSmuME_2005.vcproj b/desmume/src/windows/DeSmuME_2005.vcproj index f7cd5aecb..d8966511a 100644 --- a/desmume/src/windows/DeSmuME_2005.vcproj +++ b/desmume/src/windows/DeSmuME_2005.vcproj @@ -54,7 +54,7 @@ EnableFiberSafeOptimizations="false" WholeProgramOptimization="false" AdditionalIncludeDirectories="..;.\zlib123;.\zziplib" - PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;VERSION=\"0.8.0b2 DEBUG\";WIN32;BETA_VERSION" + PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;VERSION=\"0.8.0b2 DEBUG\";WIN32;BETA_VERSION;SPU_INTERPOLATE" ExceptionHandling="0" BufferSecurityCheck="false" EnableEnhancedInstructionSet="0" @@ -140,7 +140,7 @@ EnableFiberSafeOptimizations="true" WholeProgramOptimization="true" AdditionalIncludeDirectories="..;.\zlib123;.\zziplib" - PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;VERSION=\"0.8.0b2\";WIN32;HAVE_LIBZ;HAVE_LIBZZIP;BETA_VERSION;GDB_STUB" + PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;VERSION=\"0.8.0b2\";WIN32;HAVE_LIBZ;HAVE_LIBZZIP;BETA_VERSION;GDB_STUB;SPU_INTERPOLATE" StringPooling="true" ExceptionHandling="0" BufferSecurityCheck="false" @@ -228,7 +228,7 @@ EnableFiberSafeOptimizations="true" WholeProgramOptimization="true" AdditionalIncludeDirectories="..;.\zlib123;.\zziplib" - PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;VERSION=\"0.8.0b2 SSE2\";WIN32;HAVE_LIBZ;HAVE_LIBZZIP;SSE2;BETA_VERSION;GDB_STUB" + PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;VERSION=\"0.8.0b2 SSE2\";WIN32;HAVE_LIBZ;HAVE_LIBZZIP;SSE2;BETA_VERSION;GDB_STUB;SPU_INTERPOLATE" StringPooling="true" ExceptionHandling="0" BufferSecurityCheck="false"