Skip to content

Commit

Permalink
feat: toggle looping with soundfile mixer (#693)
Browse files Browse the repository at this point in the history
* feat: toggle looping with soundfile mixer

* Implement PR changes
  • Loading branch information
jamsea authored Nov 8, 2024
1 parent 29925a8 commit d566672
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/pipecat/audio/mixers/soundfile_mixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
#

import asyncio

from typing import Any, Dict, Mapping

import numpy as np
from loguru import logger

from pipecat.audio.mixers.base_audio_mixer import BaseAudioMixer
from pipecat.audio.utils import resample_audio
from pipecat.frames.frames import MixerControlFrame, MixerUpdateSettingsFrame, MixerEnableFrame

from loguru import logger
from pipecat.frames.frames import MixerControlFrame, MixerEnableFrame, MixerUpdateSettingsFrame

try:
import soundfile as sf
Expand Down Expand Up @@ -45,6 +43,7 @@ def __init__(
sound_files: Mapping[str, str],
default_sound: str,
volume: float = 0.4,
loop: bool = True,
**kwargs,
):
super().__init__(**kwargs)
Expand All @@ -56,6 +55,7 @@ def __init__(
self._sounds: Dict[str, Any] = {}
self._current_sound = default_sound
self._mixing = True
self._loop = loop

async def start(self, sample_rate: int):
self._sample_rate = sample_rate
Expand Down Expand Up @@ -85,6 +85,8 @@ async def _update_settings(self, frame: MixerUpdateSettingsFrame):
await self._change_sound(value)
case "volume":
await self._update_volume(value)
case "loop":
await self._update_loop(value)

async def _change_sound(self, sound: str):
if sound in self._sound_files:
Expand All @@ -96,6 +98,9 @@ async def _change_sound(self, sound: str):
async def _update_volume(self, volume: float):
self._volume = volume

async def _update_loop(self, loop: bool):
self._loop = loop

def _load_sound_file(self, sound_name: str, file_name: str):
try:
logger.debug(f"Loading background sound from {file_name}")
Expand All @@ -108,8 +113,8 @@ def _load_sound_file(self, sound_name: str, file_name: str):

# Convert from np to bytes again.
self._sounds[sound_name] = np.frombuffer(audio, dtype=np.int16)
except Exception as ex:
logger.error(f"Unable to open file {file_name}")
except Exception as e:
logger.error(f"Unable to open file {file_name}: {e}")

def _mix_with_sound(self, audio: bytes):
"""Mixes raw audio frames with chunks of the same length from the sound
Expand All @@ -127,6 +132,8 @@ def _mix_with_sound(self, audio: bytes):

# Go back to the beginning if we don't have enough data.
if self._sound_pos + chunk_size > len(sound):
if not self._loop:
return audio
self._sound_pos = 0

start_pos = self._sound_pos
Expand Down

0 comments on commit d566672

Please sign in to comment.