Created
January 10, 2018 15:10
-
-
Save coord-e/9ec00e8941a5cca445355f99fb494f60 to your computer and use it in GitHub Desktop.
Google Speech API Streaming Node.js Working Example
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
const record = require('node-record-lpcm16'); | |
// Imports the Google Cloud client library | |
const Speech = require('@google-cloud/speech'); | |
// Your Google Cloud Platform project ID | |
const projectId = 'projectId'; | |
// // Creates a client | |
const speech = new Speech.SpeechClient({ | |
projectId: projectId, | |
}); | |
// The encoding of the audio file, e.g. 'LINEAR16' | |
const encoding = 'LINEAR16'; | |
// The sample rate of the audio file in hertz, e.g. 16000 | |
const sampleRateHertz = 16000; | |
// The BCP-47 language code to use, e.g. 'en-US' | |
const languageCode = 'ja-JP'; | |
const request = { | |
config: { | |
encoding: encoding, | |
sampleRateHertz: sampleRateHertz, | |
languageCode: languageCode | |
}, | |
interimResults: false // If you want interim results, set this to true | |
}; | |
// Create a recognize stream | |
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`)); | |
// Start recording and send the microphone input to the Speech API | |
record | |
.start({ | |
sampleRateHertz: sampleRateHertz, | |
threshold: 0, | |
// Other options, see https://www.npmjs.com/package/node-record-lpcm16#options | |
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.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment