wiseman / py-webrtcvad

Python interface to the WebRTC Voice Activity Detector
Other
1.95k stars 398 forks source link

Using microphone detection #92

Open vedbobo opened 2 months ago

vedbobo commented 2 months ago

import webrtcvad import pyaudio
import os
CHUNK = 1024 FORMAT = pyaudio.paInt16
CHANNELS = 1 RATE = 16000 WAVE_OUTPUT_DIR = "temp"

if not os.path.exists(WAVE_OUTPUT_DIR):
os.makedirs(WAVE_OUTPUT_DIR)
WAVE_OUTPUT_FILENAME = os.path.join(WAVE_OUTPUT_DIR, "output.wav")

p = pyaudio.PyAudio()
vad = webrtcvad.Vad(1)

stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)

frames = []
print("start...") while True: data = stream.read(CHUNK) is_speech = vad.is_speech(data, sample_rate = RATE) if is_speech: frames.append(data) else: break print("end...")
stream.stop_stream()
stream.close()
p.terminate()

Prompt:Error while processing frame

is_speech = vad.is_speech(data, sample_rate = RATE) How to handle this data

Wetosi commented 2 months ago

Instead of "data = stream.read(CHUNK)", do "data = stream.read(int(RATE/100))".