Skip to content
Browse files

alsa_sound: Add support for flexible buffer size for recording

- In the current implementation, all the read calls to the driver
  are limited to 320 bytes only. This results performance overhead
  for recording at higher sampling rates.
- Added support for flexible buffer size to allow upto 4096 bytes.

Bug: 7223456
Change-Id: Ic0522d92de905b04481a0d8daa103c77552257e8
Signed-off-by: Iliyan Malchev <malchev@google.com>
  • Loading branch information...
1 parent c669c27 commit 77780382b45794eb5bc0e8589d9b7c96bb406772 SathishKumar Mani committed with Iliyan Malchev Sep 21, 2012
Showing with 23 additions and 31 deletions.
  1. +18 −28 alsa_sound/AudioHardwareALSA.cpp
  2. +4 −1 alsa_sound/AudioHardwareALSA.h
  3. +1 −2 alsa_sound/alsa_default.cpp
View
46 alsa_sound/AudioHardwareALSA.cpp
@@ -848,7 +848,7 @@ AudioHardwareALSA::openOutputStream(uint32_t devices,
} else {
ALOGD("openOutputStream: Lowlatency Output");
alsa_handle.bufferSize = PLAYBACK_LOW_LATENCY_BUFFER_SIZE;
- alsa_handle.latency = PLAYBACK_LOW_LATENCY;
+ alsa_handle.latency = PLAYBACK_LOW_LATENCY_MEASURED;
if ((use_case == NULL) || (!strcmp(use_case, SND_USE_CASE_VERB_INACTIVE))) {
strlcpy(alsa_handle.useCase, SND_USE_CASE_VERB_HIFI_LOWLATENCY_MUSIC, sizeof(alsa_handle.useCase));
} else {
@@ -1114,7 +1114,7 @@ AudioHardwareALSA::openInputStream(uint32_t devices,
return in;
} else {
alsa_handle_t alsa_handle;
- unsigned long bufferSize = DEFAULT_IN_BUFFER_SIZE;
+ unsigned long bufferSize = MIN_CAPTURE_BUFFER_SIZE_PER_CH;
alsa_handle.module = mALSADevice;
alsa_handle.bufferSize = bufferSize;
@@ -1266,15 +1266,11 @@ AudioHardwareALSA::openInputStream(uint32_t devices,
if(sampleRate) {
it->sampleRate = *sampleRate;
}
-#ifdef QCOM_SSR_ENABLED
- if (6 == it->channels) {
- if (!strncmp(it->useCase, SND_USE_CASE_VERB_HIFI_REC, strlen(SND_USE_CASE_VERB_HIFI_REC))
- || !strncmp(it->useCase, SND_USE_CASE_MOD_CAPTURE_MUSIC, strlen(SND_USE_CASE_MOD_CAPTURE_MUSIC))) {
- ALOGV("OpenInoutStream: Use larger buffer size for 5.1(%s) recording ", it->useCase);
- it->bufferSize = getInputBufferSize(it->sampleRate,*format,it->channels);
- }
+ if (!strncmp(it->useCase, SND_USE_CASE_VERB_HIFI_REC, strlen(SND_USE_CASE_VERB_HIFI_REC))
+ || !strncmp(it->useCase, SND_USE_CASE_MOD_CAPTURE_MUSIC, strlen(SND_USE_CASE_MOD_CAPTURE_MUSIC))) {
+ ALOGV("OpenInoutStream: Use larger buffer size for 5.1(%s) recording ", it->useCase);
+ it->bufferSize = getInputBufferSize(it->sampleRate,*format,it->channels);
}
-#endif
err = mALSADevice->open(&(*it));
if (err) {
ALOGE("Error opening pcm input device");
@@ -1338,25 +1334,19 @@ status_t AudioHardwareALSA::dump(int fd, const Vector<String16>& args)
size_t AudioHardwareALSA::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
{
- size_t bufferSize;
- if (format != AudioSystem::PCM_16_BIT
- && format != AudioSystem::AMR_NB
- && format != AudioSystem::AMR_WB
-#ifdef QCOM_QCHAT_ENABLED
- && format != AudioSystem::EVRC
- && format != AudioSystem::EVRCB
- && format != AudioSystem::EVRCWB
-#endif
- ) {
- ALOGW("getInputBufferSize bad format: %d", format);
- return 0;
- }
- if(sampleRate == 16000) {
- bufferSize = DEFAULT_IN_BUFFER_SIZE * 2 * channelCount;
- } else if(sampleRate < 44100) {
- bufferSize = DEFAULT_IN_BUFFER_SIZE * channelCount;
+ size_t bufferSize = 0;
+ if (format == AudioSystem::PCM_16_BIT) {
+ if(sampleRate == 8000 || sampleRate == 16000 || sampleRate == 32000) {
+ bufferSize = (sampleRate * channelCount * 20 * sizeof(int16_t)) / 1000;
+ } else if (sampleRate == 11025 || sampleRate == 12000) {
+ bufferSize = 256 * sizeof(int16_t) * channelCount;
+ } else if (sampleRate == 22050 || sampleRate == 24000) {
+ bufferSize = 512 * sizeof(int16_t) * channelCount;
+ } else if (sampleRate == 44100 || sampleRate == 48000) {
+ bufferSize = 1024 * sizeof(int16_t) * channelCount;
+ }
} else {
- bufferSize = DEFAULT_IN_BUFFER_SIZE * 12;
+ ALOGE("getInputBufferSize bad format: %d", format);
}
return bufferSize;
}
View
5 alsa_sound/AudioHardwareALSA.h
@@ -65,8 +65,11 @@ class AudioHardwareALSA;
#define DEFAULT_BUFFER_SIZE 4096
#define DEFAULT_VOICE_BUFFER_SIZE 2048
#define PLAYBACK_LOW_LATENCY_BUFFER_SIZE 1024
-#define PLAYBACK_LOW_LATENCY 11000
+#define PLAYBACK_LOW_LATENCY 22000
+#define PLAYBACK_LOW_LATENCY_MEASURED 42000
#define DEFAULT_IN_BUFFER_SIZE 320
+#define MIN_CAPTURE_BUFFER_SIZE_PER_CH 320
+#define MAX_CAPTURE_BUFFER_SIZE_PER_CH 2048
#define FM_BUFFER_SIZE 1024
#define VOIP_SAMPLING_RATE_8K 8000
View
3 alsa_sound/alsa_default.cpp
@@ -252,7 +252,6 @@ status_t setHardwareParams(alsa_handle_t *handle)
|| !strncmp(handle->useCase, SND_USE_CASE_MOD_CAPTURE_MUSIC, strlen(SND_USE_CASE_MOD_CAPTURE_MUSIC))) {
ALOGV("HWParams: Use 4 channels in kernel for 5.1(%s) recording ", handle->useCase);
channels = 4;
- reqBuffSize = DEFAULT_IN_BUFFER_SIZE;
}
}
#endif
@@ -275,7 +274,7 @@ status_t setHardwareParams(alsa_handle_t *handle)
format);
param_set_mask(params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
SNDRV_PCM_SUBFORMAT_STD);
- param_set_min(params, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, reqBuffSize);
+ param_set_int(params, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, reqBuffSize);
param_set_int(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 16);
param_set_int(params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
channels * 16);

0 comments on commit 7778038

Please sign in to comment.
Something went wrong with that request. Please try again.