类 TTSHook

java.lang.Object
io.agentscope.core.hook.TTSHook
所有已实现的接口:
Hook

public class TTSHook extends Object implements Hook
Hook for real-time Text-to-Speech synthesis during agent execution.

This hook implements "speak as you generate" by listening to streaming reasoning events and synthesizing speech in real-time.

Two Usage Modes:

  • Local Playback (CLI/Desktop): Use audioPlayer for direct playback
  • Server Mode (Web/SSE): Use audioCallback to return audio to frontend

Example 1: Local Playback (CLI/Testing)


 AudioPlayer player = AudioPlayer.builder().sampleRate(24000).build();

 TTSHook ttsHook = TTSHook.builder()
     .ttsModel(ttsModel)
     .audioPlayer(player)  // Local playback
     .build();
 

Example 2: Server Mode (Return to Frontend via SSE)


 TTSHook ttsHook = TTSHook.builder()
     .ttsModel(ttsModel)
     .audioCallback(audio -> {
         // Send via SSE/WebSocket to frontend
         sseEmitter.send(audio);
     })
     .build();
 

Example 3: Get Audio Stream (Reactive)


 TTSHook ttsHook = TTSHook.builder()
     .ttsModel(ttsModel)
     .build();

 // Subscribe to audio stream
 ttsHook.getAudioStream()
     .subscribe(audio -> sendToClient(audio));
 
  • 方法详细资料

    • getAudioStream

      public reactor.core.publisher.Flux<AudioBlock> getAudioStream()
      Gets the reactive audio stream.

      Use this to subscribe to audio blocks as they are generated. This is useful for SSE/WebSocket streaming to frontend.

      Example:

      
       ttsHook.getAudioStream()
           .map(audio -> ((Base64Source) audio.getSource()).getData())
           .subscribe(base64 -> sseEmitter.send(base64));
       
      返回:
      Flux of AudioBlock that emits audio as it's synthesized
    • onEvent

      public <T extends HookEvent> reactor.core.publisher.Mono<T> onEvent(T event)
      从接口复制的说明: Hook
      Handle a hook event.

      This method is called for all agent execution events. Use pattern matching to handle specific event types.

      Modifiable Events: For events with setters, you can modify the context and the changes will affect agent execution:

      Notification Events: Events without setters are read-only:

      指定者:
      onEvent 在接口中 Hook
      类型参数:
      T - The concrete event type
      参数:
      event - The hook event
      返回:
      Mono containing the potentially modified event
    • stop

      public void stop()
      Stop the audio player and clean up resources.
    • builder

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