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
},
"rendering":{
"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
"texChecker":"textures/checker.png",
......
......@@ -2,7 +2,9 @@
namespace FFT
{
extern float fAmplification;
bool Open();
bool GetFFT( float * samples );
bool GetFFT( float * _samples );
void Close();
}
\ No newline at end of file
......@@ -124,6 +124,8 @@ int main(int argc, const char *argv[])
{
if (options.get<jsonxx::Object>("rendering").has<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"))
......
......@@ -13,6 +13,7 @@ namespace FFT
mal_context context;
mal_device captureDevice;
float sampleBuf[ FFT_SIZE * 2 ];
float fAmplification = 1.0f;
void OnLog( mal_context* pContext, mal_device* pDevice, const char* message )
{
......@@ -24,7 +25,7 @@ namespace FFT
frameCount = frameCount < FFT_SIZE * 2 ? frameCount : FFT_SIZE * 2;
// 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;
for ( int i = 0; i < FFT_SIZE * 2 - frameCount; i++ )
{
......@@ -32,7 +33,7 @@ namespace FFT
}
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
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 );
if ( result != MAL_SUCCESS )
......@@ -73,7 +74,7 @@ namespace FFT
return true;
}
bool GetFFT( float * samples )
bool GetFFT( float * _samples )
{
kiss_fft_cpx out[ FFT_SIZE + 1 ];
kiss_fftr( fftcfg, sampleBuf, out );
......@@ -81,7 +82,7 @@ namespace FFT
for ( int i = 0; i < FFT_SIZE; i++ )
{
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;
......
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