generated from tailored/router-template
add funasr demo
This commit is contained in:
42
src/asr/provider/funasr/test/get-text.ts
Normal file
42
src/asr/provider/funasr/test/get-text.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { VideoWS } from '../ws.ts';
|
||||
import net from 'net';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
const videoTestPath = path.join(process.cwd(), 'videos/asr_example.wav');
|
||||
const ws = new VideoWS({
|
||||
// url: 'wss://192.168.31.220:10095',
|
||||
url: 'wss://funasr.xiongxiao.me',
|
||||
isFile: true,
|
||||
onConnect: async () => {
|
||||
console.log('onConnect');
|
||||
const data = fs.readFileSync(videoTestPath);
|
||||
let sampleBuf = new Uint8Array(data);
|
||||
|
||||
var chunk_size = 960; // for asr chunk_size [5, 10, 5]
|
||||
let totalsend = 0;
|
||||
let len = 0;
|
||||
ws.start();
|
||||
while (sampleBuf.length >= chunk_size) {
|
||||
const sendBuf = sampleBuf.slice(0, chunk_size);
|
||||
totalsend = totalsend + sampleBuf.length;
|
||||
sampleBuf = sampleBuf.slice(chunk_size, sampleBuf.length);
|
||||
if (len === 100) {
|
||||
// ws.stop();
|
||||
// ws.start();
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
ws.send(sendBuf);
|
||||
len++;
|
||||
}
|
||||
ws.stop();
|
||||
console.log('len', len);
|
||||
},
|
||||
});
|
||||
|
||||
const server = net.createServer((socket) => {
|
||||
socket.on('data', (data) => {
|
||||
console.log('data', data);
|
||||
});
|
||||
});
|
||||
server.listen(10096);
|
||||
41
src/asr/provider/funasr/test/recorder.ts
Normal file
41
src/asr/provider/funasr/test/recorder.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { VideoWS } from '../ws.ts';
|
||||
import net from 'net';
|
||||
import { Recording } from '../../../../recorder/index.ts';
|
||||
import Stream from 'stream';
|
||||
|
||||
const recorder = new Recording();
|
||||
const writeStream = new Stream.Writable();
|
||||
const ws = new VideoWS({
|
||||
url: 'wss://192.168.31.220:10095',
|
||||
isFile: false,
|
||||
onConnect: async () => {
|
||||
console.log('onConnect');
|
||||
let chunks: Buffer = Buffer.alloc(0);
|
||||
var chunk_size = 960; // for asr chunk_size [5, 10, 5]
|
||||
let totalsend = 0;
|
||||
let len = 0;
|
||||
recorder.stream().on('data', (chunk) => {
|
||||
chunks = Buffer.concat([chunks, chunk]);
|
||||
if (chunks.length > chunk_size) {
|
||||
ws.send(chunks);
|
||||
totalsend += chunks.length;
|
||||
chunks = Buffer.alloc(0);
|
||||
}
|
||||
});
|
||||
ws.start();
|
||||
setTimeout(() => {
|
||||
ws.stop();
|
||||
setTimeout(() => {
|
||||
process.exit(0);
|
||||
}, 1000);
|
||||
console.log('len', len);
|
||||
}, 20000);
|
||||
},
|
||||
});
|
||||
|
||||
const server = net.createServer((socket) => {
|
||||
socket.on('data', (data) => {
|
||||
console.log('data', data);
|
||||
});
|
||||
});
|
||||
server.listen(10096);
|
||||
114
src/asr/provider/funasr/ws.ts
Normal file
114
src/asr/provider/funasr/ws.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import WebSocket from 'ws';
|
||||
|
||||
type VideoWSOptions = {
|
||||
url?: string;
|
||||
ws?: WebSocket;
|
||||
itn?: boolean;
|
||||
mode?: string;
|
||||
isFile?: boolean;
|
||||
onConnect?: () => void;
|
||||
};
|
||||
export const VideoWsMode = ['2pass', 'online', 'offline'];
|
||||
type VideoWsMode = (typeof VideoWsMode)[number];
|
||||
|
||||
export type VideoWsResult = {
|
||||
isFinal: boolean;
|
||||
mode: VideoWsMode;
|
||||
stamp_sents: { end: number; punc: string; start: number; text_seg: string; tsList: [][] }[];
|
||||
text: string;
|
||||
timestamp: string;
|
||||
wav_name: string;
|
||||
};
|
||||
|
||||
export class VideoWS {
|
||||
ws: WebSocket;
|
||||
itn?: boolean;
|
||||
mode?: VideoWsMode;
|
||||
isFile?: boolean;
|
||||
onConnect?: () => void;
|
||||
constructor(options?: VideoWSOptions) {
|
||||
this.ws =
|
||||
options?.ws ||
|
||||
new WebSocket(options.url, {
|
||||
rejectUnauthorized: false,
|
||||
});
|
||||
this.itn = options?.itn || false;
|
||||
this.mode = options?.mode || 'online';
|
||||
this.isFile = options?.isFile || false;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
async onOpen() {
|
||||
this.onConnect();
|
||||
}
|
||||
async start() {
|
||||
let isFileMode = this.isFile;
|
||||
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', //
|
||||
is_speaking: true,
|
||||
chunk_interval: 10,
|
||||
itn: this.itn,
|
||||
mode: this.mode || 'online',
|
||||
};
|
||||
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;
|
||||
}
|
||||
}
|
||||
this.ws.send(JSON.stringify(request));
|
||||
}
|
||||
async stop() {
|
||||
var chunk_size = new Array(5, 10, 5);
|
||||
var request = {
|
||||
chunk_size: chunk_size,
|
||||
wav_name: 'h5',
|
||||
is_speaking: false,
|
||||
chunk_interval: 10,
|
||||
mode: this.mode,
|
||||
};
|
||||
this.ws.send(JSON.stringify(request));
|
||||
}
|
||||
async send(data: any) {
|
||||
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(data);
|
||||
}
|
||||
}
|
||||
async onMessage(event: MessageEvent) {
|
||||
const data = event.data;
|
||||
try {
|
||||
const result = JSON.parse(data.toString());
|
||||
console.log('result', result);
|
||||
} catch (error) {
|
||||
console.log('error', error);
|
||||
}
|
||||
}
|
||||
async onError(event: Event) {
|
||||
console.log('onError', event);
|
||||
}
|
||||
async onClose(event: CloseEvent) {
|
||||
console.log('onClose', event);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user