QAudioBuffer to FFT



  • Hello,
    I'm working on my school project, and my task is to get musical tone value (for example for 440Hz sine wave it's tone "A") in real time from audio recorder.

    What I remember from math lessons, the fast fourier transformation would be great to solve it. But implementing it in C++ it's not easy for me. I've spent today some time to implement FFTW project to my code, and I think I am on good way, but now I can't figure out how to send audio signal to my fft_function.

    I tried something like that:

    void MainWindow::fftSlot(QAudioBuffer buffer) // it's a public slot
    {
        int n = 44100;
        fftw_complex x[n]; // input
        fftw_complex y[n]; // output
    
        const double *data = buffer.data<double>();
    
        for(int i=0; i<=n; i++)
        {
           x[i][REAL] = *data;
           x[i][IMAG] = 0;
        }
        
        fftw_plan myPlan = fftw_plan_dft_1d(n, x, y, FFTW_FORWARD, FFTW_ESTIMATE);
        fftw_execute(myPlan);
    }
    

    But ot doesn't work for my. When I start that:

    QAudioProbe *probka;
    probka = new QAudioProbe;
    connect(probka, SIGNAL(audioBufferProbed(QAudioBuffer)), this, SLOT(fftSlot(QAudioBuffer)));
    

    The program crashes with info: "The program has unexpectedly finished."

    I'm pretty sure it's because that strange loop in slot. But I have no idea how to impute QAudioBuffer to the fftw_complex x[n], and what should be in that case int n=???; In the code above I use 44100, a little bit by lottery, I thought "hmm... maybe it should be sample rate from buffer.format()". But I'm not sure if it's matter, and if any "n" integer should be here at all.

    Maybe anybody could help?
    For any help thanks in advance.
    Best Regards


  • Moderators

    @Pajczur

    You are completely right. Your error is here:

    for(int i=0; i<=n; i++)
    

    You allocate only 44100 element, but you use 44101.


Log in to reply