generated from tailored/router-template
temp: add test
This commit is contained in:
49
src/wake/test/build.ts
Normal file
49
src/wake/test/build.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import vosk from 'vosk';
|
||||
import { Recording } from '../../recorder/index.ts';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { audioPath, sleep } from './common.ts';
|
||||
import { encodeWav, decodeWav } from '../../utils/convert.ts';
|
||||
// 需要先下载Vosk模型
|
||||
// const MODEL_PATH = 'vosk-model-small-en-us-0.15';
|
||||
const MODEL_PATH = path.join(process.cwd(), 'models/vosk-model-small-cn-0.22');
|
||||
|
||||
async function detectWithVosk(audioFilePath) {
|
||||
if (!fs.existsSync(MODEL_PATH)) {
|
||||
console.error('请先下载Vosk模型');
|
||||
return false;
|
||||
}
|
||||
|
||||
const model = new vosk.Model(MODEL_PATH);
|
||||
const rec = new vosk.Recognizer({ model: model, sampleRate: 16000 });
|
||||
|
||||
// const wakeWords = ['hey computer', 'okay jarvis']; // 自定义唤醒词列表
|
||||
const wakeWords = ['欢迎']; // 自定义唤醒词列表
|
||||
const audioBuffer = fs.readFileSync(audioFilePath);
|
||||
const pcmBuffer = decodeWav(audioBuffer);
|
||||
const result = await rec.acceptWaveformAsync(pcmBuffer);
|
||||
console.log('result', result, rec.result());
|
||||
// return new Promise((resolve) => {
|
||||
// const pcmBufferLength = Buffer.byteLength(pcmBuffer);
|
||||
// console.log('pcmBufferLength', pcmBufferLength);
|
||||
// const bufferLength = 1024 * 8;
|
||||
// let index = 0;
|
||||
// for (let i = 0; i < pcmBufferLength; i += bufferLength) {
|
||||
// const chunk = pcmBuffer.subarray(i, i + bufferLength);
|
||||
// index++;
|
||||
// if (rec.acceptWaveform(chunk)) {
|
||||
// const result = rec.result();
|
||||
// console.log('=========result', result, index);
|
||||
// const text = result.text.toLowerCase();
|
||||
// if (wakeWords.some((word) => text.includes(word))) {
|
||||
// resolve(true);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// resolve(false);
|
||||
// });
|
||||
}
|
||||
|
||||
detectWithVosk(audioPath).then((result) => {
|
||||
console.log('result', result);
|
||||
});
|
||||
12
src/wake/test/common.ts
Normal file
12
src/wake/test/common.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import path from 'path';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
export const config = dotenv.config({
|
||||
path: path.join(process.cwd(), '.env'),
|
||||
}).parsed;
|
||||
|
||||
export const audioPath = path.join(process.cwd(), 'videos/asr_example.wav');
|
||||
export const audioPath2 = path.join(process.cwd(), 'videos/asr_example2.wav');
|
||||
export const blankAudioPath = path.join(process.cwd(), 'videos/blank.wav');
|
||||
|
||||
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
Reference in New Issue
Block a user