当前位置:网站首页>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);
}
}
}
}
边栏推荐
- VB 学习笔记
- 这个国庆!腾讯云WeCity陪您一同出行,点亮城市地标...
- Introduction to bi-sql wildcards
- 腾讯完成全面上云 打造国内最大云原生实践
- 归并排序求逆序数
- Bi-sql delete
- ImageView shows network pictures
- 归并排序模板 & 理解
- Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
- Première application de l'informatique quantique à la modélisation des flux de puissance dans les systèmes énergétiques à l'Université technique danoise
猜你喜欢

AutoCAD - two extension modes

Bi-sql - join

Rich text tables, lists, pictures

2022 crane driver (limited to bridge crane) examination question bank simulated examination platform operation

利用 Redis 的 sorted set 做每周热评的功能

Text editor for QT project practice - Episode 12

Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse

Syntax highlighting of rich text

Add information on the left and add parts on the right of the status bar

汇编语言(4)函数传参
随机推荐
Scala trait inheritance class
Deep learning LSTM model for stock analysis and prediction
Bi skill - judge 0 and null
MySQL common basic statements (collation)
腾讯云WeCity丨产业联合 协同创新 共贺新春!
ContentResolver,拿到手机短信内容
php easywechat 和 小程序 实现 长久订阅消息推送
Introduction to smart contract security audit delegatecall (2)
【实用系列】家内wifi全覆盖
Tencent cloud wecity Hello 2022!
Properties of DOM
Picture rotation move zoom gradient
重磅:国产IDE发布,由阿里研发,完全开源!(高性能+高定制性)
yasea apk 下载 镜像
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)
Bi-sql index
汇编语言(2)基础知识-debug
1. 封装自己的脚手架 2.创建代码模块
天书夜读笔记——8.4 diskperf反汇编
JVM指令