generated from tailored/router-template
update
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import * as zlib from 'zlib';
|
||||
import { promisify } from 'util';
|
||||
import * as zlib from 'node:zlib';
|
||||
import { promisify } from 'node:util';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { VolcEngineBase, uuid } from './base.ts';
|
||||
|
||||
@@ -61,6 +61,39 @@ function generateBeforePayload(sequence: number): Buffer {
|
||||
return beforePayload;
|
||||
}
|
||||
|
||||
export type ParsedMessage = {
|
||||
isLastPackage: boolean;
|
||||
payloadSequence?: number;
|
||||
payloadMsg?: {
|
||||
audio_info?: {
|
||||
duration: number;
|
||||
};
|
||||
result?: {
|
||||
additions?: {
|
||||
log_id?: string;
|
||||
};
|
||||
text?: string;
|
||||
utterances?: Array<{
|
||||
additions?: {
|
||||
fixed_prefix_result?: string;
|
||||
};
|
||||
definite?: boolean;
|
||||
end_time?: number;
|
||||
start_time?: number;
|
||||
text?: string;
|
||||
words?: Array<{
|
||||
end_time: number;
|
||||
start_time: number;
|
||||
text: string;
|
||||
}>;
|
||||
}>;
|
||||
};
|
||||
error?: any;
|
||||
};
|
||||
payloadSize?: number;
|
||||
code?: number;
|
||||
seq?: number;
|
||||
};
|
||||
/**
|
||||
* Parse response from the WebSocket server
|
||||
*/
|
||||
@@ -393,10 +426,11 @@ export class AsrWsClient extends VolcEngineBase {
|
||||
// Wait for response
|
||||
await sendVoice(audioData, segmentSize);
|
||||
}
|
||||
|
||||
async onMessage(event: MessageEvent) {
|
||||
try {
|
||||
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);
|
||||
}
|
||||
@@ -405,10 +439,9 @@ export class AsrWsClient extends VolcEngineBase {
|
||||
this.emitter.emit('error', parsed);
|
||||
this.isError = true;
|
||||
}
|
||||
this.emitter.emit('message', parsed);
|
||||
if (parsed.isLastPackage) {
|
||||
this.emitter.emit('end', parsed);
|
||||
} else {
|
||||
this.emitter.emit('message', parsed);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error processing response:', error);
|
||||
@@ -440,6 +473,14 @@ export class AsrWsClient extends VolcEngineBase {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
async setIsEnd(isEnd: boolean) {
|
||||
super.setIsEnd(isEnd);
|
||||
if (isEnd) {
|
||||
// 发送空白包
|
||||
const emptyBuffer = Buffer.alloc(10000);
|
||||
this.sendVoiceStream(emptyBuffer);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 发送语音流, 最小10000
|
||||
* @param data
|
||||
|
||||
@@ -238,7 +238,7 @@ interface AudioItem {
|
||||
id: string | number;
|
||||
path: string;
|
||||
}
|
||||
|
||||
// 流式语音识别
|
||||
export class AsrWsClient extends VolcEngineBase {
|
||||
private audioPath: string;
|
||||
private cluster: string;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import crypto from "node:crypto"
|
||||
import { nanoid } from "nanoid"
|
||||
|
||||
export const FlashURL = "https://openspeech.bytedance.com/api/v3/auc/bigmodel/recognize/flash"
|
||||
export const AsrBaseURL = 'https://openspeech.bytedance.com/api/v3/auc/bigmodel/submit'
|
||||
export const AsrBase = 'volc.bigasr.auc'
|
||||
export const AsrTurbo = 'volc.bigasr.auc_turbo'
|
||||
|
||||
const uuid = () => crypto.randomUUID()
|
||||
const uuid = () => nanoid()
|
||||
|
||||
type AsrOptions = {
|
||||
url?: string
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { initWs } from '../../../ws-adapter/index.ts';
|
||||
import { WSServer } from '../../provider/ws-server.ts';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
|
||||
@@ -7,15 +7,22 @@ import fs from 'fs';
|
||||
const main = async () => {
|
||||
const audioId = '123';
|
||||
const asrClient = new AsrWsClient({
|
||||
appid: config.APP_ID,
|
||||
token: config.TOKEN,
|
||||
appid: config.VOLCENGINE_ASR_MODEL_APPID,
|
||||
token: config.VOLCENGINE_ASR_MODEL_TOKEN,
|
||||
});
|
||||
asrClient.emitter.on('message', (result) => {
|
||||
console.log('识别结果', JSON.stringify(result, null, 2));
|
||||
})
|
||||
asrClient.emitter.on('end', (result) => {
|
||||
console.log('识别结束', JSON.stringify(result, null, 2));
|
||||
})
|
||||
await new Promise((resolve) => setTimeout(resolve, 2000));
|
||||
const data = fs.readFileSync(audioPath);
|
||||
await asrClient.sendVoiceFile(data);
|
||||
await asrClient.sendVoiceFile(fs.readFileSync(blankAudioPath));
|
||||
// await asrClient.sendVoiceFile(fs.readFileSync(blankAudioPath));
|
||||
asrClient.setIsEnd(true);
|
||||
await asrClient.sendVoiceFile(fs.readFileSync(audioPath2));
|
||||
// await asrClient.sendVoiceFile(fs.readFileSync(audioPath2));
|
||||
|
||||
};
|
||||
|
||||
main();
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
const isBrowser = process?.env?.BROWSER === 'true' || (typeof window !== 'undefined' && typeof window.document !== 'undefined');
|
||||
const isBrowser = (typeof process === 'undefined') ||
|
||||
(typeof window !== 'undefined' && typeof window.document !== 'undefined') ||
|
||||
(typeof process !== 'undefined' && process?.env?.BROWSER === 'true');
|
||||
const chantHttpToWs = (url: string) => {
|
||||
if (url.startsWith('http://')) {
|
||||
return url.replace('http://', 'ws://');
|
||||
|
||||
Reference in New Issue
Block a user