We have to initialize the resources for OpenGL to plot the spectrum.

getFft() is the function we use to apply FFT. Lets create that function.

Now lets create getData() function which does all the work of getting N samples, applying window function,
applying FFT and storing in an array graph so that OpenGL can plot the points. timestamp_t t0 gets the current time. This will be useful to find the total execution time.

Input audio name is stored as fileName to open it using Aquila with the name we use Aquila::WaveFile wav(fileName); . mag[N/2] stores the magnitude values after applying FFT. roof stores the total samples count.

Lets collect N samples from the audio input. Initialize i = framePointer and j = 0. Initially framePointer value is zero. We will update the framePointer when one frame is fetched. Iterate the loop to take N samplings
until the framePointer < roof -N. Note that roof is the total sample count. in[i].r is the real part and in[j].i is the imaginary part. Since we donâ€™t have any imaginary part in the samples we will set the imaginary part of the input to zero.

Apply Window Function on the input. We are using Hann Window function on the input.

Update the frame pointer if framePointer < roof-N -1. Else print the values and exit.

Now we have N samples with us, we need to apply FFT on the samples. getFft() is the function that we created to apply FFT. We then calculate magnitude of first n/2 FFT and store in mag[N/2]. Optionally you can convert it into dB scale by 10 * log10(mag). Log magnitude values are stored in graph[] to plot the points.

Now we can plot the points in graph[] which we generated using OpenGL.