audio: add frames to allow playing/pausing bot background sound
This commit is contained in:
@@ -40,10 +40,20 @@ except ModuleNotFoundError as e:
|
|||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class ChangeBotBackgroundFrame(ControlFrame):
|
class ChangeBotBackgroundSoundFrame(ControlFrame):
|
||||||
sound_name: str
|
sound_name: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class PlayBotBackgroundSoundFrame(ControlFrame):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class PauseBotBackgroundSoundFrame(ControlFrame):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class BotBackgroundSound(FrameProcessor):
|
class BotBackgroundSound(FrameProcessor):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -58,9 +68,10 @@ class BotBackgroundSound(FrameProcessor):
|
|||||||
self._volume = volume
|
self._volume = volume
|
||||||
self._sample_rate = sample_rate
|
self._sample_rate = sample_rate
|
||||||
|
|
||||||
|
self._sound_pos = 0
|
||||||
self._sounds: Dict[str, Any] = {}
|
self._sounds: Dict[str, Any] = {}
|
||||||
self._current_sound = default_sound
|
self._current_sound = default_sound
|
||||||
self._sound_pos = 0
|
self._playing = True
|
||||||
|
|
||||||
self._bot_speaking = False
|
self._bot_speaking = False
|
||||||
self._sleep_time = 0.02
|
self._sleep_time = 0.02
|
||||||
@@ -81,8 +92,12 @@ class BotBackgroundSound(FrameProcessor):
|
|||||||
elif isinstance(frame, TTSAudioRawFrame):
|
elif isinstance(frame, TTSAudioRawFrame):
|
||||||
frame.audio = self._mix_with_sound(frame.audio)
|
frame.audio = self._mix_with_sound(frame.audio)
|
||||||
await self.push_frame(frame)
|
await self.push_frame(frame)
|
||||||
elif isinstance(frame, ChangeBotBackgroundFrame):
|
elif isinstance(frame, ChangeBotBackgroundSoundFrame):
|
||||||
await self._change_background_sound(frame)
|
await self._change_background_sound(frame)
|
||||||
|
elif isinstance(frame, PlayBotBackgroundSoundFrame):
|
||||||
|
await self._play_background_sound(True)
|
||||||
|
elif isinstance(frame, PauseBotBackgroundSoundFrame):
|
||||||
|
await self._play_background_sound(False)
|
||||||
else:
|
else:
|
||||||
await self.push_frame(frame, direction)
|
await self.push_frame(frame, direction)
|
||||||
|
|
||||||
@@ -97,7 +112,10 @@ class BotBackgroundSound(FrameProcessor):
|
|||||||
self._audio_task.cancel()
|
self._audio_task.cancel()
|
||||||
await self._audio_task
|
await self._audio_task
|
||||||
|
|
||||||
async def _change_background_sound(self, frame: ChangeBotBackgroundFrame):
|
async def _play_background_sound(self, play: bool):
|
||||||
|
self._playing = play
|
||||||
|
|
||||||
|
async def _change_background_sound(self, frame: ChangeBotBackgroundSoundFrame):
|
||||||
if frame.sound_name in self._sound_files:
|
if frame.sound_name in self._sound_files:
|
||||||
self._current_sound = frame.sound_name
|
self._current_sound = frame.sound_name
|
||||||
self._sound_pos = 0
|
self._sound_pos = 0
|
||||||
@@ -154,7 +172,7 @@ class BotBackgroundSound(FrameProcessor):
|
|||||||
async def _audio_task_handler(self):
|
async def _audio_task_handler(self):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
if not self._bot_speaking:
|
if self._playing and not self._bot_speaking:
|
||||||
audio = self._mix_with_sound(b"")
|
audio = self._mix_with_sound(b"")
|
||||||
frame = OutputAudioRawFrame(
|
frame = OutputAudioRawFrame(
|
||||||
audio=audio, sample_rate=self._sample_rate, num_channels=1
|
audio=audio, sample_rate=self._sample_rate, num_channels=1
|
||||||
|
|||||||
Reference in New Issue
Block a user