Add requiredInputBufferSize as an input argument of createAudioRecord.

PiperOrigin-RevId: 515374407
This commit is contained in:
Jiuqiang Tang 2023-03-09 10:27:43 -08:00 committed by Copybara-Service
parent 563b193bca
commit 5bd6a7082a

View File

@ -164,12 +164,14 @@ public class BaseAudioTaskApi implements AutoCloseable {
* *
* @param numChannels the number of audio channels. * @param numChannels the number of audio channels.
* @param sampleRate the audio sample rate. * @param sampleRate the audio sample rate.
* @param requiredInputBufferSize the required input buffer size in number of float elements.
* @return an {@link android.media.AudioRecord} instance in {@link * @return an {@link android.media.AudioRecord} instance in {@link
* android.media.AudioRecord#STATE_INITIALIZED} * android.media.AudioRecord#STATE_INITIALIZED}
* @throws IllegalArgumentException if the model required channel count is unsupported * @throws IllegalArgumentException if the model required channel count is unsupported
* @throws IllegalStateException if AudioRecord instance failed to initialize * @throws IllegalStateException if AudioRecord instance failed to initialize
*/ */
public static AudioRecord createAudioRecord(int numChannels, int sampleRate) { public static AudioRecord createAudioRecord(
int numChannels, int sampleRate, int requiredInputBufferSize) {
int channelConfig = 0; int channelConfig = 0;
switch (numChannels) { switch (numChannels) {
case 1: case 1:
@ -190,6 +192,11 @@ public class BaseAudioTaskApi implements AutoCloseable {
throw new IllegalStateException( throw new IllegalStateException(
String.format("AudioRecord.getMinBufferSize failed. Returned: %d", bufferSizeInBytes)); String.format("AudioRecord.getMinBufferSize failed. Returned: %d", bufferSizeInBytes));
} }
int bufferSizeMultiplier = 2;
int modelRequiredBufferSize = requiredInputBufferSize * Float.BYTES * bufferSizeMultiplier;
if (bufferSizeInBytes < modelRequiredBufferSize) {
bufferSizeInBytes = modelRequiredBufferSize;
}
AudioRecord audioRecord = AudioRecord audioRecord =
new AudioRecord( new AudioRecord(
// including MIC, UNPROCESSED, and CAMCORDER. // including MIC, UNPROCESSED, and CAMCORDER.
@ -217,6 +224,6 @@ public class BaseAudioTaskApi implements AutoCloseable {
*/ */
public static AudioRecord createAudioRecord() { public static AudioRecord createAudioRecord() {
// TODO: Support creating AudioRecord based on the model specifications. // TODO: Support creating AudioRecord based on the model specifications.
return createAudioRecord(1, 16000); return createAudioRecord(1, 16000, 16000);
} }
} }