No matter what I do, I can't get the Android AudioRecord class to work with Pcm-Float.
Here is my code:
public AndroidAudioDriver(Sensor sensor, int desiredSampleRate)
{
_sensor = sensor;
if (!InitialiseSampleRate(desiredSampleRate))
{
throw new NotSupportedException("No supported audio sample rates found");
}
recorder = new AudioRecord(AudioSource.Mic, SampleRate, ChannelIn.Mono, Encoding.PcmFloat, _bufferSize);
_recorder.StartRecording();
var res = _recorder.Read(_audioData, 0, (int)(SampleRate * _readPortion));
}
private bool InitialiseSampleRate(int rate)
{
for (int i = rate; i < 48000; i += 100)
{ // add the rates you wish to check against
_bufferSize = AudioRecord.GetMinBufferSize(i, ChannelIn.Mono, Encoding.Pcm16bit) * 10;
if (_bufferSize > 0)
{
_audioData = new byte[_bufferSize];
SampleRate = i;
return true;
}
}
return false;
}
_recorder.read always returns -3, which according to the documentation is "ERROR_INVALID_OPERATION". The same code works just fine with Pcm-16bit. I don't know what the invalid operation is, and there aren't any errors in logcat that I can see.
Thanks.