Real-time audio spectrum analyser

Hi, I would like to build a spectrum analyser with Scroll pHAT and pHAT DAC just like the tutorial in Pimoroni (https://learn.pimoroni.com/tutorial/sandyj/scroll-phat-spectrum-analyser). However, I want to analyse real time audio from a speech synthesis program (flite) on my Raspberry Pi Zero, instead of a wav file. What should I do to accomplish this in terms of the coding? Based on the tutorial, is the code below where I might have to modify?

sample_rate = wavfile.getframerate()
no_channels = wavfile.getnchannels()
chunk = 4096

output = aa.PCM(aa.PCM_PLAYBACK, aa.PCM_NORMAL)
output.setchannels(no_channels)
output.setrate(sample_rate)
output.setformat(aa.PCM_FORMAT_S16_LE)
output.setperiodsize(chunk)

I appreciate any advice on how I can go about working on this. Thanks!

There is, as far as I know, no way to modify this example to function in real-time. Short of capturing the output of your speech synthesis program to wav, and then generating a spectrum analysis from that.

Pi VU Meter was created to solve this problem, though: https://github.com/pimoroni/pivumeter

git clone https://github.com/pimoroni/pivumeter
cd pivumeter
./setup.sh scroll-phat

After running this, anything you play to default or pivumeter via ALSA should generate a spectrum analyser on Scroll pHAT.

Thanks for your reply. And if say, I wanted to use my TouchPhat instead, whereby ‘Back’, ‘A’, ‘B’, ‘C’, ‘D’, ‘Enter’ LED buttons would light up in response to the presence of certain frequencies (no need for amplitude magnitude display), would that be feasible as well? I note that pivumeter supports only several add-on devices.

The newer- still in development- version of Pi VU Meter has support for anything that has a Python library.

See the examples here: https://github.com/pimoroni/pivumeter/tree/devel/python_server/examples

To installer the newer version you would change your clone to:

git clone https://github.com/pimoroni/pivumeter -b devel