można spróbować coś takiego:
podstawie this question/answer
# this is the threshold that determines whether or not sound is detected
THRESHOLD = 0
#open your audio stream
# wait until the sound data breaks some level threshold
while True:
data = stream.read(chunk)
# check level against threshold, you'll have to write getLevel()
if getLevel(data) > THRESHOLD:
break
# record for however long you want
# close the stream
Prawdopodobnie będziesz chciał grać z wielkości i wartości progowych fragmentach aż do uzyskania pożądanego zachowania.
Edit:
Można użyć wbudowanego audioop pakiecie znaleźć korzeń średnią kwadratową (RMS) próbki, która jest ogólnie w jaki sposób można uzyskać poziom.
import audioop
import pyaudio
chunk = 1024
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=chunk)
data = stream.read(chunk)
rms = audioop.rms(data, 2) #width=2 for format=paInt16