I tried to use google cloud speech to text in my node.js project. I followed he quick startup guides from docs. I pass a audio file in wav and flac format but in both cases I receive an error: error TypeError: .google.cloud.speech.v1.RecognizeRequest.audio: object expected
try {
const speech = require('@google-cloud/speech');
const client = new speech.SpeechClient();
const filename = require('path').join(__dirname + '/../temp/test.wav');
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US'
};
const audio = fs.readFileSync(filename).toString('base64');
const request = {
config: config,
audio: audio
};
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
as you can see request is an object and when I consoled.log it it is properly converted toString. But the function client.recognize doesn't work. Where do you think is the problem?
The audio
property in the request
needs to be an object with the audioBytes as a property called content
, like in the example below:
// Reads a local audio file and converts it to base64
const audioBytes = fs.readFileSync(fileName).toString('base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const request = {
audio: audio,
config: config,
};