Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAudioBuffer to FFT
Forum Update on Monday, May 27th 2025

QAudioBuffer to FFT

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pajczur
    wrote on last edited by
    #1

    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

    K 1 Reply Last reply
    0
    • P Pajczur

      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

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved