I'm trying to run nodeJS Google Speech example, but when I start it with node recognize.js listen
it stops after 3 seconds without any output. There is no error output.
I've already tried other NodeJS programs and they run without problems.
I'm using nodejs v8.4.0 and npm v5.3.0 on Ubuntu 14.04.5 LTS. I've tried this on Ubuntu 16.04 LTS and worked fine.
This is the code I'm trying to run:
'use strict';
function streamingMicRecognize (encoding, sampleRateHertz, languageCode) {
const record = require('node-record-lpcm16');
const Speech = require('@google-cloud/speech');
const speech = Speech();
const request = {
config: {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode
},
interimResults: false // If you want interim results, set this to true
};
const recognizeStream = speech.streamingRecognize(request)
.on('error', console.error)
.on('data', (data) =>
process.stdout.write(
(data.results[0] && data.results[0].alternatives[0])
? `Transcription: ${data.results[0].alternatives[0].transcript}\n`
: `\n\nReached transcription time limit, press Ctrl+C\n`));
record
.start({
sampleRateHertz: sampleRateHertz,
threshold: 0,
verbose: false,
recordProgram: 'rec', // Try also "arecord" or "sox"
silence: '10.0'
})
.on('error', console.error)
.pipe(recognizeStream);
console.log('Listening, press Ctrl+C to stop.');
}
const cli = require(`yargs`)
.demand(1)
.command(
`listen`,
`Detects speech in a microphone input stream. This command requires that you have SoX installed and available in your $PATH. See https://www.npmjs.com/package/node-record-lpcm16#dependencies`,
{},
(opts) => streamingMicRecognize(opts.encoding, opts.sampleRateHertz, opts.languageCode)
)
.options({
encoding: {
alias: 'e',
default: 'LINEAR16',
global: true,
requiresArg: true,
type: 'string'
},
sampleRateHertz: {
alias: 'r',
default: 16000,
global: true,
requiresArg: true,
type: 'number'
},
languageCode: {
alias: 'l',
default: 'en-US',
global: true,
requiresArg: true,
type: 'string'
}
})
.example(`node $0 listen`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
.help()
.strict();
if (module === require.main) {
cli.parse(process.argv.slice(2));
}
I've figured out that it was the device option from node-record-lpcm16. Only added the option
device: 'plughw:0,0'
atrecord.start
.