Analysis of the getminbuffersize function of audiorecord

Source: Internet
Author: User

Analysis of the getminbuffersize function of audiorecord

1. Java usage example
This method is a static method and can be called directly. The test case is as follows:

    int size = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);    Log.d("TEST", "size = " + size);

2. Call the Roadmap
(1) The getminbuffersize function of the audiorecord. Java File
Call the JNI function native_get_min_buff_size to obtain the buffer size.

(2) The native_get_min_buff_size function in the android_media_audiorecord.cpp file. The corresponding JNI function is the android_media_audiorecord_get_min_buff_size function.

Call the audiorecord: getminframecount function to obtain framecount, which is used to calculate the buffer size.

After obtaining framecount, calculate the final buffer size using the following formula.
Framecount * nbchannels * (audioformat = javaaudiorecordfields. pcm16? 2: 1 );
(3) The audiorecord: getminframecount function of the audiorecord. cpp file to obtain framecount
Then, the minimum buffer is calculated using the following formula:
Framecount * nbchannels * (audioformat = javaaudiorecordfields. pcm16? 2: 1 );
(4) After the audiorecord: getminframecount function calls the audiosystem: getinputbuffersize function of the audiosystem. cpp file to obtain the buffer size,
Calculate the minimum buffer using the following formula:

    // We double the size of input buffer for ping pong use of record buffer.    size <<= 1;    if (audio_is_linear_pcm(format)) {        size /= channelCount * audio_bytes_per_sample(format);    }    *frameCount = size;

(5) The audiosystem: getinputbuffersize function calls audioflinger's
Ginbuffsize = af-> getinputbuffersize (samplerate, format, channelcount );

(6) continue to call the getinputbuffersize method of audioflinger to obtain the buffer size.

size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, int format, int channelCount){    return mAudioHardware->getInputBufferSize(sampleRate, format, channelCount);}

Directly return the buffer value obtained from the hardware without any additional processing.
In the getinputbuffersize method of audioflinger, the actual hardware buffer size needs to be called according to the specific hardware implementation.
(7) Call the getinputbuffersize of the audiohardwarealsa class to obtain the buffer size.

The audiohardwarealsa class is located in the hardware directory. According to the implementation of different CPU vendors, this class is located in different directories, and the implementation of getinputbuffersize is also different. It needs to be implemented based on different recorder hardware.

The following is a specific code implementation. Different buffer sizes are returned Based on the sampling rate and number of sound channels.

size_t AudioHardwareALSA::getInputBufferSize(uint32_t sampleRate, int format, int channelCount){    size_t bufferSize;    if (format != AudioSystem::PCM_16_BIT) {        LOGW("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;    } else {        bufferSize = DEFAULT_IN_BUFFER_SIZE * 12;    }    return bufferSize;}

After the result is returned to the upper-level caller, it needs to be adjusted according to the actual situation. The final result value is the minimum buffer size to be obtained at the application layer, in bytes.

For example, the value obtained in my Huawei u8500 2.2 version is 4096 bytes.
The function call process is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.