Created
October 8, 2018 13:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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