Skip to content

Instantly share code, notes, and snippets.

@baali
Last active June 6, 2016 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save baali/5516964 to your computer and use it in GitHub Desktop.
Save baali/5516964 to your computer and use it in GitHub Desktop.
Python script to plot amplitude of noise levels
import sys
import audioop
try:
import pyaudio
except ImportError:
sys.exit('You need to install pyaudio to installed to run this demo.')
SAMPLING_RATE = 22050
NUM_SAMPLES = 1024
line = None
_stream = None
def getRMS():
global _stream
if _stream is None:
pa = pyaudio.PyAudio()
_stream = pa.open(format=pyaudio.paInt16, channels=2, rate=SAMPLING_RATE,
input=True, frames_per_buffer=NUM_SAMPLES)
try:
data = _stream.read(NUM_SAMPLES)
audio_data = array('i', data)
rms = audioop.rms(audio_data, 2)
return rms
except Exception, e:
# print 'Exception occurred %s' % traceback.format_exc(e)
return -1
if __name__ == "__main__":
while True:
getRMS()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment