PDA

View Full Version : Real time audio processing



Bangalaman
17th June 2014, 13:17
I would like to make real time audio processing with Qt and display the magnitude using FFTW3.

What I've done in steps:

I capture any sound from computer device and fill it into the buffer.
I assign sound samples to double array
I compute the magnitude.
Problem

My code always returns "nan" as magnitude.
hier is my code

QByteArray *buffer;
QAudioInput *audioInput;
audioInput = new QAudioInput(format, this);

//Check the number of samples in input buffer
qint64 len = audioInput->bytesReady();

//Limit sample size
if(len > 4096)
len = 4096;

//Read sound samples from input device to buffer
qint64 l = input->read(buffer.data(), len);

if(l > 0)
{
int input_size = BufferSize;

// Compute corresponding number of complex output samples
int output_size = (input_size/2 + 1);
double *input_buffer = static_cast<double*>(fftw_malloc(input_size * sizeof(double)));
fftw_complex *out = static_cast<fftw_complex*>(fftw_malloc(output_size * sizeof(fftw_complex)));

//Assign sound samples to double array
input_buffer = (double*)buffer.data();
fftw_plan p3;

//Create plan
p3 = fftw_plan_dft_r2c_1d(input_size, input_buffer, out, FFTW_ESTIMATE);

fftw_execute(p3);
double reout[BufferSize];
double imgout[BufferSize];
double magnitude[BufferSize/2];

long ffond = 0.0; // Position of the frequency
double max = 0; // Maximal amplitude

for (int i = 0; i < BufferSize/2; i++)
{
reout[i] = out[i][0];
imgout[i] = out[i][1];
cout << imgout[i] << endl;
magnitude[i] = sqrt(reout[i]*reout[i] + imgout[i]*imgout[i]); //Calculate magnitude of first
double t = sqrt(reout[i]*reout[i] + imgout[i]*imgout[i]);
qDebug() << t;


}


fftw_destroy_plan(p3);