diff --git a/examples/batch-send-files.ts b/examples/batch-send-files.ts new file mode 100644 index 0000000..1af4de9 --- /dev/null +++ b/examples/batch-send-files.ts @@ -0,0 +1,100 @@ +import { EventEmitter } from 'eventemitter3'; +import { VideoWS, VideoWsResult, sleep } from '../src/asr/provider/funasr/ws.ts'; +import fs from 'node:fs'; +import path from 'node:path'; +type BatchSendOptions = { + vws: VideoWS; + files: string[]; + matchText?: string; + emitter?: EventEmitter; +}; +export class BatchSendFiles { + files: string[]; + vws: VideoWS; + emitter: EventEmitter; + constructor({ vws, files, emitter }: BatchSendOptions) { + this.files = files; + this.vws = vws; + this.emitter = emitter || vws.emitter; + } + async init() { + const isConnected = await this.vws.isConnected(); + if (!isConnected) { + console.error('链接失败:', isConnected); + } + this.send(); + } + waitOne() { + return new Promise((resolve) => { + this.vws.emitter.once('result', (data) => { + resolve(data); + }); + }); + } + async checkAudioFile(file: string) { + const stats = fs.statSync(file); + if (!stats.isFile()) { + throw new Error(`File not found: ${file}`); + } + const ext = path.extname(file).toLowerCase(); + const validExtensions = ['.wav', '.mp3', '.flac', '.ogg', '.aac']; + if (!validExtensions.includes(ext)) { + throw new Error(`Invalid file type: ${ext}. Supported types are: ${validExtensions.join(', ')}`); + } + const fileSize = stats.size; + if (fileSize === 0) { + throw new Error(`File is empty: ${file}`); + } + const maxSize = 100 * 1024 * 1024; // 100 MB + if (fileSize > maxSize) { + throw new Error(`File size exceeds limit: ${fileSize} bytes. Maximum allowed size is ${maxSize} bytes.`); + } + return { + file, + ext, + size: fileSize, + isValid: true, + }; + } + async send() { + const textList: { file: string; text: string }[] = []; + for (const file of this.files) { + let wav_format = 'wav'; + try { + const ck = await this.checkAudioFile(file); + if (ck.ext !== '.wav') { + wav_format = ck.ext.replace('.', ''); + } + } catch (error) { + console.error('Error checking file:', error); + continue; + } + const data = fs.readFileSync(file); + const wait = this.waitOne(); + await this.vws.sendBuffer(data, { wav_format }); + await sleep(1000); + console.log('File sent:', file); + const result: VideoWsResult = (await wait) as any; + console.log('Result:', result.text); + textList.push({ file, text: result.text }); + console.log('----------------------'); + } + this.emitter.emit('send-done', textList); + } +} +// const batchSend = new BatchSendFiles({ +// vws: ws, +// // files: [audioTestPath], +// files: [videoTestPath, audioTestPath], +// }); +// batchSend.init(); +// batchSend.emitter.on('send-done', (data) => { +// const matchText = '在一无所知中,梦里的一天结束了一个新的轮回,便会开始。'; +// const textList = data as { file: string; text: string }[]; +// for (const item of textList) { +// const getText = item.text || ''; +// const distance = natural.JaroWinklerDistance(getText, matchText); +// console.log(`File: ${item.file}, \nText: ${item.text}\nDistance: ${distance}`); +// } +// // console.log('Batch processing done:', data); +// }); diff --git a/src/asr/provider/funasr/ws.ts b/src/asr/provider/funasr/ws.ts index b25bd53..b460692 100644 --- a/src/asr/provider/funasr/ws.ts +++ b/src/asr/provider/funasr/ws.ts @@ -1,7 +1,7 @@ // import WebSocket from 'ws'; import { EventEmitter } from 'eventemitter3'; import { WSServer, WSSOptions } from '../../provider/ws-server.ts'; - +export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); export type VideoWSOptions = { url?: string; ws?: WebSocket; @@ -123,7 +123,7 @@ export class VideoWS extends WSServer { // console.log('result', result, typeof result); this.emitter.emit('result', result); } - console.log('onMessage-result', result); + // console.log('onMessage-result', result); } catch (error) { console.log('error', error); }