Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.