简体   繁体   English

Node.js中的Google Cloud语音到文本api编码问题

[英]Google Cloud Speech-to-Text api encoding issues in Node.js

I'm trying to capture audio from a user's microphone and send it to a server where it will get sent to Google's Speech-to-Text-API for translation. 我正在尝试从用户的麦克风捕获音频,然后将其发送到服务器,该服务器将发送到Google的语音文本API进行翻译。 I'm accessing the audio using navigator.mediaDevices.GetuserMedia() which I capture using a MediaRecorder object. 我正在使用navigator.mediaDevices.GetuserMedia()访问音频,该音频是使用MediaRecorder对象捕获的。 When I run the following code I get an error from Google that says "INVALID_ARGUMENT: RecognitionAudio not set." 当我运行以下代码时,我从Google收到一条错误消息,提示“ INVALID_ARGUMENT:未设置RecognitionAudio”。 I'm not sure how to set it as the relavant page ( https://cloud.google.com/speech-to-text/docs/reference/rest/v1/RecognitionAudio ) doesn't say much about it. 我不确定如何将其设置为相关页面( https://cloud.google.com/speech-to-text/docs/reference/rest/v1/RecognitionAudio )对此并没有过多提及。 Relevant client side code that runs after user presses stop: 用户按下停止后运行的相关客户端代码:

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
 });

Server side code running node.js: 运行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);
      });
});

If I run the server code with the line "const audioBytes = req.body;" 如果我使用“ const audioBytes = req.body;”行运行服务器代码。 changed to "const audioBytes = req.body.content;" 更改为“ const audioBytes = req.body.content;” I get an error message that there is bad encoding. 我收到一条错误消息,提示编码错误。 I'm not sure if I'm encoding it properly on the client side or if I'm accessing it properly on the server side. 我不确定是在客户端正确编码还是在服务器上正确访问。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

const config = {
    // "enableAutomaticPunctuation": true,
    "encoding": "LINEAR16",
    "languageCode": "en-US",
    "model": "default",
    "sampleRateHertz": 44100,
    audioChannelCount: 2,
    enableSeparateRecognitionPerChannel: true,
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM