VulcanoLE/src/VulcanoLE/Audio/FFT.cpp

56 lines
1.9 KiB
C++

#include <VulcanoLE/Audio/FFT.h>
#include <VUtils/Logging.h>
void FFT::process(stereoSample *frame) {
std::unique_lock<std::mutex> lck(m_mtx);
prepareInput(frame, FFT_SIZE);
m_fftwPlanLeft = fftw_plan_dft_r2c_1d(static_cast<int>(BUFFER_SIZE), m_fftwInputLeft,
m_fftwOutputLeft, FFTW_ESTIMATE);
m_fftwPlanRight = fftw_plan_dft_r2c_1d(static_cast<int>(BUFFER_SIZE), m_fftwInputRight,
m_fftwOutputRight, FFTW_ESTIMATE);
fftw_execute(m_fftwPlanLeft);
fftw_execute(m_fftwPlanRight);
fftw_destroy_plan(m_fftwPlanLeft);
fftw_destroy_plan(m_fftwPlanRight);
for (int i = 0; i < FFT_SIZE; ++i) {
double left = (double(*m_fftwOutputLeft[i]));
double right = (double(*m_fftwOutputRight[i]));
m_sample->leftChannel[i] = left;
m_sample->rightChannel[i] = right;
}
}
// return vector of floats!
outputSample *FFT::getData() {
std::unique_lock<std::mutex> lck(m_mtx);
return m_sample;
}
bool FFT::prepareInput(stereoSample *buffer, uint32_t sampleSize) {
bool is_silent = true;
for (auto i = 0u; i < sampleSize; ++i) {
m_fftwInputLeft[i] = buffer[i].l;
m_fftwInputRight[i] = buffer[i].r;
if (is_silent && (m_fftwInputLeft[i] > 0 || m_fftwInputRight[i] > 0)) is_silent = false;
}
return is_silent;
}
FFT::FFT() {
m_fftwResults = (static_cast<size_t>(BUFFER_SIZE) / 2) + 1;
m_fftwInputLeft = static_cast<double *>(
fftw_malloc(sizeof(double) * BUFFER_SIZE));
m_fftwInputRight = static_cast<double *>(
fftw_malloc(sizeof(double) * BUFFER_SIZE));
m_fftwOutputLeft = static_cast<fftw_complex *>(
fftw_malloc(sizeof(fftw_complex) * m_fftwResults));
m_fftwOutputRight = static_cast<fftw_complex *>(
fftw_malloc(sizeof(fftw_complex) * m_fftwResults));
}
FFT::~FFT() {
delete m_sample;
}