import pyaudio
import audioop
import math
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 1
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,
exception_on_overflow=False)
rms = audioop.rms(data, 2)
decibel = 20 * math.log10(rms)
print(decibel)
stream.stop_stream()
stream.close()
p.terminate()