audio: add frame to allow changing bot background sound

This commit is contained in:
Aleix Conchillo Flaqué
2024-11-01 08:59:11 -07:00
parent f96cf2292b
commit cca6f1fe45

View File

@@ -6,14 +6,18 @@
import asyncio
from dataclasses import dataclass
from typing import Any, Dict, Mapping
from botocore.model import instance_cache
import numpy as np
from pipecat.audio.utils import resample_audio
from pipecat.processors.frame_processor import FrameProcessor, FrameDirection
from pipecat.frames.frames import (
CancelFrame,
ControlFrame,
ErrorFrame,
OutputAudioRawFrame,
Frame,
EndFrame,
@@ -35,6 +39,11 @@ except ModuleNotFoundError as e:
raise Exception(f"Missing module: {e}")
@dataclass
class ChangeBotBackgroundFrame(ControlFrame):
sound_name: str
class BotBackgroundSound(FrameProcessor):
def __init__(
self,
@@ -72,6 +81,8 @@ class BotBackgroundSound(FrameProcessor):
elif isinstance(frame, TTSAudioRawFrame):
frame.audio = self._mix_with_sound(frame.audio)
await self.push_frame(frame)
elif isinstance(frame, ChangeBotBackgroundFrame):
await self._change_background_sound(frame)
else:
await self.push_frame(frame, direction)
@@ -86,6 +97,15 @@ class BotBackgroundSound(FrameProcessor):
self._audio_task.cancel()
await self._audio_task
async def _change_background_sound(self, frame: ChangeBotBackgroundFrame):
if frame.sound_name in self._sound_files:
self._current_sound = frame.sound_name
self._sound_pos = 0
else:
error_msg = f"{self} sound {frame.sound_name} is not available"
logger.error(error_msg)
await self.push_error(ErrorFrame(error_msg))
def _load_sound_file(self, sound_name: str, file_name: str):
try:
logger.debug(f"{self} loading background sound from {file_name}")