Commit c8564206 authored by Gargaj's avatar Gargaj
Browse files

change format to float, add amplification field

parent 3bcfbe02
...@@ -38,6 +38,7 @@ Create a `config.json` with e.g. the following contents: (all fields are optiona ...@@ -38,6 +38,7 @@ Create a `config.json` with e.g. the following contents: (all fields are optiona
}, },
"rendering":{ "rendering":{
"fftSmoothFactor": 0.9, // 0.0 means there's no smoothing at all, 1.0 means the FFT is completely smoothed flat "fftSmoothFactor": 0.9, // 0.0 means there's no smoothing at all, 1.0 means the FFT is completely smoothed flat
"fftAmplification": 1.0, // 1.0 means no change, larger values will result in brighter/stronger bands, smaller values in darker/weaker ones
}, },
"textures":{ // the keys below will become the shader variable names "textures":{ // the keys below will become the shader variable names
"texChecker":"textures/checker.png", "texChecker":"textures/checker.png",
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
namespace FFT namespace FFT
{ {
extern float fAmplification;
bool Open(); bool Open();
bool GetFFT( float * samples ); bool GetFFT( float * _samples );
void Close(); void Close();
} }
\ No newline at end of file
...@@ -124,6 +124,8 @@ int main(int argc, const char *argv[]) ...@@ -124,6 +124,8 @@ int main(int argc, const char *argv[])
{ {
if (options.get<jsonxx::Object>("rendering").has<jsonxx::Number>("fftSmoothFactor")) if (options.get<jsonxx::Object>("rendering").has<jsonxx::Number>("fftSmoothFactor"))
fFFTSmoothingFactor = options.get<jsonxx::Object>("rendering").get<jsonxx::Number>("fftSmoothFactor"); fFFTSmoothingFactor = options.get<jsonxx::Object>("rendering").get<jsonxx::Number>("fftSmoothFactor");
if (options.get<jsonxx::Object>("rendering").has<jsonxx::Number>("fftAmplification"))
FFT::fAmplification = options.get<jsonxx::Object>("rendering").get<jsonxx::Number>("fftAmplification");
} }
if (options.has<jsonxx::Object>("textures")) if (options.has<jsonxx::Object>("textures"))
......
...@@ -13,6 +13,7 @@ namespace FFT ...@@ -13,6 +13,7 @@ namespace FFT
mal_context context; mal_context context;
mal_device captureDevice; mal_device captureDevice;
float sampleBuf[ FFT_SIZE * 2 ]; float sampleBuf[ FFT_SIZE * 2 ];
float fAmplification = 1.0f;
void OnLog( mal_context* pContext, mal_device* pDevice, const char* message ) void OnLog( mal_context* pContext, mal_device* pDevice, const char* message )
{ {
...@@ -24,7 +25,7 @@ namespace FFT ...@@ -24,7 +25,7 @@ namespace FFT
frameCount = frameCount < FFT_SIZE * 2 ? frameCount : FFT_SIZE * 2; frameCount = frameCount < FFT_SIZE * 2 ? frameCount : FFT_SIZE * 2;
// Just rotate the buffer; copy existing, append new // Just rotate the buffer; copy existing, append new
const mal_int16 * samples = (const mal_int16 *)pSamples; const float * samples = (const float *)pSamples;
float * p = sampleBuf; float * p = sampleBuf;
for ( int i = 0; i < FFT_SIZE * 2 - frameCount; i++ ) for ( int i = 0; i < FFT_SIZE * 2 - frameCount; i++ )
{ {
...@@ -32,7 +33,7 @@ namespace FFT ...@@ -32,7 +33,7 @@ namespace FFT
} }
for ( int i = 0; i < frameCount; i++ ) for ( int i = 0; i < frameCount; i++ )
{ {
*( p++ ) = ( samples[ i * 2 ] + samples[ i * 2 + 1 ] ) / 65535.0f; // int16 max * 2 -> -1..1 *( p++ ) = ( samples[ i * 2 ] + samples[ i * 2 + 1 ] ) / 2.0f * fAmplification;
} }
} }
...@@ -52,7 +53,7 @@ namespace FFT ...@@ -52,7 +53,7 @@ namespace FFT
printf( "[FFT] MAL context initialized, backend is '%s'\n", mal_get_backend_name( context.backend ) ); printf( "[FFT] MAL context initialized, backend is '%s'\n", mal_get_backend_name( context.backend ) );
const mal_device_config config = mal_device_config_init( mal_format_s16, 2, 44100, OnReceiveFrames, NULL ); const mal_device_config config = mal_device_config_init( mal_format_f32, 2, 44100, OnReceiveFrames, NULL );
result = mal_device_init( &context, mal_device_type_capture, NULL, &config, NULL, &captureDevice ); result = mal_device_init( &context, mal_device_type_capture, NULL, &config, NULL, &captureDevice );
if ( result != MAL_SUCCESS ) if ( result != MAL_SUCCESS )
...@@ -73,7 +74,7 @@ namespace FFT ...@@ -73,7 +74,7 @@ namespace FFT
return true; return true;
} }
bool GetFFT( float * samples ) bool GetFFT( float * _samples )
{ {
kiss_fft_cpx out[ FFT_SIZE + 1 ]; kiss_fft_cpx out[ FFT_SIZE + 1 ];
kiss_fftr( fftcfg, sampleBuf, out ); kiss_fftr( fftcfg, sampleBuf, out );
...@@ -81,7 +82,7 @@ namespace FFT ...@@ -81,7 +82,7 @@ namespace FFT
for ( int i = 0; i < FFT_SIZE; i++ ) for ( int i = 0; i < FFT_SIZE; i++ )
{ {
static const float scaling = 1.0f / (float)FFT_SIZE; static const float scaling = 1.0f / (float)FFT_SIZE;
samples[ i ] = 2.0 * sqrtf( out[ i ].r * out[ i ].r + out[ i ].i * out[ i ].i ) * scaling; _samples[ i ] = 2.0 * sqrtf( out[ i ].r * out[ i ].r + out[ i ].i * out[ i ].i ) * scaling;
} }
return true; return true;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment