Skip to content

Instantly share code, notes, and snippets.

@fzero
Last active January 9, 2023 16:25
Show Gist options
  • Save fzero/6cee4aacb2e9ef429461703b99cfd07d to your computer and use it in GitHub Desktop.
Save fzero/6cee4aacb2e9ef429461703b99cfd07d to your computer and use it in GitHub Desktop.
Google Voice Recognition API example
// We've followed documentation links and played with the example code a bit:
// Start here: https://cloud.google.com/speech
// If you need an audio editor, try Audacity:
// https://www.audacityteam.org
//
// usage: node voice-recog.js soundfile.flac en-US
//
// The second argument is the language code (e.g. en-US, pt-BR, fr-CA...)
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
const fs = require('fs');
// Your Google Cloud Platform project ID
// You'll get this information and more once you register an application
// and install the Google Cloud Platform command line tools.
const projectId = 'your-project-id-here';
// Creates a client
const client = new speech.SpeechClient({
projectId: projectId,
});
// The name of the audio file to transcribe
const fileName = process.argv[2];
const lang = process.argv[3] || 'en-US';
// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'FLAC',
sampleRateHertz: 44100,
languageCode: lang,
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
client
.recognize(request)
.then((data) => { // Looking at the response object
console.log('Full response:', data[0].results[0].alternatives)
return data
})
.then(data => { // Actual response processing
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
})
.catch(err => {
console.error('ERROR:', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment