uxoxo/eb2ab
0
1import os2import numpy as np3import librosa4 5from pyannote.audio import Model6from pyannote.audio.pipelines import VoiceActivityDetection7from lib.conf import tts_dir8from lib.models import default_voice_detection_model9 10class BackgroundDetector:11 12 def __init__(self, wav_file: str):13 self.wav_file = wav_file14 model = Model.from_pretrained(default_voice_detection_model, cache_dir=tts_dir)15 self.pipeline = VoiceActivityDetection(segmentation=model)16 hyper_params = {17 # onset/offset activation thresholds18 "onset": 0.5, "offset": 0.5,19 # remove speech regions shorter than that many seconds.20 "min_duration_on": 0.0,21 # fill non-speech regions shorter than that many seconds.22 "min_duration_off": 0.023 }24 self.pipeline.instantiate(hyper_params)25 26 def detect(self, vad_ratio_thresh: float=0.05):27 diarization = self.pipeline(self.wav_file)28 speech_segments = [(s.start, s.end) for s in diarization.get_timeline()]29 total_duration = librosa.get_duration(path=self.wav_file)30 speech_time = sum(end - start for start, end in speech_segments)31 non_speech_ratio = 1 - (speech_time / total_duration)32 status = non_speech_ratio > vad_ratio_thresh33 report = {34 'non_speech_ratio': non_speech_ratio,35 'background_detected': status36 }37 return status, report