Browse Source

some test code

master
Weird Constructor 4 years ago
parent
commit
41bcb0aa41
  1. 3
      WaveSabreCore/CMakeLists.txt
  2. 8
      WaveSabreCore/_BUILD.bat
  3. 2
      WaveSabreCore/_TEST.bat
  4. 18
      WaveSabreCore/main.cpp
  5. 34
      WaveSabreCore/src/Helpers.cpp
  6. 3
      WaveSabreCore/src/StateVariableFilter.cpp

3
WaveSabreCore/CMakeLists.txt

@ -57,6 +57,9 @@ add_library(WaveSabreCore
target_link_libraries(WaveSabreCore Msacm32.lib)
target_include_directories(WaveSabreCore PUBLIC include)
add_executable(xxx main.cpp)
target_link_libraries(xxx WaveSabreCore)
if(MSVC)
target_compile_options(WaveSabreCore
PUBLIC $<$<CONFIG:MinSizeRel>:/GS- /Gy /Zi /GL /GR->

8
WaveSabreCore/_BUILD.bat

@ -0,0 +1,8 @@
@mkdir build > nul 2>&1
cd build
IF NOT DEFINED VCINSTALLDIR call "%VS140COMNTOOLS%\..\..\VC\vcvarsall.bat" amd64_x86
D:\Checkouts\BzVCBase\tools\cmake\bin\cmake.exe -T v140_xp -DCMAKE_GENERATOR_TOOLSET=v140_xp ..
D:\Checkouts\BzVCBase\tools\cmake\bin\cmake.exe --build . --config Release
set LOCAL_ERR=%ERRORLEVEL%
cd ..
cmd /c exit %LOCAL_ERR%

2
WaveSabreCore/_TEST.bat

@ -0,0 +1,2 @@
call _BUILD.bat
build\Release\xxx.exe

18
WaveSabreCore/main.cpp

@ -0,0 +1,18 @@
#include <iostream>
#include "WaveSabreCore/Helpers.h"
int main() {
WaveSabreCore::Helpers::Init();
std::cout << "XO" << std::endl;
std::cout << "cos" << WaveSabreCore::Helpers::FastCos(3.14159) << std::endl;
std::cout << "cos" << WaveSabreCore::Helpers::FastCos(3.14159 * 2) << std::endl;
std::cout << "sin" << WaveSabreCore::Helpers::FastSin(3.14159) << std::endl;
std::cout << "sin" << WaveSabreCore::Helpers::FastSin(3.14159 * 2) << std::endl;
WaveSabreCore::StateVariableFilter filter;
filter.SetFreq(1000.0);
filter.SetQ(1.0);
printf("%f\n", filter.Next(0.0069039087));
}

34
WaveSabreCore/src/Helpers.cpp

@ -4,6 +4,27 @@
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
#include <bitset>
#include <climits>
template<typename T>
void show_binrep(const T& a)
{
const char* beg = reinterpret_cast<const char*>(&a);
const char* end = beg + sizeof(a);
while(beg != end)
std::cout << std::bitset<CHAR_BIT>(*beg++) << ' ';
std::cout << '\n';
}
void print_bits(unsigned long long n) {
std::string s = "";
while (n) {
if (n & 1) s = "1" + s; else s = "0" + s; n >>= 1; }
std::cout << s << std::endl;
}
static __declspec(naked) double __vectorcall fpuPow(double x, double y)
{
__asm
@ -131,6 +152,7 @@ namespace WaveSabreCore
void Helpers::Init()
{
RandomSeed = 1;
std::cout << "TABSIZELOG2=" << fastCosTabLog2Size << std::endl;
for (int i = 0; i < fastCosTabSize + 1; i++)
{
@ -162,20 +184,32 @@ namespace WaveSabreCore
const auto phaseScale = 1.0 / (M_PI * 2);
auto phase = 1.0 + x * phaseScale;
// std::cout << "phase = " << phase << std::endl;
auto phaseAsInt = *reinterpret_cast<unsigned long long *>(&phase);
int exponent = (phaseAsInt >> 52) - 1023;
// std::cout << "pasi : "; print_bits(phaseAsInt);
const auto fractBits = 32 - fastCosTabLog2Size;
// std::cout << "bits : "; print_bits(fractBits);
// std::cout << "bits=" << fractBits << std::endl;
// std::cout << "bits=" << fastCosTabLog2Size << std::endl;
const auto fractScale = 1 << fractBits;
// std::cout << "scale: "; print_bits(fractScale);
const auto fractMask = fractScale - 1;
// std::cout << "mask : "; print_bits(fractMask);
auto significand = (unsigned int)((phaseAsInt << exponent) >> (52 - 32));
// std::cout << "signi: "; print_bits(significand);
auto index = significand >> fractBits;
// std::cout << "index: "; print_bits(index);
int fract = significand & fractMask;
// std::cout << "fract: "; print_bits(fract);
auto left = fastCosTab[index];
auto right = fastCosTab[index + 1];
// std::cout << "index=" << index << ", frfct=" << fract << std::endl;
auto fractMix = fract * (1.0 / fractScale);
return left + (right - left) * fractMix;
}

3
WaveSabreCore/src/StateVariableFilter.cpp

@ -1,3 +1,4 @@
#include <cstdio>
#include <WaveSabreCore/StateVariableFilter.h>
#include <WaveSabreCore/Helpers.h>
@ -61,9 +62,11 @@ namespace WaveSabreCore
float StateVariableFilter::run(float input)
{
printf("f=%f, low=%f, band=%f, in=%f\n", f, low, band, input);
low = low + f * band;
float high = q * (input - band) - low;
band = band + f * high;
printf("f=%f, low=%f, band=%f, in=%f, high=%f\n", f, low, band, input, high);
switch (type)
{
case StateVariableFilterType::Lowpass:

Loading…
Cancel
Save