feat: update dependencies and add video-tools package
- Added "@kevisual/video-tools" dependency to server/package.json. - Updated VITE_API_URL in astro.config.mjs to point to new local server. - Modified proxy settings in astro.config.mjs to include WebSocket support. - Updated various dependencies in web/package.json for improved functionality and security. - Refactored MuseApp component to use new resizable panel components. - Implemented real-time text display and copy functionality in VadVoice component. - Created a new Relatime class for handling WebSocket connections and real-time updates. - Added new board and muse pages to render the Record component.
This commit is contained in:
65
web/src/apps/muse/voice/store/relatime.ts
Normal file
65
web/src/apps/muse/voice/store/relatime.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { WSServer } from '@kevisual/video-tools/src/asr/ws.ts'
|
||||
import { useVoiceStore } from './voiceStore'
|
||||
|
||||
export class Relatime {
|
||||
asr: WSServer
|
||||
ready = false
|
||||
timeoutHandle: NodeJS.Timeout | null = null
|
||||
constructor() {
|
||||
// const url = new URL('/ws/asr', "http://localhost:51015")
|
||||
const url = new URL('/ws/asr', window.location.origin)
|
||||
url.searchParams.set('id', 'muse-voice-relatime')
|
||||
const ws = new WSServer({
|
||||
url: url.toString(),
|
||||
onConnect: () => {
|
||||
console.log('WebSocket connected');
|
||||
ws.emitter.on("message", (data) => {
|
||||
// console.log("Received message:", data.data);
|
||||
const json = JSON.parse(data.data);
|
||||
console.log('json', json);
|
||||
if (json && json.type === 'connected') {
|
||||
ws.ws.send(JSON.stringify({ type: 'init' }));
|
||||
}
|
||||
if (json && json.type === 'asr' && json.code === 200) {
|
||||
ws.emitter.emit('asr');
|
||||
}
|
||||
if (json && json.type === 'partial' || json.type === 'result') {
|
||||
const text = json.text || '';
|
||||
const isPartial = json.type === 'partial';
|
||||
const isResult = json.type === 'result';
|
||||
if (isPartial) {
|
||||
// 部分结果
|
||||
useVoiceStore.getState().setRelatimeParialText(text);
|
||||
} else {
|
||||
// 最终结果
|
||||
useVoiceStore.getState().setRelatimeFinalText(text);
|
||||
}
|
||||
}
|
||||
});
|
||||
ws.emitter.once('asr', async () => {
|
||||
console.log('ASR ready');
|
||||
this.ready = true;
|
||||
});
|
||||
}
|
||||
})
|
||||
this.asr = ws
|
||||
}
|
||||
send(data: Buffer) {
|
||||
if (!this.ready) return;
|
||||
const voice = data.toString('base64');
|
||||
this.asr.ws.send(JSON.stringify({ voice }));
|
||||
}
|
||||
sendBase64(data: string) {
|
||||
if (!this.ready) return;
|
||||
this.asr.ws.send(JSON.stringify({ voice: data, format: 'float32' }));
|
||||
if (this.timeoutHandle) {
|
||||
clearTimeout(this.timeoutHandle);
|
||||
}
|
||||
this.timeoutHandle = setTimeout(() => {
|
||||
this.asr.sendBlankJson()
|
||||
this.timeoutHandle = null;
|
||||
}, 10000); // 5秒钟没有数据则发送空JSON保持连接
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user