From f2cefeeedc7c60e99f0917529f37ddbdfa5e656e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Fri, 17 May 2024 13:02:05 -0700 Subject: [PATCH] utils: move exp_smoothing to utils module --- src/pipecat/services/ai_services.py | 6 ++---- src/pipecat/utils/utils.py | 4 ++++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/pipecat/services/ai_services.py b/src/pipecat/services/ai_services.py index ffb9aeee8..a3cb5cde5 100644 --- a/src/pipecat/services/ai_services.py +++ b/src/pipecat/services/ai_services.py @@ -22,6 +22,7 @@ from pipecat.frames.frames import ( VisionImageRawFrame, ) from pipecat.processors.frame_processor import FrameDirection, FrameProcessor +from pipecat.utils.utils import exp_smoothing class AIService(FrameProcessor): @@ -115,16 +116,13 @@ class STTService(AIService): ww.setframerate(self._sample_rate) return (content, ww) - def _exp_smoothing(self, value: float, prev_value: float, factor: float) -> float: - return prev_value + factor * (value - prev_value) - def _get_smoothed_volume(self, audio: bytes, prev_rms: float, factor: float) -> float: # https://docs.python.org/3/library/array.html audio_array = array.array('h', audio) squares = [sample**2 for sample in audio_array] mean = sum(squares) / len(audio_array) rms = math.sqrt(mean) - return self._exp_smoothing(rms, prev_rms, factor) + return exp_smoothing(rms, prev_rms, factor) async def _append_audio(self, frame: AudioRawFrame): # Try to filter out empty background noise diff --git a/src/pipecat/utils/utils.py b/src/pipecat/utils/utils.py index a72f7234e..0be73191f 100644 --- a/src/pipecat/utils/utils.py +++ b/src/pipecat/utils/utils.py @@ -29,3 +29,7 @@ def obj_count(obj) -> int: else: _COUNTS[name] += 1 return _COUNTS[name] + + +def exp_smoothing(value: float, prev_value: float, factor: float) -> float: + return prev_value + factor * (value - prev_value)