From efa5a061d7b051c9809e62b2c7492c134ada98eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Fri, 17 May 2024 11:37:38 -0700 Subject: [PATCH] silero: simplify int16 -> float32 conversion --- src/pipecat/vad/silero.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/pipecat/vad/silero.py b/src/pipecat/vad/silero.py index a6bb17e89..bfe13affe 100644 --- a/src/pipecat/vad/silero.py +++ b/src/pipecat/vad/silero.py @@ -26,19 +26,6 @@ except ModuleNotFoundError as e: raise Exception(f"Missing module(s): {e}") -# Provided by Alexander Veysov -def int2float(sound): - try: - abs_max = np.abs(sound).max() - sound = sound.astype("float32") - if abs_max > 0: - sound *= 1 / 32768 - sound = sound.squeeze() # depends on the use case - return sound - except ValueError: - return sound - - class SileroVADAnalyzer(VADAnalyzer): def __init__(self, sample_rate=16000): @@ -64,7 +51,8 @@ class SileroVADAnalyzer(VADAnalyzer): def voice_confidence(self, buffer) -> float: try: audio_int16 = np.frombuffer(buffer, np.int16) - audio_float32 = int2float(audio_int16) + # Divide by 32768 because we have signed 16-bit data. + audio_float32 = np.frombuffer(audio_int16, dtype=np.int16).astype(np.float32) / 32768.0 new_confidence = self._model(torch.from_numpy(audio_float32), self.sample_rate).item() return new_confidence except BaseException as e: