【问题标题】:Google Cloud Speech-to-Text api encoding issues in Node.jsNode.js 中的 Google Cloud Speech-to-Text api 编码问题
【发布时间】:2018-07-29 01:31:38
【问题描述】:

我正在尝试从用户的麦克风捕获音频并将其发送到服务器,然后将其发送到 Google 的 Speech-to-Text-API 进行翻译。我正在使用我使用 MediaRecorder 对象捕获的 navigator.mediaDevices.GetuserMedia() 访问音频。当我运行以下代码时,我从 Google 收到一条错误消息,上面写着“INVALID_ARGUMENT: RecognitionAudio not set”。我不确定如何将其设置为相关页面(https://cloud.google.com/speech-to-text/docs/reference/rest/v1/RecognitionAudio)并没有说太多。 用户按下停止后运行的相关客户端代码:

mediaRecorder.onstop = function(e) {
     var blob = new Blob(chunks, { type : 'audio/flac' });
     var reader = new FileReader();
     reader.readAsBinaryString(blob); 
     reader.onloadend = function() {
       base64data = reader.result;
       writeBinaryFile(base64data)
     }

     chunks = []; //array to store recording
}

//asynchronous binary file write
function writeBinaryFile(content) {
 $.ajax({
   type: "POST",
   url: "/voice_api",
   data: { content: content }
 }).done(function(data) {
   // TODO: display success status somewhere
 });

运行 node.js 的服务器端代码:

app.post("/voice_api", (req, res) => {
    const audioBytes = req.body;
    // The audio file's encoding, sample rate in hertz, and BCP-47 language code
    const audio = {
      content: audioBytes,
    };
    const config = {
      languageCode: 'en-US'
    };
    const request = {
      audio: audio,
      config: config
    };

    // Detects speech in the audio file
    client
      .recognize(request)
      .then(data => {
        const response = data[0];
        const transcription = response.results
          .map(result => result.alternatives[0].transcript)
          .join('\n');
        console.log(`Transcription: ${transcription}`);
        res.send(transcription);
      })
      .catch(err => {
        console.error('ERROR:', err);
      });
});

如果我使用“const audioBytes = req.body;”行运行服务器代码改为“const audioBytes = req.body.content;”我收到一条错误消息,提示编码错误。我不确定我是在客户端正确编码还是在服务器端正确访问它。任何帮助,将不胜感激。谢谢!

【问题讨论】:

    标签: javascript node.js encoding google-text-to-speech


    【解决方案1】:
    const config = {
        // "enableAutomaticPunctuation": true,
        "encoding": "LINEAR16",
        "languageCode": "en-US",
        "model": "default",
        "sampleRateHertz": 44100,
        audioChannelCount: 2,
        enableSeparateRecognitionPerChannel: true,
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多