Commit 36619ae0 authored by ali's avatar ali

feat:删除多余文件

parent fec7d389
var context;
var source;
var processor;
var streamLocal;
var webSocket;
var inputArea;
var bufferSize = 8192;
var sampleRate = 8000;
var wsURL = 'ws://10.90.120.27:2700';
var initComplete = false;
;(function () {
document.addEventListener('DOMContentLoaded', (event) => {
inputArea = document.getElementById('q');
const listenButton = document.getElementById('listenWithScript');
const stopListeningButton = document.getElementById('stopListeningWithScript');
listenButton.addEventListener('mousedown', function () {
listenButton.disabled = true;
initWS();
navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: true,
noiseSuppression: true,
channelCount: 1,
sampleRate
}, video: false
}).then(handleSuccess);
listenButton.style.color = 'green';
initComplete = true;
});
stopListeningButton.addEventListener('mouseup', function () {
if (initComplete === true) {
webSocket.send('{"eof" : 1}');
webSocket.close();
source.disconnect(processor);
processor.disconnect(context.destination);
if (streamLocal.active) {
streamLocal.getTracks()[0].stop();
}
listenButton.style.color = 'black';
listenButton.disabled = false;
initComplete = false;
inputArea.innerText = ""
}
});
});
}())
var handleSuccess = function (stream) {
streamLocal = stream;
context = new AudioContext({sampleRate: sampleRate});
source = context.createMediaStreamSource(stream);
processor = context.createScriptProcessor();
source.connect(processor);
processor.connect(context.destination);
processor.onaudioprocess = function (audioDataChunk) {
console.log(audioDataChunk.inputBuffer);
sendAudio(audioDataChunk);
};
};
function sendAudio(audioDataChunk) {
if (webSocket.readyState === WebSocket.OPEN) {
// convert to 16-bit payload
const inputData = audioDataChunk.inputBuffer.getChannelData(0) || new Float32Array(bufferSize);
const targetBuffer = new Int16Array(inputData.length);
for (let index = inputData.length; index > 0; index--) {
targetBuffer[index] = 32767 * Math.min(1, inputData[index]);
}
webSocket.send(targetBuffer.buffer);
}
}
function initWS() {
webSocket = new WebSocket(wsURL);
webSocket.binaryType = "arraybuffer";
webSocket.onopen = function (event) {
console.log('New connection established');
};
webSocket.onerror = function (event) {
console.error(event.data);
};
webSocket.onmessage = function (event) {
if (event.data) {
let parsed = JSON.parse(event.data);
if (parsed.partial && parsed.partial !== 'the') inputArea.innerText = parsed.partial+'|';
if (parsed.result) console.log(parsed.result);
if (parsed.text) inputArea.innerText = parsed.text;
}
};
}
......@@ -15,7 +15,7 @@ const router = useRouter()
const route = useRoute()
const { settings, video: useVideo } = useStore()
let sampleRate = 48000
const bufferSize = 8192;
const bufferSize = 8192
const iconMicrophone = new URL('/images/microphone-input.svg', import.meta.url).href
const recordVolume = ref(0)
......@@ -169,21 +169,22 @@ async function startVoskWsAudioInput() {
initVoskWS()
sampleRate = 8000
const mediaStream = await navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: true,
noiseSuppression: true,
channelCount: 1,
sampleRate
}, video: false
});
const audioContext = new AudioContext({sampleRate});
const source = audioContext.createMediaStreamSource(mediaStream);
const processor = audioContext.createScriptProcessor();
source.connect(processor);
processor.connect(audioContext.destination);
processor.onaudioprocess = (audioDataChunk) => postAudio(audioDataChunk);
audio: {
echoCancellation: true,
noiseSuppression: true,
channelCount: 1,
sampleRate
},
video: false
})
const audioContext = new AudioContext({ sampleRate })
const source = audioContext.createMediaStreamSource(mediaStream)
const processor = audioContext.createScriptProcessor()
source.connect(processor)
processor.connect(audioContext.destination)
processor.onaudioprocess = (audioDataChunk) => postAudio(audioDataChunk)
await analyzeMicrophoneVolume(mediaStream, (val) => {
recordVolume.value = val
......@@ -196,42 +197,42 @@ async function startVoskWsAudioInput() {
}
function postAudio(audioDataChunk) {
if (!inputContext.voskWs) return;
if (inputContext.voskWs.readyState === WebSocket.OPEN) {
const inputData = audioDataChunk.inputBuffer.getChannelData(0) || new Float32Array(bufferSize);
const targetBuffer = new Int16Array(inputData.length);
for (let index = inputData.length; index > 0; index--) {
targetBuffer[index] = 32767 * Math.min(1, inputData[index]);
}
inputContext.voskWs.send(targetBuffer.buffer);
if (!inputContext.voskWs) return
if (inputContext.voskWs.readyState === WebSocket.OPEN) {
const inputData = audioDataChunk.inputBuffer.getChannelData(0) || new Float32Array(bufferSize)
const targetBuffer = new Int16Array(inputData.length)
for (let index = inputData.length; index > 0; index--) {
targetBuffer[index] = 32767 * Math.min(1, inputData[index])
}
inputContext.voskWs.send(targetBuffer.buffer)
}
}
function initVoskWS() {
return new Promise((resolve, reject) => {
inputContext.voskWs = new WebSocket(settings.voskWsLUrl);
inputContext.voskWs.binaryType = "arraybuffer";
inputContext.asrPartial = '';
inputContext.voskWs = new WebSocket(settings.voskWsLUrl)
inputContext.voskWs.binaryType = 'arraybuffer'
inputContext.asrPartial = ''
inputContext.voskWs.onopen = function (event) {
resolve(inputContext.voskWs);
};
resolve(inputContext.voskWs)
}
inputContext.voskWs.onerror = function (event) {
reject(new Error(JSON.stringify(event)))
};
}
inputContext.voskWs.onmessage = function (event) {
if (!event.data) return
const parsed = JSON.parse(event.data);
if (parsed.partial && parsed.partial !== 'the') inputContext.asrPartial = parsed.partial+'|';
const parsed = JSON.parse(event.data)
if (parsed.partial && parsed.partial !== 'the') inputContext.asrPartial = parsed.partial + '|'
// if (parsed.result) console.log(parsed.result);
if (parsed.text) {
inputContext.asrPartial = parsed.text;
onAsr(inputContext.asrPartial);
};
};
inputContext.asrPartial = parsed.text
onAsr(inputContext.asrPartial)
}
}
})
}
......@@ -243,14 +244,14 @@ function endAudioInput() {
inputContext.scriptProcessorNode && (inputContext.scriptProcessorNode.onaudioprocess = null)
inputContext.model?.terminate()
if (inputContext.voskWs) {
inputContext.voskWs.send('{"eof" : 1}');
inputContext.voskWs.close();
inputContext.voskWs.send('{"eof" : 1}')
inputContext.voskWs.close()
}
}
function setVideoUrl(url: string) {
const videoEle = videoElement.value as HTMLVideoElement
if (!videoEle) return;
if (!videoEle) return
videoEle.src = url
videoEle.load()
......@@ -268,10 +269,10 @@ async function onAsr(question: string) {
console.log(question + ' : ' + q)
if (q.includes(question)) {
const videoEle = videoElement.value as HTMLVideoElement
videoEle && (videoEle.loop = false);
videoEle && (videoEle.muted = false);
setVideoUrl(url);
return;
videoEle && (videoEle.loop = false)
videoEle && (videoEle.muted = false)
setVideoUrl(url)
return
}
}
......@@ -322,7 +323,6 @@ async function onAsr(question: string) {
console.log('----------------> Asr:', question)
ws.send(JSON.stringify({ prompt: question, historys_list: [] }))
}
function initLLMSocket(): Promise<WebSocket> {
......@@ -378,9 +378,9 @@ async function runAudioPlay() {
audio.onended = () => {
isPlayRunning = false
const videoEle = videoElement.value as HTMLVideoElement
videoEle && (videoEle.loop = true);
videoEle && (videoEle.muted = true);
setVideoUrl(new URL('/libai/10.mp4', import.meta.url).href);
videoEle && (videoEle.loop = true)
videoEle && (videoEle.muted = true)
setVideoUrl(new URL('/libai/10.mp4', import.meta.url).href)
runAudioPlay()
}
await audio.play()
......
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