This commit is contained in:
熊潇 2025-04-20 01:32:28 +08:00
parent 5e781499e9
commit 17ce93d5bd
5 changed files with 49 additions and 3 deletions

0
src/asr/index.ts Normal file
View File

View File

@ -315,7 +315,7 @@ export class AsrWsClient extends VolcEngineBase {
}, },
}; };
} }
private async sendFullClientRequest() { async sendFullClientRequest() {
if (this.hasSendFullClientRequest) { if (this.hasSendFullClientRequest) {
return; return;
} }
@ -353,6 +353,7 @@ export class AsrWsClient extends VolcEngineBase {
for (const [chunk, last] of sliceData(audioData, segmentSize)) { for (const [chunk, last] of sliceData(audioData, segmentSize)) {
that.seq += 1; that.seq += 1;
const isEnd = that.isEnd && last; // 结束了,而且是语音的最后一包 const isEnd = that.isEnd && last; // 结束了,而且是语音的最后一包
console.log('chunkSize', Buffer.byteLength(chunk), segmentSize, 'last', last);
if (isEnd) { if (isEnd) {
that.seq = -that.seq; that.seq = -that.seq;
} }
@ -391,9 +392,14 @@ export class AsrWsClient extends VolcEngineBase {
try { try {
const parsed = parseResponse(Buffer.from(event.data as ArrayBuffer)); const parsed = parseResponse(Buffer.from(event.data as ArrayBuffer));
console.log(`Seq ${parsed.payloadSequence} response:`, parsed); console.log(`Seq ${parsed.payloadSequence} response:`, parsed);
if (typeof event.data === 'string') {
throw new Error('event.data is string: ' + event.data);
}
// console.log('parsed', parsed.payloadSequence, parsed.payloadMsg.result.text); // console.log('parsed', parsed.payloadSequence, parsed.payloadMsg.result.text);
if (parsed.isLastPackage) { if (parsed.isLastPackage) {
this.emitter.emit('end', parsed); this.emitter.emit('end', parsed);
} else {
this.emitter.emit('message', parsed);
} }
} catch (error) { } catch (error) {
console.error('Error processing response:', error); console.error('Error processing response:', error);
@ -425,9 +431,14 @@ export class AsrWsClient extends VolcEngineBase {
throw error; throw error;
} }
} }
/**
* , 10000
* @param data
* @returns
*/
public async sendVoiceStream(data: Buffer) { public async sendVoiceStream(data: Buffer) {
const segmentSize = Buffer.byteLength(data); let segmentSize = Buffer.byteLength(data);
console.log('segmentSize', segmentSize);
return await this.segmentDataProcessor(data, segmentSize); return await this.segmentDataProcessor(data, segmentSize);
} }
} }

View File

@ -33,6 +33,7 @@ export class VolcEngineBase extends WSServer {
async onOpen() { async onOpen() {
console.log('VolcEngineBase onOpen'); console.log('VolcEngineBase onOpen');
// 发送认证信息 // 发送认证信息
this.emitter.emit('open');
} }
async isCanSend() { async isCanSend() {
if (this.canSend) { if (this.canSend) {

View File

@ -13,7 +13,12 @@ export class WSServer {
onConnect?: () => void; onConnect?: () => void;
connected: boolean; connected: boolean;
emitter: EventEmitter; emitter: EventEmitter;
url: string;
wsOptions?: ClientOptions;
constructor(opts: WSSOptions) { constructor(opts: WSSOptions) {
this.connected = false;
this.url = opts.url;
this.wsOptions = opts.wsOptions;
this.initWs(opts); this.initWs(opts);
} }
async initWs(opts: WSSOptions) { async initWs(opts: WSSOptions) {
@ -28,6 +33,13 @@ export class WSServer {
this.ws.onerror = this.onError.bind(this); this.ws.onerror = this.onError.bind(this);
this.ws.onclose = this.onClose.bind(this); this.ws.onclose = this.onClose.bind(this);
} }
async reconnect() {
this.ws = await initWs(this.url, this.wsOptions);
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);
}
/** /**
* ws * ws
*/ */
@ -73,5 +85,14 @@ export class WSServer {
async onClose(event: CloseEvent) { async onClose(event: CloseEvent) {
console.error('WSS onClose'); console.error('WSS onClose');
this.emitter.emit('close', event); this.emitter.emit('close', event);
this.connected = false;
}
/**
* ws
*/
async close() {
if (this.ws.readyState === WebSocket.OPEN) {
this.ws.close();
}
} }
} }

View File

@ -67,3 +67,16 @@ export const converter = {
encodeWav, encodeWav,
decodeWav, decodeWav,
}; };
/**
*
* @param duration
* @param sampleRate
* @param channels
* @returns
*/
export const generateSilent = (duration: number = 2, sampleRate: number = 16000, channels: number = 1) => {
const bufferSize = Math.floor(duration * sampleRate * channels * 2); // 2 bytes per sample
const silent = Buffer.alloc(bufferSize);
return silent;
};