引入vosk
"vosk": "^0.3.32"
创建js文件
import vosk from 'vosk'
import fs from "fs"
const { Readable } = require("stream");
const cp = require("child_process");
const wav = require("wav");
export default {
//语音识别
async readFile(that,filePath,callback){
let model = await new vosk.Model(that.modePath)
let wfReader = await new wav.Reader();
const wfReadable = new Readable().wrap(wfReader);
wfReader.on('format', async ({ audioFormat, sampleRate, channels }) => {
console.log(audioFormat, sampleRate, channels)
let rec = new vosk.Recognizer({model: model, sampleRate: sampleRate});
if (audioFormat != 1 ||channels !=1) {
process.exit(1);
}
let i=0
let wordResult=[]
for await (let data of wfReadable) {
that.itemSize=data.length
const end_of_speech = await rec.acceptWaveformAsync(data);
i++;
if (end_of_speech) {
let text=rec.result().text
console.log(text);
wordResult.push({index:i,size:data.length,word:text})
}else{
wordResult.push({index:i,size:data.length,word:''})
}
}
let re=await rec.finalResult();
callback(wordResult);
rec.free();
});
fs.createReadStream(filePath, {'highWaterMark': 4096}).pipe(wfReader);
},
}
that是引用资源的this对象,filePath是wav格式的音频文件路径,callBack是完成后方法回调,modePath是vosk的model路径。
https://download.csdn.net/download/qq_22071421/85159760
这里没有像java和ws生成时间节点的json,只有识别文字,所以我是想用{index:i,size:data.length,word:text}这样的对象,然后获取音频文件的总大小,和data.length去计算。