S
S
s4q2021-03-19 20:09:13
linux
s4q, 2021-03-19 20:09:13

How to detect microphone volume via PyAudio on Linux?

Need to detect sound volume (in decibels) in microphone using PyAudio

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy _, 2021-03-19
@s4q

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()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question