Yes, it can be done.
All you need is a fast FFT algorithm!
First decide the desired frequency resolution, for example, you can set the sampling frequency from your microphone to 8000 Hz, now select one block size, for example 1024 or 2048, to capture from your microphone.
If you select 2048 points and a sampling frequency of 8000, you will have a resolution of frequency = 3.9063 (8000/2048).
Apply one window function to your 2048 points, then apply FFT and get the value!
Remember the sample rate of Nyquist theorems = 8000/2 = 4000, now you know that your FFT can receive frequencies between 3.9063 Hz at 4000 Hz.
FFT bit of the corresponding frequencies:
1 -> 3,90625 hz 2 -> 7,8125 hz 3 -> 11,71875 hz ... 1024 -> 4000 hz ... 2048 - > 8000 hz
To do this, you only need the values โโof the first half of the FFT, for this case 1024.
Now, if you build this data from your FFT, you will have a spectrum!
EDIT
Pseudocode:
#construct one hanning window Function Chunk = 2048; windowed = [Chunk]; hanning = [Chunk]; for i 1:Chunk: hanning[i] = ((1 - cos(i*2*pi/Chunk-1))/2)
source share