【问题标题】:Azure Speech to text result undefined nodejsAzure Speech to text 结果未定义 nodejs
【发布时间】:2020-06-24 13:24:07
【问题描述】:

我正在尝试使用 userMedia 实现语音到文本。我在成功创建的 Nodejs 服务器文件上的文件中写入流,但是当尝试使用 Azure fromStreamInput 将语音转换为文本时得到未定义的结果。


var subscriptionKey = "--";
  var serviceRegion = "--"; // e.g., "westus"


  var s = sdk.SpeechConfig.fromSubscription(subscriptionKey, serviceRegion);
  function LoadArrayFromFile (filename) {
    const fileContents = fs.readFileSync(filename);

    const ret = Uint8Array.from(fileContents.slice(44));
 
    return ret.buffer;
  }
  const fileBuffer = LoadArrayFromFile('output.mp3');

  let bytesSent = 0;
  let p;

  p = sdk.AudioInputStream.createPullStream(
    {
      close: () => { return; },
      read: (buffer) => {
        const copyArray = new Uint8Array(buffer);
        const start = bytesSent;
        const end = buffer.byteLength > (fileBuffer.byteLength - bytesSent) ? (fileBuffer.byteLength - 1) : (bytesSent + buffer.byteLength - 1);
        copyArray.set(new Uint8Array(fileBuffer.slice(start, end)));
        bytesSent += (end - start) + 1;

        if (bytesSent < buffer.byteLength) {
          setTimeout(() => p.close(), 1000);
        }

        return (end - start) + 1;
      },
    });

  const config = sdk.AudioConfig.fromStreamInput(p);

  const r = new sdk.SpeechRecognizer(s, config);

  // expect(r).not.toBeUndefined();
  // expect(r instanceof sdk.Recognizer);

  r.canceled = (o, e) => {
    try {
      console.log("canceled", res)

    } catch (error) {
      console.log("canceled error", error)

    }
  };

  r.recognizeOnceAsync(
    (p2) => {
      const res = p2;
      try {
       console.log(res)
      } catch (error) {
        console.log(error)
        // done.fail(error);
      }
    },
    (error) => {
      console.log(error)

      // done.fail(error);
    });
  });

【问题讨论】:

    标签: azure speech-to-text getusermedia nodejs-stream azure-speech


    【解决方案1】:

    您的代码正在读取 .mp3 文件的内容并将其传递给 AudioConfig.fromStreamInput()。但是azure doc 非常清楚地表明只有未压缩的音频(他们说脉冲编码调制或 .WAV)在这里有效。

    在将音频传递给语音识别器之前,您需要弄清楚如何解压缩音频。否则 Azure 将不得不弄清楚如何处理压缩音频。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多