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/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);
},
});

View File

@ -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);
});
},
});

View File

@ -60,7 +60,7 @@ export class VideoWS extends WSServer {
async start(opts?: Partial<OpenRequest>) {
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) {