Created
February 10, 2018 12:12
-
-
Save diessica/dec0a386bbc29128681a5782980fb333 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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