fix funasr

This commit is contained in:
2025-05-19 01:01:38 +08:00
parent 8e04962cc1
commit a1df51f56b
7 changed files with 176 additions and 129 deletions

View File

@@ -1,5 +1,7 @@
// import WebSocket from 'ws';
import { initWs } from '../../../ws-adapter/index.ts';
import { logger } from '@/logger/index.ts';
import { WSServer } from '../../provider/ws-server.ts';
export type VideoWSOptions = {
url?: string;
@@ -8,10 +10,22 @@ export type VideoWSOptions = {
mode?: VideoWsMode;
isFile?: boolean;
onConnect?: () => void;
wav_format?: string;
};
export const videoWsMode = ['2pass', 'online', 'offline'] as const;
type VideoWsMode = (typeof videoWsMode)[number];
type OpenRequest = {
chunk_size: number[];
wav_name: string;
is_speaking: boolean;
chunk_interval: number;
// 逆文本标准化(ITN):
itn: boolean;
mode: VideoWsMode;
wav_format?: string;
audio_fs?: number;
hotwords?: string;
};
export type VideoWsResult = {
isFinal: boolean;
mode: VideoWsMode;
@@ -21,48 +35,21 @@ export type VideoWsResult = {
wav_name: string;
};
export class VideoWS {
ws: WebSocket;
export class VideoWS extends WSServer {
itn?: boolean;
mode?: VideoWsMode;
isFile?: boolean;
onConnect?: () => void;
wav_format?: string;
constructor(options?: VideoWSOptions) {
super({ url: options?.url, ws: options?.ws, onConnect: options?.onConnect });
this.itn = options?.itn || false;
this.itn = options?.itn || false;
this.mode = options?.mode || 'online';
this.isFile = options?.isFile || false;
this.initWs(options);
}
async initWs(options: VideoWSOptions) {
if (options?.ws) {
this.ws = options.ws;
} else {
this.ws = await initWs(options.url);
}
this.onConnect = options?.onConnect || (() => {});
this.ws.onopen = this.onOpen.bind(this);
this.ws.onmessage = this.onMessage.bind(this);
this.ws.onerror = this.onError.bind(this);
this.ws.onclose = this.onClose.bind(this);
this.wav_format = options?.wav_format;
}
async onOpen() {
this.onConnect();
}
async start() {
let isFileMode = this.isFile;
async start(opts?: Partial<OpenRequest>) {
const chunk_size = new Array(5, 10, 5);
type OpenRequest = {
chunk_size: number[];
wav_name: string;
is_speaking: boolean;
chunk_interval: number;
itn: boolean;
mode: VideoWsMode;
wav_format?: string;
audio_fs?: number;
hotwords?: string;
};
const request: OpenRequest = {
chunk_size: chunk_size,
wav_name: 'h5', //
@@ -70,17 +57,16 @@ export class VideoWS {
chunk_interval: 10,
itn: this.itn,
mode: this.mode || 'online',
...opts,
};
console.log('request', request);
if (isFileMode) {
const file_ext = 'wav';
const file_sample_rate = 16000;
request.wav_format = file_ext;
if (file_ext == 'wav') {
request.wav_format = 'PCM';
request.audio_fs = file_sample_rate;
}
const file_sample_rate = 16000;
request.wav_format = request.wav_format || this.wav_format || 'wav';
if ('wav' == request.wav_format) {
request.wav_format = 'PCM';
request.audio_fs = file_sample_rate;
}
console.log('request', request);
this.ws.send(JSON.stringify(request));
}
async stop() {
@@ -99,7 +85,28 @@ export class VideoWS {
this.ws.send(data);
}
}
async sendBuffer(data: Buffer, opts?: { isFile?: boolean; wav_format?: string }) {
const { wav_format = 'wav' } = opts || {};
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
let sampleBuf = new Uint8Array(data);
const ws = this;
var chunk_size = 960; // for asr chunk_size [5, 10, 5]
let totalsend = 0;
let len = 0;
ws.start({ wav_format });
while (sampleBuf.length >= chunk_size) {
const sendBuf = sampleBuf.slice(0, chunk_size);
totalsend = totalsend + sampleBuf.length;
sampleBuf = sampleBuf.slice(chunk_size, sampleBuf.length);
await new Promise((resolve) => setTimeout(resolve, 10));
ws.send(sendBuf);
len++;
}
ws.stop();
}
}
async onMessage(event: MessageEvent) {
super.onMessage(event);
const data = event.data;
try {
const result = JSON.parse(data.toString());