From 756cf4fa8f77f0209aa9eb70b7e4c6877d3d0220 Mon Sep 17 00:00:00 2001 From: koofrank Date: Fri, 18 Oct 2024 18:00:46 +0800 Subject: [PATCH 1/2] Add audio output post processing --- .../agents/multimodal/multimodal_agent.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/livekit-agents/livekit/agents/multimodal/multimodal_agent.py b/livekit-agents/livekit/agents/multimodal/multimodal_agent.py index e8f3d0337..596d967f0 100644 --- a/livekit-agents/livekit/agents/multimodal/multimodal_agent.py +++ b/livekit-agents/livekit/agents/multimodal/multimodal_agent.py @@ -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 @@ -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 @@ -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: @@ -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, From 352e22c042b592f5b75d8fdfef86f063d08fef9f Mon Sep 17 00:00:00 2001 From: KooFrank Date: Fri, 18 Oct 2024 18:19:47 +0800 Subject: [PATCH 2/2] Create silly-dryers-admire.md --- .changeset/silly-dryers-admire.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/silly-dryers-admire.md diff --git a/.changeset/silly-dryers-admire.md b/.changeset/silly-dryers-admire.md new file mode 100644 index 000000000..f1bc7479e --- /dev/null +++ b/.changeset/silly-dryers-admire.md @@ -0,0 +1,5 @@ +--- +"livekit-agents": patch +--- + +Add audio output post processing