From dd89ac1bb82620bba847dbe4429eef53fcf9f367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Fri, 1 Nov 2024 14:22:29 -0700 Subject: [PATCH] audio: add frames to update bot background sound volume --- .../processors/audio/bot_background_sound.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/pipecat/processors/audio/bot_background_sound.py b/src/pipecat/processors/audio/bot_background_sound.py index c0761a169..25e889d53 100644 --- a/src/pipecat/processors/audio/bot_background_sound.py +++ b/src/pipecat/processors/audio/bot_background_sound.py @@ -7,9 +7,8 @@ import asyncio from dataclasses import dataclass -from typing import Any, Dict, Mapping +from typing import Any, Dict, Mapping, Optional -from botocore.model import instance_cache import numpy as np from pipecat.audio.utils import resample_audio @@ -42,6 +41,12 @@ except ModuleNotFoundError as e: @dataclass class ChangeBotBackgroundSoundFrame(ControlFrame): sound_name: str + volume: Optional[float] = None + + +@dataclass +class UpdateBotBackgroundSoundVolumeFrame(ControlFrame): + volume: float @dataclass @@ -94,6 +99,8 @@ class BotBackgroundSound(FrameProcessor): await self.push_frame(frame) elif isinstance(frame, ChangeBotBackgroundSoundFrame): await self._change_background_sound(frame) + elif isinstance(frame, UpdateBotBackgroundSoundVolumeFrame): + await self._update_background_volume(frame.volume) elif isinstance(frame, PlayBotBackgroundSoundFrame): await self._play_background_sound(True) elif isinstance(frame, PauseBotBackgroundSoundFrame): @@ -117,6 +124,8 @@ class BotBackgroundSound(FrameProcessor): async def _change_background_sound(self, frame: ChangeBotBackgroundSoundFrame): if frame.sound_name in self._sound_files: + if frame.volume: + await self._update_background_volume(frame.volume) self._current_sound = frame.sound_name self._sound_pos = 0 else: @@ -124,6 +133,9 @@ class BotBackgroundSound(FrameProcessor): logger.error(error_msg) await self.push_error(ErrorFrame(error_msg)) + async def _update_background_volume(self, volume: float): + self._volume = volume + def _load_sound_file(self, sound_name: str, file_name: str): try: logger.debug(f"{self} loading background sound from {file_name}")