I am trying to implement speech recognition using Google speech api.
I record a raw audio on my web page and then send the raw file to server where it is trying to recognize by using Google's speech api.
Here is my angular code
start() {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
this.mediaRecorder = new MediaRecorder(stream);
this.mediaRecorder.start();
const audioChunks = [];
this.mediaRecorder.addEventListener("dataavailable", event => {
audioChunks.push(event.data);
});
this.mediaRecorder.addEventListener("stop", () => {
const audioBlob = new Blob(audioChunks, { type: "audio/wav"});
const audioUrl = URL.createObjectURL(audioBlob);
this.fileService.saveFile(audioBlob).subscribe(res => { console.log(res); });
const audio = new Audio(audioUrl);
audio.play();
});
});
}
Here is my C# controller code'
private string GetSpeechToText()
{
string result = string.Empty;
try
{
SpeechClient speechClient = SpeechClient.Create();
RecognitionConfig config = new RecognitionConfig()
{
Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
SampleRateHertz = 16000,
LanguageCode = "en-US",
};
RecognizeResponse response = speechClient.Recognize(config,
RecognitionAudio.FromFile(fileName));
foreach (var responseResult in response.Results)
{
foreach (var alternative in responseResult.Alternatives)
{
result += alternative.Transcript;
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
return result;
}
return result;
}
The audio plays fine both on client side and the server side file after getting written to file.
I have included the GOOGLE_APPLICATION_CREDENTIALS path correctly.
But I am getting empty response. Please tell me if I am missing something