Commit 7456d071 authored by ali's avatar ali

feat: 照片数字人语音播放

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