ffmpeg库实现音频重采样功能

本文介绍了如何使用ffmpeg库在C++项目中实现音频重采样的功能,详细讲述了项目需求、ffmpeg库的简介及安装,并提供了核心函数的示例代码,通过初始化SwrContext结构体和调用swr_convert函数来完成音频重采样,最后通过与MATLAB的resample函数结果对比进行了验证。
摘要由CSDN通过智能技术生成

ffmpeg库实现音频重采样功能

项目需求

因使用的信号采集探头频率不同,导致输入音频信号频率不固定,后续处理中为了获得稳定的视频信号,将声音频率重采样到固定的频率输出。

ffmpeg库简介

官方下载:http://www.ffmpeg.org/download.html。根据网页下载自行下载。
参考安装教程:https://blog.csdn.net/xiaoxueyaoxuexi/article/details/110451006
https://blog.csdn.net/qq_34732729/article/details/87182435
根据自己需求进行安装,本人因项目原因须在VS2010中搭建ffmpeg环境,且得是win32版本,为此颇费了一番功夫,后续会整理VS2010搭建ffmpeg环境过程。
另外还尝试了VS2017版本搭建ffmpeg库,x64版本,方法过程基本一致。

示例代码
#include "ResampleFunction.h"
#include <iostream>
#include <algorithm>
using namespace std;

#ifdef __cplusplus
extern "C"
{
   
#endif
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
#ifdef __cplusplus
};
#endif
#define OUT_SAMPLE_RATE 48000
int ResampleFunction::getResampleData(double *in_data, int in_len, int in_sample_rate, double** out_data, int* out_len)
{
   
		//重采样上下文
		int ret;
		ResampleFunction::SWR_param_set swr_param_set;
		dst_data = NULL;
		src_data = NULL;
		swr_param_set.src_ch_layout = AV_CH_LAYOUT_MONO;
		swr_param_set.dst_ch_layout = AV_CH_LAYOUT_MONO;
		swr_param_set.src_nb_channels = 0;
		swr_param_set.dst_nb_channels = 0;
		swr_param_set.src_sample_fmt = AV_SAMPLE_FMT_DBLP;
		swr_param_set.dst_sample_fmt = AV_SAMPLE_FMT_DBLP;
		swr_param_set.dst_rate = OUT_SAMPLE_RATE;
		swr_param_set.src_rate = in_sample_rate;
		int out_spb = av_get_bytes_per_sample(swr_param_set.dst_sample_fmt);//占用字节数
		//每次采用样本数
		int src_nb_samples = in_len, dst_nb_samples, max_dst_nb_samples;
		int dst_bufsize;
		int out_samples;
		*out_len = 0;
		if (m_sign == true)
		{
   
			swr_ctx = swr_alloc();
			if (!swr_ctx)
			{
   
				ret = -1;
				return ret;
			}
			//设置源通道布局
			av_opt_set_int(swr_ctx, "in_channel_layout", swr_param_set.src_ch_layout, 0);
			设置源通道采样率
			av_opt_set_int(swr_ctx, "in_sample_rate", in_sample_rate, 0);
			设置源通道样本格式
			av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", swr_param_set.src_sample_fmt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值