Detailed explanation of real-time volume decibel value in Android

Basic knowledge

The most familiar unit for measuring sound intensity is decibel (DB). This is a dimensionless relative unit. The calculation formula is as follows:

The numerator is the sound pressure of the measured value, and the denominator is the sound pressure of the reference value (20 MPa, the minimum sound pressure that can be heard by humans). Therefore, when it comes to the sound intensity in daily life, a small reference value is defaulted.

The physical quantity that Android device sensors can provide is the amplitude of the field. The decibel value is calculated by the following formula:

After reading the amplitude of a section of audio data from the SDK, take the maximum amplitude or average amplitude (which can be averaged by the sum of squares or the sum of absolute values) and replace it into A1 of the above formula.

Now the question is, what is the amplitude A0 as the reference value?

Bloggers check many posts and blogs. This is the most paste place. Some blog posts take 600, which is based on the assumption that the amplitude of the apparent noise is 600. At this time, the calculated value is the DB value relative to the background noise. If the user does not make sound from the microphone, the calculated value is basically 0 dB. The background noise in the user's actual use scenario varies greatly. It would be wrong if we also follow the gourd painting. Especially for those who need to make an absolute decibel meter, we should find out the amplitude corresponding to the sound pressure value of 20 MPa (or take a standard decibel meter as a calibration reference).

Bloggers are lazy and set A0 as 1, which is the minimum sound amplitude that can be "heard" by the microphone of Android device. In this way, the measured amplitude can be directly substituted into A1 of the second formula to calculate the decibel value.

Android API needs to apply for corresponding permission in androidmanifest.xml to use microphone:

MediaRecorder:

The object initialization of this class is troublesome because it is designed to record a complete piece of audio and write it to the file system. However, it is convenient to obtain the amplitude after initialization. We can directly use its nonparametric method getmaxamplitude to obtain the maximum amplitude in the sound source data in a short period of time. However, the possible disadvantage of taking the maximum value is that it will be affected by extreme data, which makes the later calculated DB value fluctuate greatly. However, this method is used by many recording applications to calculate the volume level.

This method returns a 16 bit integer in the range of 0 to 32767. The principle may be to take the value with the largest absolute value from a piece of sound source data with a value range of - 32768 to 32767 and return it. This value is a linear function of the sound pressure in Pascal. In addition, it should be noted that the value obtained by calling this method for the first time is 0, and the DB value calculated in the formula is negative infinity, so it is necessary to judge this situation in the code. It can be calculated that since the maximum value returned by getmaxamplitude is 32767, the calculated maximum DB value is 90.3. In other words, the blogger ordered the reference amplitude value to be 1, and the normal range of the calculated DB value is 0 dB to 90.3 dB.

The demo code is as follows. Rewrite the code based on hongfa.yy:

AudioRecord:

This class can obtain specific sound source data values. Use the read (byte [] audiodata, int offsetinbytes, int sizeinbytes) method to read a piece of audio source data from the buffer to the incoming byte array audiodata, and then you can operate on it, such as finding the sum of squares or the average of absolute values. This can avoid the influence of individual extreme values and make the calculation results more stable. After obtaining the average value, if it is the sum of squares, it is substituted into the formula with constant coefficient of 10. If it is an absolute value, it is substituted into the formula with constant coefficient of 20 to calculate the DB value.

The demonstration code is as follows:

According to the measured results (equipment Xiaomi 2S), mediarecorderdemo fluctuates greatly. As long as you blow air into the microphone, the decibel value can be up to 90:

The audiorecorddemo is very stable, and it is difficult to blow hard to above 88:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>