From 0332f6941dff7d6b48ddc0271c271a6645d2ed26 Mon Sep 17 00:00:00 2001 From: shengchanzhe <179998674@qq.com> Date: Mon, 16 Oct 2023 16:51:50 +0800 Subject: [PATCH] gengx --- index.js | 40 +++++++++++++++---- src/typings/chat.d.ts | 1 + src/views/chat/components/Message/index.vue | 5 ++- src/views/chat/index.vue | 44 +++++++++++++++++---- 4 files changed, 74 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index acdf285..d3448a3 100644 --- a/index.js +++ b/index.js @@ -58,6 +58,8 @@ function toBase64(buffer) { } function countdown() { + if (countdownInterval) + clearInterval(countdownInterval) let seconds = 60 btnControl.innerText = `录音中(${seconds}s)` countdownInterval = setInterval(() => { @@ -81,7 +83,7 @@ function changeBtnStatus(status) { resultTextTemp = '' } else if (status === 'OPEN') { - countdown() + // countdown() } else if (status === 'CLOSING') { btnControl.innerText = '关闭连接中' @@ -176,6 +178,7 @@ function connectWebSocket() { changeBtnStatus('CLOSED') } iatWS.onclose = (e) => { + console.log('录音关闭', e) recorder.stop() changeBtnStatus('CLOSED') } @@ -186,7 +189,8 @@ recorder.onFrameRecorded = ({ isLastFrame, frameBuffer }) => { iatWS.send( JSON.stringify({ data: { - status: isLastFrame ? 2 : 1, + // status: isLastFrame ? 2 : 1, + status: 1, format: 'audio/L16;rate=16000', encoding: 'raw', audio: toBase64(frameBuffer), @@ -202,19 +206,41 @@ recorder.onStop = () => { } function RecordXunfei() { if (btnStatus === 'UNDEFINED' || btnStatus === 'CLOSED') { - console.log('开始录音') + console.log('准备开始录音') connectWebSocket() } else if (btnStatus === 'CONNECTING' || btnStatus === 'OPEN') { - console.log('结束录音') + console.log('准备结束录音') + iatWS.close() // 结束录音 recorder.stop() } } +function getRecordStatus() { + return btnStatus +} + +function getIAT() { + return iatWS +} + // 重新录制 function reRecordXunfei() { - console.log('重新录制') - - connectWebSocket() + if (iatWS && iatWS.readyState !== WebSocket.CLOSED) { + iatWS.onclose = () => { + console.log('重新录制') + connectWebSocket() + } + iatWS.close() + } + else { + console.log('重新录制') + connectWebSocket() + } +} + +function stopRecord() { + iatWS.close() + recorder.stop() } diff --git a/src/typings/chat.d.ts b/src/typings/chat.d.ts index 26919fa..7a27473 100644 --- a/src/typings/chat.d.ts +++ b/src/typings/chat.d.ts @@ -4,6 +4,7 @@ declare namespace Chat { dateTime: string text: string tts?: string + role?: string inversion?: boolean error?: boolean loading?: boolean diff --git a/src/views/chat/components/Message/index.vue b/src/views/chat/components/Message/index.vue index db0e4c2..0bca241 100644 --- a/src/views/chat/components/Message/index.vue +++ b/src/views/chat/components/Message/index.vue @@ -166,7 +166,7 @@ const watchTTS = watch(() => props.tts, async (n: any, o: any) => { const stopPlay = () => { console.log('停止播放音频') pauseIndex = -1 - playStatus.value = 'end' + playStatus.value = 'pause' emit('changeNowStatus', 'input') for (let i = 0; i < audioElements.length; i++) audioElements[i].pause() @@ -187,6 +187,7 @@ let sok = false async function radioPlay() { // return console.log('播放', props.tts[0]) audioElements.length = 0 + playing = 0 emit('changeNowStatus', 'loding') const socket = new WebSocket('wss://chat.lihaink.cn/zhanti/tts') sok = true @@ -295,6 +296,8 @@ const pauseAudio = () => { audioElements[i].pause() } const noPauseAudio = () => { + if (pauseIndex === -1) + return null playStatus.value = 'playing' emit('changeNowStatus', 'playing') audioElements[pauseIndex]?.play() diff --git a/src/views/chat/index.vue b/src/views/chat/index.vue index 71b45dc..5c1bf35 100644 --- a/src/views/chat/index.vue +++ b/src/views/chat/index.vue @@ -100,7 +100,10 @@ const startOrder = () => { const endOrder = () => { if (recordFalg.value == 1) { - RecordXunfei() + // RecordXunfei() + const iatWS = getIAT() + if (iatWS!.readyState === WebSocket.OPEN) + iatWS.close() recordFalg.value = 0 nowStatus.value = 'loding' handleSubmit() @@ -112,9 +115,10 @@ const endOrder = () => { const reOrder = () => { if (recordFalg.value == 1) { - // RecordXunfei() // 结束 - recordFalg.value = 0 + console.log('重新录制') + // RecordXunfei() nowStatus.value = 'run' + recordFalg.value = 0 prompt.value = '' if (messageRef.value?.length > 0) messageRef.value[messageRef.value.length - 1].stopPlay() @@ -127,18 +131,39 @@ const reOrder = () => { // nowStatus.value = 'record' // }, 500) // }, 2500) - reRecordXunfei() - prompt.value = '' - recordFalg.value = 1 + // reRecordXunfei() + stopRecord() setTimeout(() => { - nowStatus.value = 'record' + recordFalg.value = 1 + RecordXunfei() + setTimeout(() => { + prompt.value = '' + nowStatus.value = 'record' + }, 500) }, 1600) + // const btnStatus = getRecordStatus() + // if (btnStatus === 'UNDEFINED' || btnStatus === 'CLOSED') { + + // const timer = setInterval(() => { + // const btn = getRecordStatus() + + // }, 500) + // } } else { console.log('无效指令') } } +const getRErecord = () => { + const btnStatus = getRecordStatus() + if (btnStatus === 'UNDEFINED' || btnStatus === 'CLOSED') { + console.log('关闭录音') + RecordXunfei() + return true + } +} + const cleatOrder = () => { if (recordFalg.value == 1) { RecordXunfei() @@ -312,6 +337,7 @@ async function onConversation() { dateTime: new Date().toLocaleString(), text: message, tts: '', + role: 'user', inversion: true, error: false, conversationOptions: null, @@ -332,9 +358,10 @@ async function onConversation() { let infoList = JSON.parse(JSON.stringify(dataSources.value)) infoList = infoList.slice(-10) + infoList = infoList.map((item: any, index: any) => { return { - role: index % 2 == 0 ? 'user' : 'assistant', + role: item.role, content: item.text, } }) @@ -343,6 +370,7 @@ async function onConversation() { dateTime: new Date().toLocaleString(), text: '思考中', tts: '', + role: 'assistant', loading: true, inversion: false, error: false,