Volume Calculation During Android and iOS recording

Source: Internet
Author: User

I am often asked how to calculate the volume during recording. The iOS platform has APIs that can be called directly, but there is no good method on the Android platform, so we have to calculate it by ourselves.


The demand for volume calculation is because many applications want to achieve some animation effects based on the volume. Therefore, based on this requirement, we only need to change the volume value obtained based on The Voice size, without having to overpeat the exact range. Because there are many calculation methods, the values calculated by different methods are certainly different, but as long as we can reflect the size change, our goal is achieved.


The following uses Android recording as an example to describe a calculation method. You can re-calculate the value based on your own needs, or perform some mathematical transformations on the calculated value to meet your own needs.


It should be noted in advance that there are similar calculation methods on the Internet, but they cannot be copied because they are related to the encoding of recordings and the types of recording data.


There are two types of recording encoding: 8-bit pcm and 16-bit pcm. 8-bit pcm uses one byte to represent a speech point, and 16-bit pcm uses two bytes, that is, a short to represent a speech point. Note that if you use 16-bit pcm encoding and the recording data is byte, You need to convert the two bytes into a short. Two bytes are converted into one short, which has two types: Small-end and big-end. Generally, the default value is small-end. However, some open-source libraries, such as lamemp3, need big-end, this should be handled according to different situations.


The following uses Android as an example to describe how to calculate the volume using the average value.


Private double calculateVolume (short [] buffer ){

Double sumVolume = 0.0;

Double avgVolume = 0.0;

Double volume = 0.0;

For (short B: buffer ){

SumVolume + = Math. abs (B );

}

AvgVolume = sumVolume/buffer. length;

Volume = Math. log10 (1 + avgVolume) * 10;

Return volume;

}


This method transmits data of the short type, so the recording encoding must be a 16-bit pcm, which can be calculated directly without conversion. I believe everyone has heard of the sound waves. Everyone uses the audio editing software Adobe audition to open a sound:


650) this. width = 650; "src =" http://img1.51cto.com/attachment/201307/101959596.png "title =" A254A574-ABBF-449A-A9F5-7D283CC24241.png "/>


From here we can see that the sound is fluctuating and fluctuating, with peaks and valleys. To put it bluntly, there is positive and negative. Therefore, when calculating, we need to first calculate the absolute value, or else we will offset up and down. After the absolute value is obtained, the value is accumulated and divided by the length of the entire data. Then, the average value of the Speech data is obtained.


However, the result calculated in this way is relatively large, which is not conducive to our use. Therefore, the logarithm is obtained and multiplied by 10:


Volume = Math. log10 (1 + avgVolume) * 10;


These operations can be performed based on your own needs. Here is a simple example.


Note that if the encoding of your recording is pcm and the recording data is byte, You need to convert two bytes into one short for processing, we recommend that you use a small-end method.


Private doublecalculateVolume (byte [] buffer ){

Double sumVolume = 0.0;

Double avgVolume = 0.0;

Double volume = 0.0;

For (int I = 0; I <buffer. length; I + = 2 ){

Int v1 = buffer [I] & 0xFF;

Int v2 = buffer [I + 1] & 0xFF;

Int temp = v1 + (v2 <8); // Small End

If (temp & gt; = 0x8000 ){

Temp = 0 xffff-temp;

}

SumVolume + = Math. abs (temp );

}

AvgVolume = sumVolume/buffer. length/2;

Volume = Math. log10 (1 + avgVolume) * 10;

Return volume;

}


It is not difficult to convert two bytes into a short shift operation using a small-end method. I will not explain it too much here.


The above is the method for calculating the volume by using the average value. However, in iOS, you do not need to calculate the volume. You can directly call the system api.


To obtain the recording volume for iOS, follow these steps:


1. Before starting the recording, set the attribute for obtaining the system volume.


// Set the attribute for obtaining the volume.

UInt32 enabledLevelMeter = true;

AudioQueueSetProperty (_ audioQueue, kAudioQueueProperty_EnableLevelMetering, & enabledLevelMeter, sizeof (UInt32 ));


2. Call the system api to obtain the volume. This method can be called in the buffer callback function, or you can use a timer to call it.


AudioQueueLevelMeterState levelMeter;

UInt32 levelMeterSize = sizeof (AudioQueueLevelMeterState );

AudioQueueGetProperty (_ audioQueue, kAudioQueueProperty_CurrentLevelMeterDB, & levelMeter, & levelMeterSize );

Double volume = levelMeter. mAveragePower


Of course, if you don't want to use the system's volume value, you can calculate it as you did above, depending on your needs.


Finally, I uploaded the android demo of obtaining the audio volume to the attachment. For details, refer.

This article is from the "mobile development" blog, please be sure to keep this source http://ikinglai.blog.51cto.com/6220785/1256781

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.