当前位置:网站首页>音频PCM数据计算声音分贝值,实现简单VAD功能
音频PCM数据计算声音分贝值,实现简单VAD功能
2022-06-24 20:36:00 【SongYuLong的博客】
计算音频数据PCM分贝值,计算公式:
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:当前声音振幅值;
P r e f Pref Pref:声音振幅最大值(即PCM数据表示的最大值);
对于16bitsPCM数据,一个声音采样点为2个字节最大( 2 16 − 1 = 65535 2^{16}-1=65535 216−1=65535)
我们对n个采样点数据求和然后取其平均值,作为 P r m s / P r e f Prms/Pref Prms/Pref
/** * @brief 获取PCM数据分贝值 * * @param pcm : pcm数据指针 * @param len : pcm数据长度 * @return int : 分贝值 = 20*log10(pcm数据平均值) * @note Lp = 20*log10(Prms/Pref)dB | Lp:计算结果音频分贝值, Prms:当前声音振幅值,Pref:声音振幅最大值(16bit=65535), 16Bit最大分贝值=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)); // 获取2字节PCM数据
sum += abs(tmp); // 绝对值求和累加
}
sum = sum / (len /2); // 求PCM数据的平均值,2个字节表示一个16Bit PCM采样数据
if (sum) {
dB = (int)(20*log10(sum));
}
return dB;
}
通过音频分贝值实现简单VAD功能:
#define D_VAD_VALID_dB_Max 75 // VAD 有效分贝值 最大值 大于此值认为VAD有效
#define D_VAD_VALID_dB_Min 58 // VAD 无效分贝值 最小值 小于此值认为VAD无效
#define D_VAD_VALID_UP_COUNT 2 // VAD 拉起计数(连续有效值计数),大于此值触发 VAD_UP事件
#define D_VAD_VALID_DOWN_COUNT 30 // VAD 拉起计数(连续无效值计数),大于此值触发 VAD_DOWN事件
/** * @brief VAD 状态类型 * */
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:计算音频PCM数据音频分贝值
int dB = komijbox_sound_dB((uint8_t*)data, len);
// printf("pcmdB:%d\n", dB);
// TODO:通过音频分贝值判断VAD状态,实现声控开关功能
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);
}
}
}
}
边栏推荐
- 用手机在同花顺上开户靠谱吗?这样炒股有没有什么安全隐患
- 2022 simulated 100 questions of safety officer-c certificate examination and online simulated examination
- 【实用系列】家内wifi全覆盖
- 使用 Loki 微服务模式部署生产集群
- 云开发技术峰会·公益编程挑战赛【火热报名中】!
- Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
- Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse
- I'd like to ask how to open an account at industrial securities? Is it safe to open a stock account through the link
- 丹麥技術大學首創將量子計算應用於能源系統潮流建模
- Scala IO read by line
猜你喜欢

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

51单片机多机通信

Bi-sql between

Mobile security tool jar

Cobalt Strike安装教程

丹麦技术大学首创将量子计算应用于能源系统潮流建模

108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)

Introduction to bi-sql wildcards

2022年危险化学品经营单位安全管理人员考试试题及模拟考试

QT electronic clock
随机推荐
adb shell sendevent
卷积与转置卷积
Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?
Scala template method pattern
Scala object blending trait
2022r1 quick opening pressure vessel operation test questions and answers
Cobalt Strike安装教程
Powerbi - for you who are learning
Scala adapter pattern
JS Chapter 1 Summary
Tencent cloud wecity solution
智能合约安全审计入门篇 —— delegatecall (2)
MySQL common basic statements (collation)
丹麦技术大学首创将量子计算应用于能源系统潮流建模
我想问一下兴业证券怎么开户?通过链接办理股票开户安全吗
Examination questions and mock examination for safety management personnel of hazardous chemical business units in 2022
Add information on the left and add parts on the right of the status bar
I 刷题 I — 复制带随机指针的链表
【直播回顾】2022腾讯云未来社区城市运营方招募会暨SaaS 2.0新品发布会!