@minto-ai/xunfei-tts
Version:
借助“讯飞在线语音合成API”实现浏览器端“文本转语音
35 lines (28 loc) • 1 kB
text/typescript
import type { SerialTaskExecuteContext } from '../handler'
import type { PrivateCustomEventName } from '../types'
import TextStreamSlicer from '@minto-ai/text-stream-slicer'
import { SerialHandler } from '../handler'
import { createEventBus } from '../utils'
const $bus = createEventBus<PrivateCustomEventName>()
class TextSplit extends SerialHandler<string, string> {
private textStreamSlicer = new TextStreamSlicer()
public execute(
context: SerialTaskExecuteContext<string, string>,
): void {
if (context.isLastExecute) {
this.textStreamSlicer.processText('', true).forEach((paragraph) => {
this.forwardToHandler(paragraph)
})
}
else {
this.textStreamSlicer.processText(context.taskItem.original!).forEach((paragraph) => {
this.forwardToHandler(paragraph)
})
}
this.taskCompletedCallback()
}
protected onFinish(): void {
$bus.emit('_textSplitFinish')
}
}
export default TextSplit