当前位置:网站首页>Audio PCM data calculates sound decibel value to realize simple VAD function
Audio PCM data calculates sound decibel value to realize simple VAD function
2022-06-25 01:17:00 【Songyulong's blog】
Calculate audio data PCM Decibel value , Calculation formula :
L p = 20 ∗ L o g 10 ( P r m s / P r e f ) d B L_p=20*Log_{10}(Prms/Pref)dB Lp=20∗Log10(Prms/Pref)dB
P r m s Prms Prms: Current sound amplitude value ;
P r e f Pref Pref: The maximum amplitude of sound ( namely PCM The maximum value represented by the data );
about 16bitsPCM data , A sound sampling point is 2 Maximum bytes ( 2 16 − 1 = 65535 2^{16}-1=65535 216−1=65535)
We are right. n Sum the data of sampling points and then take the average value , As P r m s / P r e f Prms/Pref Prms/Pref
/** * @brief obtain PCM Data decibel value * * @param pcm : pcm Data pointer * @param len : pcm Data length * @return int : Decibel value = 20*log10(pcm Data average ) * @note Lp = 20*log10(Prms/Pref)dB | Lp: Calculate the audio decibel value of the result , Prms: Current sound amplitude value ,Pref: The maximum amplitude of sound (16bit=65535), 16Bit Maximum decibel value =20*log10(65535)=96.32(dB). * */
static int komijbox_sound_dB(const uint8_t *pcm, int len)
{
int sum = 0;
int dB = 0;
short tmp = 0;
short* pcmaddr = (short*)pcm;
for (int i=0; i<len; i+=2) {
memcpy(&tmp, pcmaddr+i, sizeof(short)); // obtain 2 byte PCM data
sum += abs(tmp); // Sum and accumulate absolute values
}
sum = sum / (len /2); // seek PCM The average of the data ,2 Bytes represent a 16Bit PCM Sampled data
if (sum) {
dB = (int)(20*log10(sum));
}
return dB;
}
It is simple to realize through audio decibel value VAD function :
#define D_VAD_VALID_dB_Max 75 // VAD Effective decibel value Maximum Greater than this value is considered VAD It works
#define D_VAD_VALID_dB_Min 58 // VAD Invalid DB value minimum value Less than this value is considered VAD Invalid
#define D_VAD_VALID_UP_COUNT 2 // VAD Pull up count ( Continuous valid value count ), Greater than this value triggers VAD_UP event
#define D_VAD_VALID_DOWN_COUNT 30 // VAD Pull up count ( Continuous invalid value count ), Greater than this value triggers VAD_DOWN event
/** * @brief VAD State type * */
enum {
E_VAD_STATUS_NONE = 0,
E_VAD_STATUS_UP, // Up
E_VAD_STATUS_LISTENING, // Listening
E_VAD_STATUS_DOWN // Down
};
typedef void (*vad_callback)(int vad_status);
struct __vad_t{
int status;
int up_count;
int down_count;
vad_callback up_cb;
vad_callback down_cb;
vad_callback listening_cb;
};
// TODO: Calculate audio PCM Data audio decibel value
int dB = komijbox_sound_dB((uint8_t*)data, len);
// printf("pcmdB:%d\n", dB);
// TODO: Judge by audio decibel value VAD state , Realize the function of voice control switch
if (dB >= D_VAD_VALID_dB_Max) {
__this->vad.up_count += 1;
__this->vad.down_count = 0;
if (__this->vad.up_count >= D_VAD_VALID_UP_COUNT) {
// __this->vad.down_count = 0;
if (__this->vad.status != E_VAD_STATUS_UP) {
__this->vad.status = E_VAD_STATUS_UP;
// TODO: VAD UP
printf("=============== VAD Up\n");
if (__this->vad.up_cb != NULL) {
__this->vad.up_cb(__this->vad.status);
}
} else {
// TODO: VAD LISTENING // listening
printf("=============== VAD Listening\n");
}
}
} else if (dB <= D_VAD_VALID_dB_Min) {
__this->vad.down_count += 1;
__this->vad.up_count = 0;
if (__this->vad.down_count >= D_VAD_VALID_DOWN_COUNT) {
// __this->vad.up_count = 0;
if (__this->vad.status != E_VAD_STATUS_DOWN) {
__this->vad.status = E_VAD_STATUS_DOWN;
// TODO: VAD DOWN
printf("=============== VAD Down\n");
if (__this->vad.down_cb != NULL) {
__this->vad.down_cb(__this->vad.status);
}
}
}
}
边栏推荐
- Text editor of QT project practice ---------- episode 11
- Library management system code source code (php+css+js+mysql) complete code source code
- Scala classes inherit multiple attributes
- 天书夜读笔记——反汇编引擎xde32
- Lenovo tongfuyao: 11 times the general trend, we attacked the city and pulled out the stronghold all the way
- activity生命周期
- Distinguish between i++ and ++i seconds
- Scala IO read by character
- 归并排序求逆序数
- 4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
猜你喜欢

汇编语言(2)基础知识-debug

Text editor for QT project practice - Episode 12

How to store dataframe data in pandas into MySQL

Bi-sql - join

4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?

Text editor for QT project practice - Episode 10

程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?

AutoCAD - two extension modes

Bi skill - judge 0 and null

Using macro code to generate handwriting automatically in word or WPS
随机推荐
This national day! Tencent cloud wecity will accompany you to travel and light up the city landmark
Scala trait construction mechanism
Convolution and transpose convolution
汇编语言(2)基础知识-debug
Bi-sql - join
卷积与反卷积关系超详细说明及推导(反卷积又称转置卷积、分数步长卷积)
扎克伯格上手演示四款VR头显原型机,Meta透露元宇宙「家底」
Tencent cloud wecity Industry joint collaborative innovation to celebrate the New Year!
Yasea APK Download Image
activity生命周期
4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?
Powerbi - for you who are learning
Text editor for QT project practice -- Episode 9
天书夜读笔记——反汇编引擎xde32
汇编语言(3)16位汇编基础框架与加减循环
Library management system code source code (php+css+js+mysql) complete code source code
天书夜读笔记——深入虚函数virtual
Tencent moved!
Introduction to smart contract security audit delegatecall (2)
[practical series] full WiFi coverage at home