Reputation: 857
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.
Upvotes: 3
Views: 1469
Reputation: 857
To use PcmFloat with AudioRecord, you must use the overload :
AudioRecord.Read(float[], offset, count, readType)
.
The issue was I was passing it a byte[], which appears to only work with Pcm8bit and Pcm16bit.
Upvotes: 4
Reputation: 74174
AudioRecord as of API M and AudioTrack as of API LOLLIPOP support ENCODING_PCM_FLOAT.
So on your API-21 / API-22 devices you will not have PcmFloat
available
API-23 (Marshmallow) added support for PcmFloat
for AudioRecord
.
Upvotes: 1