I am trying to use an InputAudioStream from AudioRecord to use in the Azure Microsoft CognitiveServices KeywordRecognizer in .NET Maui for Android.
I have created a Class SpeechAndroidAudioRecorder:
public class SpeechAndroidAudioRecorder : PullAudioInputStreamCallback
{
private AudioRecord microphone;
public void StartRecording()
{
int sampleRate = 44100;
ChannelIn channelConfig = ChannelIn.Mono;
Encoding audioFormat = Encoding.Pcm16bit;
int minBufferSize = AudioRecord.GetMinBufferSize(sampleRate, channelConfig, audioFormat) * 10;
microphone = new AudioRecord(AudioSource.Mic, sampleRate, channelConfig, audioFormat, minBufferSize);
microphone.StartRecording();
}
public override int Read(byte[] dataBuffer, uint size)
{
int bytesRead = microphone.Read(dataBuffer, 0, checked((int)size));
return bytesRead;
}
The class initializes the recorder and extends PullAudioInputStreamCallback to Override the read function. With that I can use it for the .FromStreamInput for the KeywordRecognizer:
SpeechAndroidAudioRecorder audioRecorder = new SpeechAndroidAudioRecorder();
audioRecorder.StartRecording();
AudioStreamFormat audioStreamFormat = AudioStreamFormat.GetWaveFormatPCM(44100, 16, 1);
AudioConfig audioConfig = AudioConfig.FromStreamInput(audioRecorder, audioStreamFormat);
_keywordRecognizer = new KeywordRecognizer(audioConfig);
The AudioRecord is initialized and when I check for the bytesRead it reads about 8000 bytes every call and the dataBuffer is filled with values other than 0. The KeywordRecognizer is also initialized and is waiting for the keyword with this call:
KeywordRecognitionResult result = await _keywordRecognizer.RecognizeOnceAsync(_recognitionModel);
But when I say the keyword it is not recognizing it. Tried it on the emulator and on a physical device.