Skip to content

Instantly share code, notes, and snippets.

@mpj
Created October 8, 2018 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpj/4dba4f6859084e390317e4147b2b0021 to your computer and use it in GitHub Desktop.
Save mpj/4dba4f6859084e390317e4147b2b0021 to your computer and use it in GitHub Desktop.
Most simple example I could find for ongoing listening of microphone using the google cloud speech api.
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech')
const record = require('node-record-lpcm16')
const client = new speech.SpeechClient({
// generated service key downloaded from cloud console
credentials: require('./admin-key.json')
})
const sampleRateHertz = 16000
record
.start({
sampleRateHertz: sampleRateHertz,
threshold: 0,
verbose: false,
recordProgram: 'rec'
// Other options, see https://www.npmjs.com/package/node-record-lpcm16#options
})
.on('error', console.error)
.pipe(client
.streamingRecognize({
interimResults: true,
config: {
encoding: 'LINEAR16',
sampleRateHertz,
languageCode: 'sv-SE',
},
})
.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`
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment