xunfei-tts
Version:
借助“讯飞在线语音合成API”实现浏览器端“文本转语音
24 lines (21 loc) • 631 B
text/typescript
import { Base64 } from 'js-base64'
function encodeText(text: string, encoding: string): string {
if (encoding === 'unicode') {
const buf = new ArrayBuffer(text.length * 4)
const bufView = new Uint16Array(buf)
for (let i = 0, strlen = text.length; i < strlen; i++) {
bufView[i] = text.charCodeAt(i)
}
let binary = ''
const bytes = new Uint8Array(buf)
const len = bytes.byteLength
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i])
}
return window.btoa(binary)
}
else {
return Base64.encode(text)
}
}
export { encodeText }