Description
Sorry for spamming here, I wanted to comment on https://www.swharden.com/wp/2016-07-19-realtime-audio-visualization-in-python/ but I couldn't manage to comment over there ...
You are showing this code snippet:
import sounddevice #pip install sounddevice
for i in range(30): #30 updates in 1 second
rec = sounddevice.rec(44100/30)
sounddevice.wait()
print(rec.shape)
The function sounddevice.rec()
is really not meant to be used repeatedly in quick succession.
It's really just a high-level wrapper for the case where you want to record one piece of audio and you know the duration beforehand. It's no wonder that it creates glitches if you use it like in your example, because for each call a new "stream" object is created, audio processing is started, then the (very short) recording is done, audio processing is stopped and the "stream" object is destroyed. Just to create a new "stream" object an instance later ...
Since you are using stream.read()
from PyAudio, you could as well use stream.read()
from the sounddevice
module: https://python-sounddevice.readthedocs.io/en/latest/api.html#sounddevice.Stream.read.
This uses the exact same underlying PortAudio function call, it just does the conversion to a NumPy array internally without you having to worry about it.
This should take about the same amount of resources than PyAudio (only a bit more for the very little overhead of CFFI calls in Python code, probably not really measurable).
And it should definitely not produce any glitches (given that the same block size and latency settings are used as in PyAudio).
As a (not necessarily better) alternative to using stream.read()
you can also implement your own callback function (as you could also do in PyAudio, but again the NumPy array conversion is done for you). I've provided an example program for recordings where the duration is not known in advance: https://github.com/spatialaudio/python-sounddevice/blob/master/examples/rec_unlimited.py.
BTW, I'm the author of the sounddevice
module, in case you didn't know.