This commit is contained in:
shengchanzhe 2023-10-16 16:51:50 +08:00
parent 4428fdd00f
commit 0332f6941d
4 changed files with 74 additions and 16 deletions

View File

@ -58,6 +58,8 @@ function toBase64(buffer) {
} }
function countdown() { function countdown() {
if (countdownInterval)
clearInterval(countdownInterval)
let seconds = 60 let seconds = 60
btnControl.innerText = `录音中(${seconds}s` btnControl.innerText = `录音中(${seconds}s`
countdownInterval = setInterval(() => { countdownInterval = setInterval(() => {
@ -81,7 +83,7 @@ function changeBtnStatus(status) {
resultTextTemp = '' resultTextTemp = ''
} }
else if (status === 'OPEN') { else if (status === 'OPEN') {
countdown() // countdown()
} }
else if (status === 'CLOSING') { else if (status === 'CLOSING') {
btnControl.innerText = '关闭连接中' btnControl.innerText = '关闭连接中'
@ -176,6 +178,7 @@ function connectWebSocket() {
changeBtnStatus('CLOSED') changeBtnStatus('CLOSED')
} }
iatWS.onclose = (e) => { iatWS.onclose = (e) => {
console.log('录音关闭', e)
recorder.stop() recorder.stop()
changeBtnStatus('CLOSED') changeBtnStatus('CLOSED')
} }
@ -186,7 +189,8 @@ recorder.onFrameRecorded = ({ isLastFrame, frameBuffer }) => {
iatWS.send( iatWS.send(
JSON.stringify({ JSON.stringify({
data: { data: {
status: isLastFrame ? 2 : 1, // status: isLastFrame ? 2 : 1,
status: 1,
format: 'audio/L16;rate=16000', format: 'audio/L16;rate=16000',
encoding: 'raw', encoding: 'raw',
audio: toBase64(frameBuffer), audio: toBase64(frameBuffer),
@ -202,19 +206,41 @@ recorder.onStop = () => {
} }
function RecordXunfei() { function RecordXunfei() {
if (btnStatus === 'UNDEFINED' || btnStatus === 'CLOSED') { if (btnStatus === 'UNDEFINED' || btnStatus === 'CLOSED') {
console.log('开始录音') console.log('准备开始录音')
connectWebSocket() connectWebSocket()
} }
else if (btnStatus === 'CONNECTING' || btnStatus === 'OPEN') { else if (btnStatus === 'CONNECTING' || btnStatus === 'OPEN') {
console.log('结束录音') console.log('准备结束录音')
iatWS.close()
// 结束录音 // 结束录音
recorder.stop() recorder.stop()
} }
} }
function getRecordStatus() {
return btnStatus
}
function getIAT() {
return iatWS
}
// 重新录制 // 重新录制
function reRecordXunfei() { function reRecordXunfei() {
console.log('重新录制') if (iatWS && iatWS.readyState !== WebSocket.CLOSED) {
iatWS.onclose = () => {
connectWebSocket() console.log('重新录制')
connectWebSocket()
}
iatWS.close()
}
else {
console.log('重新录制')
connectWebSocket()
}
}
function stopRecord() {
iatWS.close()
recorder.stop()
} }

View File

@ -4,6 +4,7 @@ declare namespace Chat {
dateTime: string dateTime: string
text: string text: string
tts?: string tts?: string
role?: string
inversion?: boolean inversion?: boolean
error?: boolean error?: boolean
loading?: boolean loading?: boolean

View File

@ -166,7 +166,7 @@ const watchTTS = watch(() => props.tts, async (n: any, o: any) => {
const stopPlay = () => { const stopPlay = () => {
console.log('停止播放音频') console.log('停止播放音频')
pauseIndex = -1 pauseIndex = -1
playStatus.value = 'end' playStatus.value = 'pause'
emit('changeNowStatus', 'input') emit('changeNowStatus', 'input')
for (let i = 0; i < audioElements.length; i++) for (let i = 0; i < audioElements.length; i++)
audioElements[i].pause() audioElements[i].pause()
@ -187,6 +187,7 @@ let sok = false
async function radioPlay() { async function radioPlay() {
// return console.log('', props.tts[0]) // return console.log('', props.tts[0])
audioElements.length = 0 audioElements.length = 0
playing = 0
emit('changeNowStatus', 'loding') emit('changeNowStatus', 'loding')
const socket = new WebSocket('wss://chat.lihaink.cn/zhanti/tts') const socket = new WebSocket('wss://chat.lihaink.cn/zhanti/tts')
sok = true sok = true
@ -295,6 +296,8 @@ const pauseAudio = () => {
audioElements[i].pause() audioElements[i].pause()
} }
const noPauseAudio = () => { const noPauseAudio = () => {
if (pauseIndex === -1)
return null
playStatus.value = 'playing' playStatus.value = 'playing'
emit('changeNowStatus', 'playing') emit('changeNowStatus', 'playing')
audioElements[pauseIndex]?.play() audioElements[pauseIndex]?.play()

View File

@ -100,7 +100,10 @@ const startOrder = () => {
const endOrder = () => { const endOrder = () => {
if (recordFalg.value == 1) { if (recordFalg.value == 1) {
RecordXunfei() // RecordXunfei()
const iatWS = getIAT()
if (iatWS!.readyState === WebSocket.OPEN)
iatWS.close()
recordFalg.value = 0 recordFalg.value = 0
nowStatus.value = 'loding' nowStatus.value = 'loding'
handleSubmit() handleSubmit()
@ -112,9 +115,10 @@ const endOrder = () => {
const reOrder = () => { const reOrder = () => {
if (recordFalg.value == 1) { if (recordFalg.value == 1) {
// RecordXunfei() // console.log('重新录制')
recordFalg.value = 0 // RecordXunfei()
nowStatus.value = 'run' nowStatus.value = 'run'
recordFalg.value = 0
prompt.value = '' prompt.value = ''
if (messageRef.value?.length > 0) if (messageRef.value?.length > 0)
messageRef.value[messageRef.value.length - 1].stopPlay() messageRef.value[messageRef.value.length - 1].stopPlay()
@ -127,18 +131,39 @@ const reOrder = () => {
// nowStatus.value = 'record' // nowStatus.value = 'record'
// }, 500) // }, 500)
// }, 2500) // }, 2500)
reRecordXunfei() // reRecordXunfei()
prompt.value = '' stopRecord()
recordFalg.value = 1
setTimeout(() => { setTimeout(() => {
nowStatus.value = 'record' recordFalg.value = 1
RecordXunfei()
setTimeout(() => {
prompt.value = ''
nowStatus.value = 'record'
}, 500)
}, 1600) }, 1600)
// const btnStatus = getRecordStatus()
// if (btnStatus === 'UNDEFINED' || btnStatus === 'CLOSED') {
// const timer = setInterval(() => {
// const btn = getRecordStatus()
// }, 500)
// }
} }
else { else {
console.log('无效指令') console.log('无效指令')
} }
} }
const getRErecord = () => {
const btnStatus = getRecordStatus()
if (btnStatus === 'UNDEFINED' || btnStatus === 'CLOSED') {
console.log('关闭录音')
RecordXunfei()
return true
}
}
const cleatOrder = () => { const cleatOrder = () => {
if (recordFalg.value == 1) { if (recordFalg.value == 1) {
RecordXunfei() RecordXunfei()
@ -312,6 +337,7 @@ async function onConversation() {
dateTime: new Date().toLocaleString(), dateTime: new Date().toLocaleString(),
text: message, text: message,
tts: '', tts: '',
role: 'user',
inversion: true, inversion: true,
error: false, error: false,
conversationOptions: null, conversationOptions: null,
@ -332,9 +358,10 @@ async function onConversation() {
let infoList = JSON.parse(JSON.stringify(dataSources.value)) let infoList = JSON.parse(JSON.stringify(dataSources.value))
infoList = infoList.slice(-10) infoList = infoList.slice(-10)
infoList = infoList.map((item: any, index: any) => { infoList = infoList.map((item: any, index: any) => {
return { return {
role: index % 2 == 0 ? 'user' : 'assistant', role: item.role,
content: item.text, content: item.text,
} }
}) })
@ -343,6 +370,7 @@ async function onConversation() {
dateTime: new Date().toLocaleString(), dateTime: new Date().toLocaleString(),
text: '思考中', text: '思考中',
tts: '', tts: '',
role: 'assistant',
loading: true, loading: true,
inversion: false, inversion: false,
error: false, error: false,