【问题标题】:recorder.js to node to azure speaker recognitionrecorder.js 到节点以进行 azure 说话人识别
【发布时间】:2020-04-18 03:47:31
【问题描述】:

我有一个浏览器应用程序,它通过麦克风记录用户的声音并使用 recorder.js 导出到 WAV 文件。我认为将其转换为数据 url 并将其发布到节点。然后我尝试将音频发送到 Microsoft Azure Speaker Recognition API,但我总是得到“无效的音频格式:不是 WAVE 文件 - 没有 RIFF 标头”。

有没有办法添加 RIFF 标头,或者是否有关于 recorder.js 或与 base64 之间的转换来删除这些标头?有没有办法重新添加它们?

代码: 索引.HTML

rec.exportWAV(function (blob) {
    var reader = new window.FileReader();
    reader.readAsDataURL(blob);
    reader.onloadend = function () {
        base64 = reader.result;
        console.log(base64)
        $.ajax({
            type: "POST",
            url: '/addVoiceToProfile',
            data: { userId: '', voiceId: "XXX-XXX", voice: base64, sampleRate: audioContext.sampleRate / 1000 },
            success: function (results) {
                console.log(results)
            }
        })
    }
})

节点路线:

app.post('/addVoiceToProfile', function(req, res){
    var voicedata = req.body.voice;
    var base64Data = voicedata.replace(/^data:([A-Za-z-+/]+);base64,/, '');
    addVoicesToProfile(base64Data, req.body.voiceId).then(function(results){
        res.send(results)
    })
})

addVoicesToProfile:

function addVoicesToProfile(voice, id, user){
    return new Promise(function (resolve, reject) {
        var url = AzureParameters.endPoint+"/spid/v1.0/verificationProfiles/"+id+"/enroll";
        request({
            url:url,
            headers:{'Content-Type':'audio/wave', 'Ocp-Apim-Subscription-Key':AzureParameters.key},
            body: Buffer.from(voice, 'base64').toString('binary'),
            method: "POST",
            json: true
        }, function(err, response, body){
            if(err) return reject(err);
            return resolve(body)
        })
    })
}

【问题讨论】:

  • 你错过了来自 recorder.js github.com/mattdiamond/Recorderjs/blob/… 的 encodeWAV。你在sanpleRate也有错字
  • 谢谢 Nikolay - 我在哪里做 encodeWav?
  • 没关系,它已经在 recorderjs 中完成了。首先修复 sanpleRate。

标签: javascript azure microsoft-cognitive voice-recognition recorder.js


【解决方案1】:

看起来问题实际上是文件的发送方式。我不知道为什么它在帖子正文中以 wav 形式发送,而不是作为帖子中的数据,但如果它对将来的任何人有帮助,这是我的工作代码:

html:

我使用this answer 作为 html。它包括 wav 的编码。正如答案所示,我所做的唯一更改是发布到我的服务器,而不是直接发布到 MS Azure 服务。

节点路由:

这花了我一段时间,因为我无法让 node 查看我正在发送的文件。此外,我正在使用 bodyParser,因此在我看到它之前,原始请求的原始正文就被摆弄了。这是我发帖请求的路线:

  app.post('/uploadAudio', function (req, res, next) {
    var data = new Buffer('');
    req.on('data', function (chunk) {
        data = Buffer.concat([data, chunk]);
    });
    req.on('end', function () {
        req.rawBody = data;
        next();
    });
  }, function (req, res) {
    addVoicesToProfile(req.rawBody)
  })

我希望这对某人有所帮助,因为这两个部分都花了我太长时间来解决!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 2020-07-01
    相关资源
    最近更新 更多