Commit 7456d071 authored by ali's avatar ali

feat: 照片数字人语音播放

parent 76e086cc
/* eslint-disable camelcase */
export async function audioAiTTS({
host,
text,
......@@ -35,14 +36,23 @@ export async function audioAiTTS({
return res.data
}
export async function localTTS({ url, text }: { url: string; text: string }) {
export async function localTTS({
url,
text,
audio_path
}: {
url: string
text: string
audio_path?: string
}) {
const resp = await fetch(url, {
headers: {
accept: 'application/json, text/plain, */*',
'content-type': 'application/json'
},
body: JSON.stringify({
text
text,
audio_path
}),
method: 'POST',
mode: 'cors'
......
......@@ -247,6 +247,8 @@ function endAudioInput() {
}
async function onAsr(question: string) {
console.log('---------------->question: ', question);
endAudioInput();
const ws = await initSocket()
inputContext.ws = ws
......@@ -264,6 +266,10 @@ async function onAsr(question: string) {
}
if (event === 'stream_end') {
answerArray.push(sliceAnswer)
runTTSTask(answerArray)
sliceAnswer = ''
answerArray.push(sliceAnswer)
sliceAnswer = ''
inputContext.ws?.close()
......@@ -301,7 +307,6 @@ function initSocket(): Promise<WebSocket> {
}
let isTTSRunning = false
const ttsAudios: HTMLAudioElement[] = []
async function runTTSTask(tasks: string[]) {
if (isTTSRunning) return
isTTSRunning = true
......@@ -310,11 +315,16 @@ async function runTTSTask(tasks: string[]) {
while (tasks.length) {
const task = tasks.shift()
if (!task) break
if (task.length < 1) continue;
console.time(task + ' TTS: ')
const res = await localTTS({ url: settings.ttsHost, text: task })
console.log('----------------> TTS:', res)
const res = await localTTS({ url: settings.ttsHost, text: task, audio_path: settings.userData })
console.log('----------------> TTS:', res[0].text)
console.timeEnd(task + ' TTS: ')
console.log('---------------->', ttsAudios)
const audio = new Audio(`file://${res[0].text}`);
audio.load();
ttsAudios.push(audio);
runAudioPlay();
}
} catch (error) {
console.error(error)
......@@ -323,6 +333,24 @@ async function runTTSTask(tasks: string[]) {
isTTSRunning = false
}
const ttsAudios: HTMLAudioElement[] = []
let isPlayRunning = false
async function runAudioPlay() {
if (isPlayRunning) return
isPlayRunning = true
const audio = ttsAudios.shift();
if (!audio) {
isPlayRunning = false;
return;
}
audio.onended = () => {
isPlayRunning = false;
runAudioPlay()
};
await audio.play();
}
// eslint-disable-next-line no-unused-vars
async function xfTTS(text: string) {
const tone = settings.source.find(({ sourceId }) => settings.selectSource === sourceId)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment