UNPKG

@minto-ai/huoshan-tts

Version:

借助“火山引擎在线语音合成API”实现浏览器端“文本转语音

77 lines (62 loc) 2.21 kB
import type { SerialTaskExecuteContext } from '../../handler' import type { BusinessParams, SystemConfig } from '../../types' import { createWebSocket, objectToQueryString } from '@minto-ai/tools' import { SerialHandler } from '../../handler' class TtsRequest extends SerialHandler<string, string> { private webSocketInstance: WebSocket | null = null private businessParams!: BusinessParams private systemConfig!: SystemConfig public initProperty(systemConfig: SystemConfig, businessParams: BusinessParams): void { this.businessParams = { ...businessParams } this.systemConfig = { ...systemConfig, } } public onActive(): void { this.webSocketInstance = createWebSocket(this.generateRequestUrl()) this.webSocketInstance.addEventListener('message', (response: any) => { if (Object.prototype.toString.call(response.data) === '[object Blob]') { this.forwardToHandler(response.data) } else { const data = JSON.parse(response.data) if (data.event === 'sentence_end') { this.taskCompletedCallback() } } }) this.webSocketInstance.addEventListener('open', () => { this.taskCompletedCallback() }) } public executePreCheck(): boolean { return this.webSocketInstance?.readyState === WebSocket.OPEN } public execute(context: SerialTaskExecuteContext<string, Blob>): void { if (context.isLastExecute) { this.taskCompletedCallback() return } if (this.webSocketInstance) { this.webSocketInstance.send(JSON.stringify(this.generateRequestParams(context.taskItem.original!))) } } private generateRequestParams(text: string): { event: 'text' text: string } { return { event: 'text', text, } } private generateRequestUrl(): string { return `${this.systemConfig.ttsRequestBaseUrl}?${objectToQueryString({ ...this.businessParams })}` } protected onFinish(): void { this.executeController?.$bus.emit('_ttsRequestFinish') this.webSocketInstance?.close() this.webSocketInstance = null } } export default TtsRequest