gillesdemey / node-record-lpcm16

:microphone: Records a 16-bit signed-integer linear pulse modulation code encoded audio file.
ISC License
212 stars 60 forks source link

SoX crashes #53

Open avi12 opened 5 years ago

avi12 commented 5 years ago

I added node_modules/node-record-lpcm16/examples/speech-to-text.js:

'use strict';

const path = require("path");
const fs = require("fs");
const {record} = require('../');
const speech = require("@google-cloud/speech");

const client = new speech.SpeechClient({keyFilename: getFullPath("cloud-api-credentials.json")});
const recording = record();

const request = {
  config: {
    encoding: "LINEAR16",
    sampleRateHertz: 16000,
    // Languages: https://cloud.google.com/speech-to-text/docs/languages
    languageCode: "en-US",
    enableAutomaticPunctuation: true,
    model: "default", // https://cloud.google.com/speech-to-text/docs/transcription-model
    useEnhanced: true
  },
  interimResults: false,
};

let recognizeStream;

function startRecording() {
  recognizeStream = client
    .streamingRecognize(request)
    .on("data", data => {
      console.log(data.results[0].alternatives[0].transcript);
    });

  recording
    .stream({verbose: true})
    .pipe(recognizeStream);
}

startRecording();

function getFullPath(item) {
  const folder = __dirname.split(path.sep);
  const getFullPathOf = item => path.join(folder.join("/"), item);

  while (!fs.existsSync(getFullPathOf(item))) {
    folder.pop();
  }
  return getFullPathOf(item);
}

If I try to run this script while using SoX 14.4.2, I get an error:

events.js:189
    throw err; // Unhandled 'error' event
    ^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('sox has exited with error code 1.\n\nEnable debugging with the environment variable DEBUG=record.')
    at Socket.emit (events.js:187:17)
    at ChildProcess.cp.on.code (C:\repositories\speech-to-text\node_modules\node-record-lpcm16\index.js:68:11)
    at ChildProcess.emit (events.js:198:13)
    at maybeClose (internal/child_process.js:982:16)
    at Socket.stream.socket.on (internal/child_process.js:389:11)
    at Socket.emit (events.js:198:13)
    at Pipe._handle.close (net.js:606:12)

If I try to use SoX 14.4.1 instead, the script works.
However, once I call recording.stop() - the same error as the one above, occurs.

How can I fix the problem?
It is certainly not a problem with SoX, as in the past, I made a prototype with which I used this package with SoX 14.4.1 and it worked very nice.

MisterCommand commented 4 years ago

Same problem, any solution?

mInzamamMalik commented 4 years ago

sox 14.4.1 worked for me, earlier it was saying no default device found