From: <man...@us...> - 2013-07-30 08:29:42
|
Revision: 2552 http://sourceforge.net/p/modplug/code/2552 Author: manxorist Date: 2013-07-30 08:29:28 +0000 (Tue, 30 Jul 2013) Log Message: ----------- [Ref] Move dithering code to its own file. Modified Paths: -------------- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters trunk/OpenMPT/mptrack/mptrack_08.vcproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters trunk/OpenMPT/soundlib/Fastmix.cpp trunk/OpenMPT/soundlib/Sndfile.h Added Paths: ----------- trunk/OpenMPT/soundlib/Dither.cpp trunk/OpenMPT/soundlib/Dither.h Modified: trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj 2013-07-30 07:58:16 UTC (rev 2551) +++ trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj 2013-07-30 08:29:28 UTC (rev 2552) @@ -423,6 +423,7 @@ <ClInclude Include="..\include\pugixml\src\pugiconfig.hpp" /> <ClInclude Include="..\include\pugixml\src\pugixml.hpp" /> <ClInclude Include="..\soundlib\ChunkReader.h" /> + <ClInclude Include="..\soundlib\Dither.h" /> <ClInclude Include="..\soundlib\Dlsbank.h" /> <ClInclude Include="..\soundlib\Endianness.h" /> <ClInclude Include="..\soundlib\FileReader.h" /> @@ -479,6 +480,7 @@ <ClCompile Include="..\common\typedefs.cpp" /> <ClCompile Include="..\common\version.cpp" /> <ClCompile Include="..\include\pugixml\src\pugixml.cpp" /> + <ClCompile Include="..\soundlib\Dither.cpp" /> <ClCompile Include="..\soundlib\Dlsbank.cpp" /> <ClCompile Include="..\soundlib\Fastmix.cpp" /> <ClCompile Include="..\soundlib\ITCompression.cpp" /> Modified: trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters 2013-07-30 07:58:16 UTC (rev 2551) +++ trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters 2013-07-30 08:29:28 UTC (rev 2552) @@ -233,6 +233,9 @@ <ClInclude Include="..\common\svn_version_subwcrev\svn_version.template.h"> <Filter>Header Files\common</Filter> </ClInclude> + <ClInclude Include="..\soundlib\Dither.h"> + <Filter>Header Files\soundlib</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp"> @@ -487,5 +490,8 @@ <ClCompile Include="libopenmpt_test.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="..\soundlib\Dither.cpp"> + <Filter>Source Files\soundlib</Filter> + </ClCompile> </ItemGroup> </Project> \ No newline at end of file Modified: trunk/OpenMPT/mptrack/mptrack_08.vcproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_08.vcproj 2013-07-30 07:58:16 UTC (rev 2551) +++ trunk/OpenMPT/mptrack/mptrack_08.vcproj 2013-07-30 08:29:28 UTC (rev 2552) @@ -311,6 +311,10 @@ > </File> <File + RelativePath="..\soundlib\Dither.cpp" + > + </File> + <File RelativePath="..\soundlib\dlsbank.cpp" > </File> @@ -877,6 +881,10 @@ > </File> <File + RelativePath="..\soundlib\Dither.h" + > + </File> + <File RelativePath="..\Soundlib\Dlsbank.h" > </File> Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2013-07-30 07:58:16 UTC (rev 2551) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2013-07-30 08:29:28 UTC (rev 2552) @@ -265,6 +265,7 @@ <ClCompile Include="..\sounddsp\DSP.cpp" /> <ClCompile Include="..\sounddsp\EQ.cpp" /> <ClCompile Include="..\sounddsp\Reverb.cpp" /> + <ClCompile Include="..\soundlib\Dither.cpp" /> <ClCompile Include="..\soundlib\Dlsbank.cpp" /> <ClCompile Include="..\soundlib\Fastmix.cpp" /> <ClCompile Include="..\soundlib\ITCompression.cpp" /> @@ -482,6 +483,7 @@ <ClInclude Include="..\sounddsp\EQ.h" /> <ClInclude Include="..\sounddsp\Reverb.h" /> <ClInclude Include="..\soundlib\ChunkReader.h" /> + <ClInclude Include="..\soundlib\Dither.h" /> <ClInclude Include="..\soundlib\Dlsbank.h" /> <ClInclude Include="..\soundlib\Endianness.h" /> <ClInclude Include="..\soundlib\FileReader.h" /> Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2013-07-30 07:58:16 UTC (rev 2551) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2013-07-30 08:29:28 UTC (rev 2552) @@ -478,6 +478,9 @@ <ClCompile Include="..\soundlib\S3MTools.cpp"> <Filter>Source Files\soundlib\Module Loaders</Filter> </ClCompile> + <ClCompile Include="..\soundlib\Dither.cpp"> + <Filter>Source Files\soundlib</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\soundlib\Loaders.h"> @@ -882,6 +885,9 @@ <ClInclude Include="..\common\svn_version_subwcrev\svn_version.template.h"> <Filter>Header Files\common</Filter> </ClInclude> + <ClInclude Include="..\soundlib\Dither.h"> + <Filter>Header Files\soundlib</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <None Include="res\bitmap1.bmp"> Copied: trunk/OpenMPT/soundlib/Dither.cpp (from rev 2551, trunk/OpenMPT/soundlib/Fastmix.cpp) =================================================================== --- trunk/OpenMPT/soundlib/Dither.cpp (rev 0) +++ trunk/OpenMPT/soundlib/Dither.cpp 2013-07-30 08:29:28 UTC (rev 2552) @@ -0,0 +1,157 @@ +/* + * Dither.cpp + * ---------- + * Purpose: Dithering when converting to lower resolution sample formats. + * Notes : (currently none) + * Authors: Olivier Lapicque + * OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + +#include "stdafx.h" + +#include "Dither.h" + +#include "Snd_defs.h" + + + +////////////////////////////////////////////////////////////////////////// +// Noise Shaping (Dithering) + +#if MPT_COMPILER_MSVC +#pragma warning(disable:4731) // ebp modified +#endif + + +#ifdef ENABLE_X86 + +void X86_Dither(int *pBuffer, UINT nSamples, UINT nBits, DitherModPlugState *state) +//--------------------------------------------------------------------------------- +{ + if(nBits + MIXING_ATTENUATION + 1 >= 32) //if(nBits>16) + { + return; + } + + static int gDitherA_global, gDitherB_global; + + int gDitherA = state ? state->rng_a : gDitherA_global; + int gDitherB = state ? state->rng_b : gDitherB_global; + + _asm { + mov esi, pBuffer // esi = pBuffer+i + mov eax, nSamples // ebp = i + mov ecx, nBits // ecx = number of bits of noise + mov edi, gDitherA // Noise generation + mov ebx, gDitherB + add ecx, MIXING_ATTENUATION+1 + push ebp + mov ebp, eax +noiseloop: + rol edi, 1 + mov eax, dword ptr [esi] + xor edi, 0x10204080 + add esi, 4 + lea edi, [ebx*4+edi+0x78649E7D] + mov edx, edi + rol edx, 16 + lea edx, [edx*4+edx] + add ebx, edx + mov edx, ebx + sar edx, cl + add eax, edx + dec ebp + mov dword ptr [esi-4], eax + jnz noiseloop + pop ebp + mov gDitherA, edi + mov gDitherB, ebx + } + + if(state) state->rng_a = gDitherA; else gDitherA_global = gDitherA; + if(state) state->rng_b = gDitherB; else gDitherB_global = gDitherB; + +} + +#endif // ENABLE_X86 + + +static forceinline int32 dither_rand(uint32 &a, uint32 &b) +//-------------------------------------------------------- +{ + a = (a << 1) | (a >> 31); + a ^= 0x10204080u; + a += 0x78649E7Du + (b * 4); + b += ((a << 16 ) | (a >> 16)) * 5; + return (int32)b; +} + +static void C_Dither(int *pBuffer, UINT nSamples, UINT nBits, DitherModPlugState *state) +//-------------------------------------------------------------------------------------- +{ + if(nBits + MIXING_ATTENUATION + 1 >= 32) //if(nBits>16) + { + return; + } + + static uint32 global_a = 0; + static uint32 global_b = 0; + + uint32 a = state ? state->rng_a : global_a; + uint32 b = state ? state->rng_b : global_b; + + while(nSamples--) + { + *pBuffer += dither_rand(a, b) >> (nBits + MIXING_ATTENUATION + 1); + pBuffer++; + } + + if(state) state->rng_a = a; else global_a = a; + if(state) state->rng_b = b; else global_b = b; + +} + +static void Dither_ModPlug(int *pBuffer, UINT nSamples, UINT nChannels, UINT nBits, DitherModPlugState &state) +//------------------------------------------------------------------------------------------------------------ +{ + #ifdef ENABLE_X86 + X86_Dither(pBuffer, nSamples * nChannels, nBits, &state); + #else // !ENABLE_X86 + C_Dither(pBuffer, nSamples * nChannels, nBits, &state); + #endif // ENABLE_X86 +} + + +void Dither::Reset() +{ + state = DitherState(); +} + +Dither::Dither() +{ + mode = DitherModPlug; +} + +void Dither::SetMode(DitherMode &mode_) +{ + mode = mode_; +} + +DitherMode Dither::GetMode() const +{ + return mode; +} + +void Dither::Process(int *mixbuffer, std::size_t count, std::size_t channels, int bits) +{ + switch(mode) + { + case DitherNone: + // nothing + break; + case DitherModPlug: + Dither_ModPlug(mixbuffer, count, channels, bits, state.modplug); + break; + } +} Property changes on: trunk/OpenMPT/soundlib/Dither.cpp ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/x-c++src \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: trunk/OpenMPT/soundlib/Dither.h =================================================================== --- trunk/OpenMPT/soundlib/Dither.h (rev 0) +++ trunk/OpenMPT/soundlib/Dither.h 2013-07-30 08:29:28 UTC (rev 2552) @@ -0,0 +1,48 @@ +/* + * Dither.h + * -------- + * Purpose: Dithering when converting to lower resolution sample formats. + * Notes : (currently none) + * Authors: Olivier Lapicque + * OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + + +#pragma once + + +struct DitherModPlugState +{ + uint32 rng_a; + uint32 rng_b; + DitherModPlugState() + { + rng_a = 0; + rng_b = 0; + } +}; + +struct DitherState +{ + DitherModPlugState modplug; +}; + +enum DitherMode +{ + DitherNone = 0, + DitherModPlug = 1 +}; + +class Dither +{ +private: + DitherState state; + DitherMode mode; +public: + Dither(); + void SetMode(DitherMode &mode); + DitherMode GetMode() const; + void Reset(); + void Process(int *mixbuffer, std::size_t count, std::size_t channels, int bits); +}; Property changes on: trunk/OpenMPT/soundlib/Dither.h ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/x-chdr \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/OpenMPT/soundlib/Fastmix.cpp =================================================================== --- trunk/OpenMPT/soundlib/Fastmix.cpp 2013-07-30 07:58:16 UTC (rev 2551) +++ trunk/OpenMPT/soundlib/Fastmix.cpp 2013-07-30 08:29:28 UTC (rev 2552) @@ -1881,147 +1881,12 @@ ////////////////////////////////////////////////////////////////////////// -// Noise Shaping (Dithering) #if MPT_COMPILER_MSVC #pragma warning(disable:4731) // ebp modified #endif - #ifdef ENABLE_X86 - -void X86_Dither(int *pBuffer, UINT nSamples, UINT nBits, DitherModPlugState *state) -//--------------------------------------------------------------------------------- -{ - if(nBits + MIXING_ATTENUATION + 1 >= 32) //if(nBits>16) - { - return; - } - - static int gDitherA_global, gDitherB_global; - - int gDitherA = state ? state->rng_a : gDitherA_global; - int gDitherB = state ? state->rng_b : gDitherB_global; - - _asm { - mov esi, pBuffer // esi = pBuffer+i - mov eax, nSamples // ebp = i - mov ecx, nBits // ecx = number of bits of noise - mov edi, gDitherA // Noise generation - mov ebx, gDitherB - add ecx, MIXING_ATTENUATION+1 - push ebp - mov ebp, eax -noiseloop: - rol edi, 1 - mov eax, dword ptr [esi] - xor edi, 0x10204080 - add esi, 4 - lea edi, [ebx*4+edi+0x78649E7D] - mov edx, edi - rol edx, 16 - lea edx, [edx*4+edx] - add ebx, edx - mov edx, ebx - sar edx, cl - add eax, edx - dec ebp - mov dword ptr [esi-4], eax - jnz noiseloop - pop ebp - mov gDitherA, edi - mov gDitherB, ebx - } - - if(state) state->rng_a = gDitherA; else gDitherA_global = gDitherA; - if(state) state->rng_b = gDitherB; else gDitherB_global = gDitherB; - -} - -#endif // ENABLE_X86 - - -static forceinline int32 dither_rand(uint32 &a, uint32 &b) -//-------------------------------------------------------- -{ - a = (a << 1) | (a >> 31); - a ^= 0x10204080u; - a += 0x78649E7Du + (b * 4); - b += ((a << 16 ) | (a >> 16)) * 5; - return (int32)b; -} - -static void C_Dither(int *pBuffer, UINT nSamples, UINT nBits, DitherModPlugState *state) -//-------------------------------------------------------------------------------------- -{ - if(nBits + MIXING_ATTENUATION + 1 >= 32) //if(nBits>16) - { - return; - } - - static uint32 global_a = 0; - static uint32 global_b = 0; - - uint32 a = state ? state->rng_a : global_a; - uint32 b = state ? state->rng_b : global_b; - - while(nSamples--) - { - *pBuffer += dither_rand(a, b) >> (nBits + MIXING_ATTENUATION + 1); - pBuffer++; - } - - if(state) state->rng_a = a; else global_a = a; - if(state) state->rng_b = b; else global_b = b; - -} - -static void Dither_ModPlug(int *pBuffer, UINT nSamples, UINT nChannels, UINT nBits, DitherModPlugState &state) -//------------------------------------------------------------------------------------------------------------ -{ - #ifdef ENABLE_X86 - X86_Dither(pBuffer, nSamples * nChannels, nBits, &state); - #else // !ENABLE_X86 - C_Dither(pBuffer, nSamples * nChannels, nBits, &state); - #endif // ENABLE_X86 -} - - -void Dither::Reset() -{ - state = DitherState(); -} - -Dither::Dither() -{ - mode = DitherModPlug; -} - -void Dither::SetMode(DitherMode &mode_) -{ - mode = mode_; -} - -DitherMode Dither::GetMode() const -{ - return mode; -} - -void Dither::Process(int *mixbuffer, std::size_t count, std::size_t channels, int bits) -{ - switch(mode) - { - case DitherNone: - // nothing - break; - case DitherModPlug: - Dither_ModPlug(mixbuffer, count, channels, bits, state.modplug); - break; - } -} - - -#ifdef ENABLE_X86 static void X86_InterleaveFrontRear(int *pFrontBuf, int *pRearBuf, DWORD nFrames) //------------------------------------------------------------------------------- { Modified: trunk/OpenMPT/soundlib/Sndfile.h =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.h 2013-07-30 07:58:16 UTC (rev 2551) +++ trunk/OpenMPT/soundlib/Sndfile.h 2013-07-30 08:29:28 UTC (rev 2552) @@ -39,6 +39,7 @@ #include "../sounddsp/AGC.h" #include "../sounddsp/DSP.h" #include "../sounddsp/EQ.h" +#include "Dither.h" class FileReader; @@ -186,42 +187,7 @@ class CModDoc; #endif // MODPLUG_TRACKER -struct DitherModPlugState -{ - uint32 rng_a; - uint32 rng_b; - DitherModPlugState() - { - rng_a = 0; - rng_b = 0; - } -}; -struct DitherState -{ - DitherModPlugState modplug; -}; - -enum DitherMode -{ - DitherNone = 0, - DitherModPlug = 1 -}; - -class Dither -{ -private: - DitherState state; - DitherMode mode; -public: - Dither(); - void SetMode(DitherMode &mode); - DitherMode GetMode() const; - void Reset(); - void Process(int *mixbuffer, std::size_t count, std::size_t channels, int bits); -}; - - void StereoMixToFloat(const int *pSrc, float *pOut1, float *pOut2, UINT nCount, const float _i2fc); void FloatToStereoMix(const float *pIn1, const float *pIn2, int *pOut, UINT nCount, const float _f2ic); void MonoMixToFloat(const int *pSrc, float *pOut, UINT nCount, const float _i2fc); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |