temp test

This commit is contained in:
熊潇 2025-08-23 22:34:36 +08:00
parent e4596b4fde
commit 8047577165
3 changed files with 65 additions and 31 deletions

View File

@ -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/asr_example2.wav');
// const videoTestPath = path.join(process.cwd(), 'videos/tts_mix.mp3'); // const videoTestPath = path.join(process.cwd(), 'videos/tts_mix.mp3');
const videoTestPath = path.join(process.cwd(), 'videos/my_speech_text.wav'); 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 name = 'output-1746007775571.mp3';
const videoTestPath2 = path.join(process.cwd(), 'build', name); const videoTestPath2 = path.join(process.cwd(), 'build', name);
const url = 'wss://funasr.xiongxiao.me'; const url = 'wss://funasr.xiongxiao.me';
const url5 = 'https://1.15.101.247:10095'; // pro
// const ws = new VideoWS({ // const ws = new VideoWS({
// // url: 'wss://192.168.31.220:10095', // // url: 'wss://192.168.31.220:10095',
// url: 'wss://funasr.xiongxiao.me', // url: 'wss://funasr.xiongxiao.me',
@ -54,12 +56,27 @@ const url = 'wss://funasr.xiongxiao.me';
// server.listen(10096); // server.listen(10096);
const ws2 = new VideoWS({ const ws2 = new VideoWS({
url: url, url: url5,
mode: '2pass',
onConnect: async () => { onConnect: async () => {
const data = fs.readFileSync(videoTestPath); const data = fs.readFileSync(videoTestPath3);
await ws2.sendBuffer(data, { wav_format: 'mp3' }); // await ws2.sendBuffer(data, { wav_format: 'mp3' });
await new Promise((resolve) => setTimeout(resolve, 1000)); // await new Promise((resolve) => setTimeout(resolve, 1000));
const data2 = fs.readFileSync(videoTestPath2); // const data2 = fs.readFileSync(videoTestPath2);
await ws2.sendBuffer(data2, { wav_format: 'mp3' }); // 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);
}, },
}); });

View File

@ -1,48 +1,60 @@
import { VideoWS } from '../ws.ts'; import { VideoWS } from '../ws.ts';
import path from 'node:path';
import net from 'net'; import net from 'net';
import { Recording } from '../../../../recorder/index.ts'; import { Recording } from '../../../../recorder/index.ts';
import Stream from 'stream'; 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 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({ const ws = new VideoWS({
// url: 'wss://192.168.31.220:10095', url: url5,
url: url,
isFile: false, isFile: false,
// mode: 'online',
mode: '2pass', mode: '2pass',
wsOptions: { wsOptions: {
rejectUnauthorized: false, rejectUnauthorized: false,
}, },
onConnect: async () => { onConnect: async () => {
console.log('onConnect'); console.log('onConnect');
ws.start();
recorder.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; let len = 0;
recorder.stream().on('data', (chunk) => { recorder.stream().on('data', (chunk) => {
// chunks = Buffer.concat([chunks, chunk]); // ws.sendBuffer(chunk, { online: true });
// if (chunks.length > chunk_size) { // console.log('Sending audio chunk:', chunk.length);
// ws.send(chunks); ws.send(chunk)
// console.log('chunk', chunk.length); fileStream.write(chunk); // 新增:将音频数据写入文件
len += chunk.length;
// totalsend += chunks.length;
// chunks = Buffer.alloc(0);
// }
ws.send(chunk);
}); });
ws.start();
setTimeout(() => { setTimeout(() => {
ws.stop(); ws.stop();
fileStream.end(); // 新增:关闭文件流
setTimeout(() => { setTimeout(() => {
process.exit(0); process.exit(0);
}, 1000); }, 1000);
console.log('len', len); console.log('len', len);
}, 10 * 30 * 1000); }, 10 * 1000);
// }, 5 * 1000);
ws.emitter.on('message', (event) => { ws.emitter.on('message', (event) => {
// console.log('message', event.data); console.log('message', event.data);
}); });
}, },
}); });

View File

@ -60,7 +60,7 @@ export class VideoWS extends WSServer {
async start(opts?: Partial<OpenRequest>) { async start(opts?: Partial<OpenRequest>) {
const chunk_size = new Array(5, 10, 5); const chunk_size = new Array(5, 10, 5);
console.log('start', chunk_size);
const request: OpenRequest = { const request: OpenRequest = {
chunk_size: chunk_size, chunk_size: chunk_size,
wav_name: 'h5', // wav_name: 'h5', //
@ -94,15 +94,20 @@ export class VideoWS extends WSServer {
this.ws.send(data); 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) { if (this.ws && this.ws.readyState === WebSocket.OPEN) {
let sampleBuf = new Uint8Array(data); let sampleBuf = new Uint8Array(data);
const ws = this; const ws = this;
var chunk_size = 960; // for asr chunk_size [5, 10, 5] var chunk_size = 960; // for asr chunk_size [5, 10, 5]
let totalsend = 0; let totalsend = 0;
let len = 0; let len = 0;
ws.start({ wav_format }); if (!online) ws.start({ wav_format });
while (sampleBuf.length >= chunk_size) { while (sampleBuf.length >= chunk_size) {
const sendBuf = sampleBuf.slice(0, chunk_size); const sendBuf = sampleBuf.slice(0, chunk_size);
totalsend = totalsend + sampleBuf.length; totalsend = totalsend + sampleBuf.length;
@ -111,7 +116,7 @@ export class VideoWS extends WSServer {
ws.send(sendBuf); ws.send(sendBuf);
len++; len++;
} }
ws.stop(); if (!online) ws.stop();
} }
} }
async onMessage(event: MessageEvent) { async onMessage(event: MessageEvent) {