Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audio output post processing #953

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silly-dryers-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-agents": patch
---

Add audio output post processing
16 changes: 15 additions & 1 deletion livekit-agents/livekit/agents/multimodal/multimodal_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import asyncio
from dataclasses import dataclass
from typing import Callable, Literal, Protocol
from typing import AsyncIterable,Awaitable, Callable, Literal, Protocol

import aiohttp
from livekit import rtc
Expand Down Expand Up @@ -92,6 +92,10 @@ def __init__(
self._update_state_task: asyncio.Task | None = None
self._http_session: aiohttp.ClientSession | None = None

# audio output post processing
self._audio_processor: Callable[[AsyncIterable[bytes]], Awaitable[AsyncIterable[bytes]]] | None = None


@property
def vad(self) -> vad.VAD | None:
return self._vad
Expand All @@ -104,6 +108,12 @@ def fnc_ctx(self) -> llm.FunctionContext | None:
def fnc_ctx(self, value: llm.FunctionContext | None) -> None:
self._session.fnc_ctx = value

def set_audio_processor(self, processor: Callable[[AsyncIterable[bytes]], Awaitable[AsyncIterable[bytes]]]):
"""
Set a custom audio processor function.
"""
self._audio_processor = processor

def start(
self, room: rtc.Room, participant: rtc.RemoteParticipant | str | None = None
) -> None:
Expand Down Expand Up @@ -145,6 +155,10 @@ def _on_content_added(message: realtime.RealtimeContent):
hyphenate_word=self._opts.transcription.hyphenate_word,
)

audio_stream = message.audio_stream
if self._audio_processor:
audio_stream = self._audio_processor(audio_stream)

self._playing_handle = self._agent_playout.play(
item_id=message.item_id,
content_index=message.content_index,
Expand Down