类 DashScopeRealtimeTTSModel

java.lang.Object
io.agentscope.core.model.tts.DashScopeRealtimeTTSModel
所有已实现的接口:
RealtimeTTSModel, TTSModel

public class DashScopeRealtimeTTSModel extends Object implements RealtimeTTSModel
DashScope Realtime TTS Model with WebSocket streaming input support.

This model uses DashScope's WebSocket-based TTS API via WebSocketTransport, enabling true streaming input where text can be pushed incrementally while maintaining context continuity for natural prosody and intonation.

Key Features:

  • WebSocket-based streaming with WebSocketTransport
  • Supports both server_commit and commit modes
  • push(text) - Push text incrementally, TTS maintains context
  • finish() - Signal end of input, get remaining audio
  • Solves prosody/intonation issues that occur with independent HTTP requests
  • No 600-character limit per request (streaming input)

Session Modes:

  • server_commit - Server automatically commits text buffer for synthesis
  • commit - Client must manually commit text buffer

Usage Example:


 DashScopeRealtimeTTSModel tts = DashScopeRealtimeTTSModel.builder()
     .apiKey(apiKey)
     .modelName("qwen3-tts-flash-realtime")
     .voice("Cherry")
     .mode(SessionMode.SERVER_COMMIT)
     .build();

 // Start a streaming session
 tts.startSession();

 // Push text chunks as LLM generates them (context is maintained!)
 tts.push("Hello, ").subscribe(audio -> player.play(audio));
 tts.push("welcome to ").subscribe(audio -> player.play(audio));
 tts.push("AgentScope.").subscribe(audio -> player.play(audio));

 // Finish and get remaining audio
 tts.finish().subscribe(audio -> player.play(audio));
 
  • 方法详细资料

    • supportsStreamingInput

      public boolean supportsStreamingInput()
      Returns true if this model supports streaming input (push/finish pattern).
      返回:
      always true for this implementation
    • startSession

      public void startSession()
      Starts a new TTS session with WebSocket connection.

      This establishes a WebSocket connection to DashScope's TTS service. Call this before using push()/finish() pattern.

      指定者:
      startSession 在接口中 RealtimeTTSModel
    • push

      public reactor.core.publisher.Flux<AudioBlock> push(String text)
      Pushes a text chunk for synthesis.

      Unlike HTTP-based TTS, this maintains context continuity across multiple push() calls, resulting in natural prosody and intonation.

      Note: This method returns empty Flux. Audio is delivered through getAudioStream() to avoid duplicate subscriptions causing repeated audio playback.

      指定者:
      push 在接口中 RealtimeTTSModel
      参数:
      text - the text chunk to synthesize
      返回:
      empty Flux (audio delivered via getAudioStream)
    • commitTextBuffer

      public void commitTextBuffer()
      Commits the text buffer to trigger processing.

      Only needed in DashScopeRealtimeTTSModel.SessionMode.COMMIT mode. In DashScopeRealtimeTTSModel.SessionMode.SERVER_COMMIT mode, the server commits automatically.

    • clearTextBuffer

      public void clearTextBuffer()
      Clears the text buffer.
    • finish

      public reactor.core.publisher.Flux<AudioBlock> finish()
      Finishes the streaming session and retrieves remaining audio.

      This signals the end of input and waits for all audio to be generated. The sink will be completed when the server sends the "session.finished" event.

      指定者:
      finish 在接口中 RealtimeTTSModel
      返回:
      Flux of remaining AudioBlock chunks
    • waitForResponseDone

      public boolean waitForResponseDone(long timeout, TimeUnit unit)
      Waits for the current response to complete.
      参数:
      timeout - the maximum time to wait
      unit - the time unit of the timeout
      返回:
      true if the response completed within the timeout
    • close

      public void close()
      Closes the WebSocket connection and releases resources.
      指定者:
      close 在接口中 RealtimeTTSModel
    • getAudioStream

      public reactor.core.publisher.Flux<AudioBlock> getAudioStream()
      Gets the audio stream for listening to all audio chunks.
      指定者:
      getAudioStream 在接口中 RealtimeTTSModel
      返回:
      Flux of AudioBlock that emits audio as it's synthesized
    • synthesizeStream

      public reactor.core.publisher.Flux<AudioBlock> synthesizeStream(String text)
      Synthesizes complete text to audio using streaming.

      This is a convenience method that creates a session, pushes all text, and returns the complete audio stream.

      指定者:
      synthesizeStream 在接口中 RealtimeTTSModel
      参数:
      text - the complete text to synthesize
      返回:
      Flux of AudioBlock chunks as they are generated
    • synthesize

      public reactor.core.publisher.Mono<TTSResponse> synthesize(String text, TTSOptions options)
      Synthesizes text to audio (blocking).
      指定者:
      synthesize 在接口中 TTSModel
      参数:
      text - the text to synthesize
      options - optional TTS options (may be null)
      返回:
      Mono containing the TTS response with audio data
    • getModelName

      public String getModelName()
      Gets the model name.
      指定者:
      getModelName 在接口中 TTSModel
      返回:
      the model name
    • builder

      public static DashScopeRealtimeTTSModel.Builder builder()
      Creates a new builder for DashScopeRealtimeTTSModel.
      返回:
      a new Builder instance