@minto-ai/huoshan-tts
Version:
借助“火山引擎在线语音合成API”实现浏览器端“文本转语音
47 lines (35 loc) • 1.26 kB
text/typescript
import type { SerialTaskExecuteContext } from '../../handler'
import { SerialHandler } from '../../handler'
class ByteBuffer extends SerialHandler<Blob, Blob> {
private static MIN_BYTE_SIZE = 60 * 1024
private static MIN_LOAD_SIZE = 0.1 * 1024
private lastRemainingByte: Blob = new Blob([])
public execute(
context: SerialTaskExecuteContext<Blob, Blob>,
): void {
if (context.isLastExecute) {
const originalByte = new Blob([this.lastRemainingByte])
if (originalByte.size > ByteBuffer.MIN_LOAD_SIZE) {
this.forwardToHandler(originalByte)
}
this.lastRemainingByte = new Blob([])
this.taskCompletedCallback()
}
else {
const originalByte = new Blob([this.lastRemainingByte, context.taskItem.original!])
if (originalByte.size >= ByteBuffer.MIN_BYTE_SIZE) {
this.forwardToHandler(originalByte)
this.lastRemainingByte = new Blob([])
}
else {
this.lastRemainingByte = originalByte
}
this.taskCompletedCallback()
}
}
protected onFinish(): void {
this.executeController?.$bus.emit('_byteBufferFinish')
this.lastRemainingByte = new Blob([])
}
}
export default ByteBuffer