Cognitive-services-speech-sdk: AudioConfig.fromWavFileInput fails under node due to use of FileReader

Created on 25 Sep 2020  路  2Comments  路  Source: Azure-Samples/cognitive-services-speech-sdk

Describe the bug
Running under node and trying to use AudioConfig.fromWavFileInput fails due to it trying to use FileReader which is not available in node.

To Reproduce
Steps to reproduce the behavior:

  1. Install the SDK using npm install microsoft-cognitiveservices-speech-sdk
  2. Try to use the following code:
const sdk = require("microsoft-cognitiveservices-speech-sdk");

const audioConfig = sdk.AudioConfig.fromWavFileInput("sample.wav");

This will fail with:

node_modules/microsoft-cognitiveservices-speech-sdk/distrib/lib/src/common.browser/FileAudioSource.js:72
            var headerReader = new FileReader();
                               ^

ReferenceError: FileReader is not defined

Expected behavior
It should load the wave file successfully when running in node.

Version of the Cognitive Services Speech SDK
1.13.1

Platform, Operating System, and Programming Language

  • OS: MacOS
  • Hardware - x64
  • Programming language: Javascript
  • Browser: NA

Additional context

node_modules/microsoft-cognitiveservices-speech-sdk/distrib/lib/src/common.browser/FileAudioSource.js:72
            var headerReader = new FileReader();
                               ^

ReferenceError: FileReader is not defined

Most helpful comment

@cgreening, @mkral AudioConfig.fromWavFileInput is currently not supported for node. We do have an open item to support that method in a future release. I looked at the Speech-to-Text quickstart in Azure Docs, and was surprised to find bad information there about fromWavFileInput. To create an inputStream for audioConfig, please use the following:

  var sdk = require("microsoft-cognitiveservices-speech-sdk");
  var fs = require("fs")
  var filename = "<<FILENAME>>"; // 16000 Hz, Mono

  // create the push stream we need for the speech sdk.
  var pushStream = sdk.AudioInputStream.createPushStream();

  // open the file and push it to the push stream.
  fs.createReadStream(filename).on('data', function(arrayBuffer) {
    pushStream.write(arrayBuffer.slice());
  }).on('end', function() {
    pushStream.close();
  });

  var audioConfig = sdk.AudioConfig.fromStreamInput(pushStream);

I apologize for the incorrect info in the docs, and have opened an issue with the docs team to get it corrected ASAP.

Thanks,
Glenn

All 2 comments

I'm also experiencing this issue, any help would be appreciated.

@cgreening, @mkral AudioConfig.fromWavFileInput is currently not supported for node. We do have an open item to support that method in a future release. I looked at the Speech-to-Text quickstart in Azure Docs, and was surprised to find bad information there about fromWavFileInput. To create an inputStream for audioConfig, please use the following:

  var sdk = require("microsoft-cognitiveservices-speech-sdk");
  var fs = require("fs")
  var filename = "<<FILENAME>>"; // 16000 Hz, Mono

  // create the push stream we need for the speech sdk.
  var pushStream = sdk.AudioInputStream.createPushStream();

  // open the file and push it to the push stream.
  fs.createReadStream(filename).on('data', function(arrayBuffer) {
    pushStream.write(arrayBuffer.slice());
  }).on('end', function() {
    pushStream.close();
  });

  var audioConfig = sdk.AudioConfig.fromStreamInput(pushStream);

I apologize for the incorrect info in the docs, and have opened an issue with the docs team to get it corrected ASAP.

Thanks,
Glenn

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RodrigoRVieira picture RodrigoRVieira  路  7Comments

vins31 picture vins31  路  5Comments

SOliasS picture SOliasS  路  4Comments

jzhw0130 picture jzhw0130  路  3Comments

Ray12345678910 picture Ray12345678910  路  3Comments