narrow-chef-71655
12/11/2025, 6:02 PMtall-airline-16756
12/11/2025, 6:02 PMtall-airline-16756
12/11/2025, 6:03 PMimport asyncio
from livekit import agents, rtc
from livekit.agents import JobContext, WorkerOptions, cli
from livekit.agents.voice import VoicePipelineAgent
from livekit.agents.llm import FunctionContext
# Replace these plugin imports with your configured plugin bindings
from livekit.plugins import deepgram, openai, cartesia
# Translator agent: subscribes to source_id, translates to target_lang, publishes to target_id
class DirectionalTranslator:
def __init__(self, source_id: str, target_id: str, stt_lang: str, tts_voice: str):
self.source_id = source_id
self.target_id = target_id
self.stt = deepgram.STT(language=stt_lang)
self.tts = cartesia.TTS(voice=tts_voice)
self.llm = openai.LLM(model="gpt-4") # used for translation step
self.fnc_ctx = FunctionContext()
self.agent_session: VoicePipelineAgent | None = None
async def start(self, ctx: JobContext):
# Create a voice pipeline but disable auto subscription so we control subscriptions
self.agent_session = VoicePipelineAgent(
ctx.room,
stt=self.stt,
tts=self.tts,
llm=self.llm,
fnc_ctx=self.fnc_ctx,
auto_subscribe=agents.AutoSubscribe.SUBSCRIBE_NONE,
)
# Wait for the source participant and subscribe only to their microphone track(s)
src = await ctx.room.wait_for_participant(self.source_id)
for pub in src.tracks.values():
if pub.source == rtc.TrackSource.SOURCE_MICROPHONE:
await pub.set_subscribed(True)
await self.agent_session.start()
# Override speak/publish behavior: synthesize and publish only to the target participant
original_speak = self.agent_session.agent.speak
async def speak_to_target(text: str):
# synthesize audio buffer/track from text
output = await self.agent_session.synthesize(text)
tgt = await self.agent_session.room.wait_for_participant(self.target_id)
# publish synthesized audio to the target participant only
# create a local audio track on behalf of the room/server and publish to target
await tgt.local_audio_track.publish(output.track)
# replace speak with our targeted speak
self.agent_session.agent.speak = speak_to_target
# Entrypoints to register two workers: A->B and B->A
async def entrypoint_a_to_b(ctx: JobContext):
# A speaks English; B should hear Spanish
translator = DirectionalTranslator(
source_id="A",
target_id="B",
stt_lang="en",
tts_voice="male-es-v1",
)
await translator.start(ctx)
# keep running until job stops
await asyncio.Event().wait()
async def entrypoint_b_to_a(ctx: JobContext):
# B speaks Spanish; A should hear English
translator = DirectionalTranslator(
source_id="B",
target_id="A",
stt_lang="es",
tts_voice="female-en-v1",
)
await translator.start(ctx)
await asyncio.Event().wait()
if __name__ == "__main__":
cli.register_worker(
WorkerOptions(
worker_type=agents.WorkerType.JOB,
entrypoint_fnc=entrypoint_a_to_b,
name="translator-a-to-b",
)
)
cli.register_worker(
WorkerOptions(
worker_type=agents.WorkerType.JOB,
entrypoint_fnc=entrypoint_b_to_a,
name="translator-b-to-a",
)
)
cli.run_app()
Key points to implement on your side:
• STT: configure language models/recognizers for the source language.
• Translation: use the LLM or translation API to convert recognized text to the target language before TTS.
• TTS: select voices for clarity and latency trade-offs.
• Subscriptions: ensure agents subscribe only to the source mic to prevent hearing original audio.
• Publishing: publish synthesized audio as a local audio track visible only to the intended participant.
If you'd like, I can:
• Show the missing translation step inside the pipeline (STT → LLM translate → TTS) with code snippets, or
• Adapt the example to use different plugin providers or to suppress direct peer audio on the client side. Which would you prefer? Sources: LiveKit Docsnarrow-chef-71655
12/11/2025, 6:05 PMtall-airline-16756
12/11/2025, 6:05 PMnarrow-chef-71655
12/11/2025, 6:08 PMtall-airline-16756
12/11/2025, 6:08 PMnarrow-chef-71655
12/11/2025, 6:19 PMtall-airline-16756
12/11/2025, 6:19 PM