56 lines
2.0 KiB
C++
56 lines
2.0 KiB
C++
#include <VulcanoLE/Audio/FFT.h>
|
|
#include <VUtils/Logging.h>
|
|
|
|
void FFT::process(pcm_stereo_sample *pFrame) {
|
|
std::unique_lock<std::mutex> lck(m_mtx);
|
|
prepareInput(pFrame, FFT_SIZE);
|
|
m_fftw_plan_left = fftw_plan_dft_r2c_1d(static_cast<int>(BUFFER_SIZE), m_fftw_input_left,
|
|
m_fftw_output_left, FFTW_ESTIMATE);
|
|
m_fftw_plan_right = fftw_plan_dft_r2c_1d(static_cast<int>(BUFFER_SIZE), m_fftw_input_right,
|
|
m_fftw_output_right, FFTW_ESTIMATE);
|
|
fftw_execute(m_fftw_plan_left);
|
|
fftw_execute(m_fftw_plan_right);
|
|
fftw_destroy_plan(m_fftw_plan_left);
|
|
fftw_destroy_plan(m_fftw_plan_right);
|
|
for (int i = 0; i < FFT_SIZE; ++i) {
|
|
double left = (double(*m_fftw_output_left[i]));
|
|
double right = (double(*m_fftw_output_right[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(pcm_stereo_sample *buffer, uint32_t sample_size) {
|
|
bool is_silent = true;
|
|
for (auto i = 0u; i < sample_size; ++i) {
|
|
m_fftw_input_left[i] = buffer[i].l;
|
|
m_fftw_input_right[i] = buffer[i].r;
|
|
if (is_silent && (m_fftw_input_left[i] > 0 || m_fftw_input_right[i] > 0)) is_silent = false;
|
|
}
|
|
return is_silent;
|
|
}
|
|
|
|
FFT::FFT() {
|
|
m_fftw_results = (static_cast<size_t>(BUFFER_SIZE) / 2) + 1;
|
|
|
|
m_fftw_input_left = static_cast<double *>(
|
|
fftw_malloc(sizeof(double) * BUFFER_SIZE));
|
|
m_fftw_input_right = static_cast<double *>(
|
|
fftw_malloc(sizeof(double) * BUFFER_SIZE));
|
|
|
|
m_fftw_output_left = static_cast<fftw_complex *>(
|
|
fftw_malloc(sizeof(fftw_complex) * m_fftw_results));
|
|
m_fftw_output_right = static_cast<fftw_complex *>(
|
|
fftw_malloc(sizeof(fftw_complex) * m_fftw_results));
|
|
}
|
|
FFT::~FFT() {
|
|
delete m_sample;
|
|
}
|
|
|