类 TTSHook
java.lang.Object
io.agentscope.core.hook.TTSHook
- 所有已实现的接口:
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));
-
嵌套类概要
嵌套类 -
方法概要
修饰符和类型方法说明static TTSHook.Builderbuilder()Creates a new builder for TTSHook.reactor.core.publisher.Flux<AudioBlock> Gets the reactive audio stream.<T extends HookEvent>
reactor.core.publisher.Mono<T> onEvent(T event) Handle a hook event.voidstop()Stop the audio player and clean up resources.
-
方法详细资料
-
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
从接口复制的说明:HookHandle 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:
PreReasoningEvent- Modify messages before LLM reasoningPostReasoningEvent- Modify reasoning resultsPreActingEvent- Modify tool parameters before executionPostActingEvent- Modify tool resultsPreCallEvent- Modify messages before agent startsPostCallEvent- Modify final agent response
Notification Events: Events without setters are read-only:
ReasoningChunkEvent- Streaming reasoning chunksActingChunkEvent- Streaming tool execution chunksErrorEvent- Errors during execution
-
stop
public void stop()Stop the audio player and clean up resources. -
builder
Creates a new builder for TTSHook.- 返回:
- a new Builder instance
-