不能使用ubuntu22.04 ,异常报错
使用20.04正常按照官网操作即可
自己在克隆下面的项目目录下面直接写一个文件testvad.c
#include "fvad.h"
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h> // 需要libsndfile库来读取.wav文件
int main()
{
// 打开WAV文件
const char *file_path = "/opt/libfvad/video1.wav";
SF_INFO sfinfo;
SNDFILE *sndfile = sf_open(file_path, SFM_READ, &sfinfo);
if (sndfile == NULL)
{
fprintf(stderr, "无法打开音频文件:%s\n", file_path);
return 1;
}
// 确保文件是单声道且有16000 Hz的采样率
if (sfinfo.channels != 1 || sfinfo.samplerate != 16000)
{
fprintf(stderr, "音频文件格式不符合要求。需要16000 Hz采样率的单声道文件。\n");
sf_close(sndfile);
return 1;
}
// 初始化VAD实例
Fvad *vad = fvad_new();
if (vad == NULL)
{
fprintf(stderr, "无法创建VAD实例\n");
sf_close(sndfile);
return 1;
}
// 设置VAD的采样率
if (fvad_set_sample_rate(vad, 16000) == -1)
{
fprintf(stderr, "无法设置VAD采样率\n");
fvad_free(vad);
sf_close(sndfile);
return 1;
}
// 设置VAD模式
if (fvad_set_mode(vad, 3) == -1)
{
fprintf(stderr, "无法设置VAD模式\n");
fvad_free(vad);
sf_close(sndfile);
return 1;
}
// 分配帧缓冲区,16000 Hz采样率下10毫秒的帧大小
const size_t frame_size = 160; // 16000 * 0.01 = 160
int16_t frame[frame_size];
// 读取和处理音频文件中的每一帧
while (sf_read_short(sndfile, frame, frame_size) == frame_size)
{
// 处理帧并获取VAD决策
int vad_result = fvad_process(vad, frame, frame_size);
if (vad_result == -1)
{
fprintf(stderr, "处理音频帧时出错。\n");
break;
}
// 输出VAD的状态
printf("%d\n", vad_result);
}
// 清理并释放资源
fvad_free(vad);
sf_close(sndfile);
return 0;
}
然后终端编译,运行即可
gcc -o testvad testvad.c -I/usr/local/include -L/usr/local/lib -lfvad -L/usr/lib -lsndfile
./testvad
结果如下: