This commit is contained in:
weipengfei 2023-10-16 18:41:10 +08:00
commit b2fb292105
4 changed files with 74 additions and 16 deletions

View File

@ -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() {
if (iatWS && iatWS.readyState !== WebSocket.CLOSED) {
iatWS.onclose = () => {
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
text: string
tts?: string
role?: string
inversion?: boolean
error?: boolean
loading?: boolean

View File

@ -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()

View File

@ -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(() => {
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,