Skip to content

Instantly share code, notes, and snippets.

@diessica
Created February 10, 2018 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diessica/dec0a386bbc29128681a5782980fb333 to your computer and use it in GitHub Desktop.
Save diessica/dec0a386bbc29128681a5782980fb333 to your computer and use it in GitHub Desktop.
import pyaudio
import audioop
import serial
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
arduino = serial.Serial('/dev/ttyUSB0')
p = pyaudio.PyAudio()
stream = p.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK
)
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
rms = audioop.rms(data, 2) # here's where you calculate the volume
arduino.write(rms)
print(rms)
stream.stop_stream()
stream.close()
p.terminate()
arduino.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment