Skip to content

Instantly share code, notes, and snippets.

@kylemcdonald
Created October 11, 2023 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylemcdonald/bf27d7b231748c2a478561560154715d to your computer and use it in GitHub Desktop.
Save kylemcdonald/bf27d7b231748c2a478561560154715d to your computer and use it in GitHub Desktop.
Show microphone level in realtime using pyaudio.
import pyaudio
import audioop
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=1024)
try:
while True:
data = stream.read(1024)
rms = audioop.rms(data, 2)
max_n = 100
n = min(int(rms//2), max_n)
blank = max_n - n
print(f"RMS: {n *'█'}{blank*' '}", end='\r')
except KeyboardInterrupt:
stream.stop_stream()
stream.close()
p.terminate()
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment