当前位置:网站首页>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 for QT project practice -- Episode 9
- Basic knowledge of assembly language (2) -debug
- LLVM TargetPassConfig
- Deploy a production cluster using Loki microservice pattern
- 4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
- Scala template method pattern
- Ideas and examples of divide and conquer
- 【直播回顾】2022腾讯云未来社区城市运营方招募会暨SaaS 2.0新品发布会!
- Yasea APK Download Image
- Preliminary understanding of qtoolbutton
猜你喜欢

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

AutoCAD - two extension modes
The latest QQ wechat domain name anti red PHP program source code + forced jump to open

Bi-sql create

Scala IO read by line

Preliminary understanding of qtoolbutton

4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?

Syntax highlighting of rich text

2022 simulated 100 questions of safety officer-c certificate examination and online simulated examination

Text editor for QT project practice - Episode 12
随机推荐
Golang example renewal lock: redis+channel+sync Mutex
Library management system code source code (php+css+js+mysql) complete code source code
Bi-sql Union
VB 学习笔记
丹麦技术大学首创将量子计算应用于能源系统潮流建模
PHP 利用getid3 获取mp3、mp4、wav等媒体文件时长等数据
Tencent cloud wecity Hello 2022!
Ecological escort cloud service providers wave "Intel flag"
Bi-sql select into
汇编语言(2)基础知识-debug
Tencent cloud wecity Industry joint collaborative innovation to celebrate the New Year!
天书夜读笔记——深入虚函数virtual
腾讯云WeCity解决方案
Powerbi - for you who are learning
Properties of DOM
Program.launch(xxx)打开文件
Text editor for QT project practice -- Episode 9
Scala IO read by line
MySQL multi condition matching fuzzy query
How to store dataframe data in pandas into MySQL