Skip to content

Unable to record using node.js example  #409

@josefsandoval

Description

@josefsandoval

Hello
I am trying to use the google cloud speech API. I already created a cloud project and got authenticated.
I also have created a service account key and got the json file that comes with it as well.

I am trying to get this example to work: Performing Streaming Speech Recognition on an Audio Stream
Link: https://cloud.google.com/speech/docs/streaming-recognize

Here is the code I am running on the command line with node

// [START speech_streaming_mic_recognize]
  const record = require('node-record-lpcm16');

    // Your Google Cloud Platform project ID
  const projectId = 'my projects name';

  const  keyFilename = 'path to my key .json file ';

  // Imports the Google Cloud client library
  const Speech = require('@google-cloud/speech');

  // Instantiates a client
  const speech = Speech({
      projectId: projectId,
      keyFilename: keyFilename
  });

  const request = {
    config: {
      encoding: 'LINEAR16',
      sampleRateHertz: 16000,
      languageCode: 'en-US'
    },
    interimResults: false // If you want interim results, set this to true
  };

  // Create a recognize stream
  const recognizeStream = speech.createRecognizeStream(request)
    .on('error', console.error)
    .on('data', (data) => process.stdout.write(data.results));

  // Start recording and send the microphone input to the Speech API
  record
    .start({
      sampleRateHertz: 16000,
      threshold: 0,
      // Other options, see https://www.npmjs.com/package/node-record-lpcm16#options
      verbose: false,
      recordProgram: 'sox', // Try also "arecord" or "sox"
      silence: '10.0'
    })
    .on('error', console.error)
    .pipe(recognizeStream);

  console.log('Listening, press Ctrl+C to stop.');
  // [END speech_streaming_mic_recognize]

whenever I try to run this program from command line using node, it just displays
"Listening, press Ctrl+C to stop." and then ends immediately, not allowing me to record and output my speech.

Does anyone know how to solve this issue?
Or am I doing something wrong?

Help would be greatly appreciated!!
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    🚨This issue needs some love.triage meI really want to be triaged.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions