diff options
| author | 2013-12-20 15:37:15 +0000 | |
|---|---|---|
| committer | 2013-12-20 15:37:16 +0000 | |
| commit | 44e26f2f5c0761f8211a3baef3f4a2fb1ea5f622 (patch) | |
| tree | 884e4a96dc452945426e3c6987d529937d5b87f7 | |
| parent | 3bc26143dc81267beba26e50965b4835cf73fdf8 (diff) | |
| parent | a5a42384367230b8e16cf441d5dcf4225332f9d7 (diff) | |
Merge "Prepare for additional formats beyond 8-bit and 16-bit PCM"
| -rw-r--r-- | core/jni/android_media_AudioRecord.cpp | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/core/jni/android_media_AudioRecord.cpp b/core/jni/android_media_AudioRecord.cpp index 03f94b79153e..aae20e0f1352 100644 --- a/core/jni/android_media_AudioRecord.cpp +++ b/core/jni/android_media_AudioRecord.cpp @@ -81,6 +81,17 @@ jint android_media_translateRecorderErrorCode(int code) { } } +static audio_format_t audioFormatToNative(int audioFormat) +{ + switch (audioFormat) { + case ENCODING_PCM_16BIT: + return AUDIO_FORMAT_PCM_16_BIT; + case ENCODING_PCM_8BIT: + return AUDIO_FORMAT_PCM_8_BIT; + default: + return AUDIO_FORMAT_INVALID; + } +} // ---------------------------------------------------------------------------- static void recorderCallback(int event, void* user, void *info) { @@ -173,21 +184,19 @@ android_media_AudioRecord_setup(JNIEnv *env, jobject thiz, jobject weak_this, uint32_t nbChannels = popcount(channelMask); // compare the format against the Java constants - if ((audioFormat != ENCODING_PCM_16BIT) - && (audioFormat != ENCODING_PCM_8BIT)) { + audio_format_t format = audioFormatToNative(audioFormat); + if (format == AUDIO_FORMAT_INVALID) { ALOGE("Error creating AudioRecord: unsupported audio format %d.", audioFormat); return AUDIORECORD_ERROR_SETUP_INVALIDFORMAT; } - int bytesPerSample = audioFormat == ENCODING_PCM_16BIT ? 2 : 1; - audio_format_t format = audioFormat == ENCODING_PCM_16BIT ? - AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT; + size_t bytesPerSample = audio_bytes_per_sample(format); if (buffSizeInBytes == 0) { ALOGE("Error creating AudioRecord: frameCount is 0."); return AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT; } - int frameSize = nbChannels * bytesPerSample; + size_t frameSize = nbChannels * bytesPerSample; size_t frameCount = buffSizeInBytes / frameSize; if ((uint32_t(source) >= AUDIO_SOURCE_CNT) && (uint32_t(source) != AUDIO_SOURCE_HOTWORD)) { @@ -513,10 +522,10 @@ static jint android_media_AudioRecord_get_min_buff_size(JNIEnv *env, jobject th sampleRateInHertz, nbChannels, audioFormat); size_t frameCount = 0; + audio_format_t format = audioFormatToNative(audioFormat); status_t result = AudioRecord::getMinFrameCount(&frameCount, sampleRateInHertz, - (audioFormat == ENCODING_PCM_16BIT ? - AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT), + format, audio_channel_in_mask_from_count(nbChannels)); if (result == BAD_VALUE) { @@ -525,7 +534,7 @@ static jint android_media_AudioRecord_get_min_buff_size(JNIEnv *env, jobject th if (result != NO_ERROR) { return -1; } - return frameCount * nbChannels * (audioFormat == ENCODING_PCM_16BIT ? 2 : 1); + return frameCount * nbChannels * audio_bytes_per_sample(format); } |