Skip to main content
AgentScope Java provides comprehensive TTS capabilities, enabling Agents not only to think and respond, but also to speak. Compared to text-only scenarios, voice is a more natural interaction method, suitable for intelligent customer service, in-car assistants, and real-time conversation scenarios that generate and speak simultaneously.

Choosing the Right Model

Non-real-time Model: Suitable for scenarios that require high audio quality and can accept a few seconds of delay, such as podcasts, audiobooks, and short video dubbing. Uses DashScopeTTSModel and qwen3-tts-flash model, sending complete text at once and waiting for the server to process the entire audio before returning. The model can globally optimize stress, intonation, and emotion for the entire speech, achieving the best audio quality. Real-time Model: Suitable for scenarios that require high response speed and need to generate and play simultaneously, such as AI assistants and real-time translation. Uses DashScopeRealtimeTTSModel and qwen3-tts-flash-realtime model, streaming text chunks and the server returns audio chunks in real-time with lower latency. Although synthesized in chunks, the model also maintains a context window to preserve naturalness.

Usage Methods

AgentScope provides three ways to use TTS: ReActAgent Integration: By adding TTSHook to ReActAgent, you can achieve automatic speech for all Agent responses. Simply adding TTSHook enables the speak-while-generating effect. Standalone TTSModel Usage: Independent of Agent, directly call TTSModel for standalone speech synthesis, providing flexible usage suitable for scenarios that require separate voice conversion. Using DashScopeMultiModalTool as a Tool: Provide TTS as a multimodal tool to Agent, allowing Agent to decide when to convert text to speech.

Method 1: ReActAgent Integration

By adding TTSHook to ReActAgent, ReActAgent can automatically speak when responding. Working Principle:
  • Event Listening Mechanism: TTSHook implements the Hook interface and listens to events during Agent execution. When Agent starts reasoning, it triggers PreReasoningEvent, when generating text chunks it triggers ReasoningChunkEvent, and when reasoning completes it triggers PostReasoningEvent.
  • Real-time Streaming Synthesis: In real-time mode, TTSHook listens to ReasoningChunkEvent. Whenever Agent generates a text chunk, it immediately pushes it to the TTS model via WebSocket for speech synthesis. This achieves the “speak-while-generating” effect, with users feeling almost no delay.
  • Session Lifecycle Management: When receiving the first text chunk, TTSHook starts a TTS session (establishes WebSocket connection) and subscribes to the audio stream. When Agent reasoning completes, it calls finish() to commit remaining text and close the session, ensuring all audio is synthesized and played.
  • Audio Distribution Mechanism: Generated audio blocks are distributed in three ways: 1) Sent to reactive stream (audioSink) for SSE/WebSocket frontend subscription; 2) Call audioCallback callback function for custom processing; 3) Play locally via AudioPlayer, suitable for CLI/desktop applications.
  • Playback Interruption Handling: When new reasoning starts (PreReasoningEvent), TTSHook interrupts currently playing audio, closes the old TTS session, ensuring new response audio can start playing immediately, avoiding audio confusion.

Local Playback Mode (CLI/Desktop Application)

Uses WebSocket real-time streaming synthesis, supporting speak-while-generating:

Server Mode (Web/SSE)

In web applications, audio needs to be sent to the frontend for playback. You can send audio to the frontend via SSE or use reactive streams. Complete code can be found in the agentscope-examples/documentation/chat-tts module, which includes frontend and backend interaction.

Method 2: Standalone TTSModel Usage

Independent of Agent, directly call TTS model for speech synthesis.

2.1 Non-real-time Mode

Suitable for returning complete audio at once:

2.2 Real-time Mode - Incremental Input (Push/Finish Mode)

Suitable for LLM streaming output scenarios, synthesize while receiving text:

SessionMode Description


Method 3: DashScopeMultiModalTool (As Agent Tool)

Agent calls TTS via tool, Agent decides when to convert text to speech:

Complete Examples

  • Quick Start: agentscope-examples/documentation/quickstart/TTSExample.java
  • Complete Example: agentscope-examples/documentation/chat-tts module, includes frontend and backend interaction

Core Component Configuration Parameters

DashScopeRealtimeTTSModel

DashScopeTTSModel

TTSHook