From 80475771654580687b805c83008dff745564eafc Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sat, 23 Aug 2025 22:34:36 +0800 Subject: [PATCH] temp test --- src/asr/provider/funasr/test/get-text.ts | 29 ++++++++++--- src/asr/provider/funasr/test/recorder.ts | 52 +++++++++++++++--------- src/asr/provider/funasr/ws.ts | 15 ++++--- 3 files changed, 65 insertions(+), 31 deletions(-) diff --git a/src/asr/provider/funasr/test/get-text.ts b/src/asr/provider/funasr/test/get-text.ts index b7be4df..5f6dc8a 100644 --- a/src/asr/provider/funasr/test/get-text.ts +++ b/src/asr/provider/funasr/test/get-text.ts @@ -7,10 +7,12 @@ import fs from 'fs'; // const videoTestPath = path.join(process.cwd(), 'videos/asr_example2.wav'); // const videoTestPath = path.join(process.cwd(), 'videos/tts_mix.mp3'); const videoTestPath = path.join(process.cwd(), 'videos/my_speech_text.wav'); +const videoTestPath3 = path.join(process.cwd(), 'funasr_test.wav'); const name = 'output-1746007775571.mp3'; const videoTestPath2 = path.join(process.cwd(), 'build', name); const url = 'wss://funasr.xiongxiao.me'; +const url5 = 'https://1.15.101.247:10095'; // pro // const ws = new VideoWS({ // // url: 'wss://192.168.31.220:10095', // url: 'wss://funasr.xiongxiao.me', @@ -54,12 +56,27 @@ const url = 'wss://funasr.xiongxiao.me'; // server.listen(10096); const ws2 = new VideoWS({ - url: url, + url: url5, + mode: '2pass', onConnect: async () => { - const data = fs.readFileSync(videoTestPath); - await ws2.sendBuffer(data, { wav_format: 'mp3' }); - await new Promise((resolve) => setTimeout(resolve, 1000)); - const data2 = fs.readFileSync(videoTestPath2); - await ws2.sendBuffer(data2, { wav_format: 'mp3' }); + const data = fs.readFileSync(videoTestPath3); + // await ws2.sendBuffer(data, { wav_format: 'mp3' }); + // await new Promise((resolve) => setTimeout(resolve, 1000)); + // const data2 = fs.readFileSync(videoTestPath2); + // await ws2.sendBuffer(data2, { wav_format: 'mp3' }); + ws2.emitter.on('message', (event) => { + console.log('message', event.data); + }); + ws2.emitter.on('result', (result) => { + if (result.is_final) { + console.log('Final result:', result); + process.exit(0); + } + }); + await ws2.start(); + await ws2.sendBuffer(data, { online: true }); + setTimeout(() => { + ws2.stop(); + }, 4000); }, }); diff --git a/src/asr/provider/funasr/test/recorder.ts b/src/asr/provider/funasr/test/recorder.ts index 089064f..642aed2 100644 --- a/src/asr/provider/funasr/test/recorder.ts +++ b/src/asr/provider/funasr/test/recorder.ts @@ -1,48 +1,60 @@ import { VideoWS } from '../ws.ts'; +import path from 'node:path'; import net from 'net'; import { Recording } from '../../../../recorder/index.ts'; import Stream from 'stream'; +import fs from 'node:fs'; // 新增 + +const recorder = new Recording({ + sampleRate: 16000, + channels: 1, // + audioType: 'wav', + threshold: 0, + recorder: 'rec', + silence: '1.0', + endOnSilence: true, +}); +const writeFilePath = path.join(process.cwd(), 'funasr_test.wav'); +const fileStream = fs.createWriteStream(writeFilePath, { encoding: 'binary' }); -const recorder = new Recording(); -const writeStream = new Stream.Writable(); const url = 'wss://funasr.xiongxiao.me'; +const url3 = 'wss://pro.xiongxiao.me:10095'; +const url4 = 'wss://121.4.112.18:10095'; // aliyun +const url5 = 'https://1.15.101.247:10095'; // pro + const ws = new VideoWS({ - // url: 'wss://192.168.31.220:10095', - url: url, + url: url5, isFile: false, + // mode: 'online', mode: '2pass', wsOptions: { rejectUnauthorized: false, }, onConnect: async () => { console.log('onConnect'); + ws.start(); + recorder.start(); - 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); - // console.log('chunk', chunk.length); - - // totalsend += chunks.length; - // chunks = Buffer.alloc(0); - // } - ws.send(chunk); + // ws.sendBuffer(chunk, { online: true }); + // console.log('Sending audio chunk:', chunk.length); + ws.send(chunk) + fileStream.write(chunk); // 新增:将音频数据写入文件 + len += chunk.length; }); - ws.start(); + setTimeout(() => { ws.stop(); + fileStream.end(); // 新增:关闭文件流 setTimeout(() => { process.exit(0); }, 1000); console.log('len', len); - }, 10 * 30 * 1000); - // }, 5 * 1000); + }, 10 * 1000); + ws.emitter.on('message', (event) => { - // console.log('message', event.data); + console.log('message', event.data); }); }, }); diff --git a/src/asr/provider/funasr/ws.ts b/src/asr/provider/funasr/ws.ts index b460692..b6a732d 100644 --- a/src/asr/provider/funasr/ws.ts +++ b/src/asr/provider/funasr/ws.ts @@ -60,7 +60,7 @@ export class VideoWS extends WSServer { async start(opts?: Partial) { const chunk_size = new Array(5, 10, 5); - + console.log('start', chunk_size); const request: OpenRequest = { chunk_size: chunk_size, wav_name: 'h5', // @@ -94,15 +94,20 @@ export class VideoWS extends WSServer { this.ws.send(data); } } - async sendBuffer(data: Buffer, opts?: { isFile?: boolean; wav_format?: string }) { - const { wav_format = 'wav' } = opts || {}; + /** + * 发送音频数据, 离线 + * @param data 音频数据 + * @param opts 选项 + */ + async sendBuffer(data: Buffer, opts?: { isFile?: boolean; wav_format?: string; online?: boolean }) { + const { wav_format = 'wav', online = false } = 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 }); + if (!online) ws.start({ wav_format }); while (sampleBuf.length >= chunk_size) { const sendBuf = sampleBuf.slice(0, chunk_size); totalsend = totalsend + sampleBuf.length; @@ -111,7 +116,7 @@ export class VideoWS extends WSServer { ws.send(sendBuf); len++; } - ws.stop(); + if (!online) ws.stop(); } } async onMessage(event: MessageEvent) {