当前位置: 首页 > 工具软件 > DarkIce > 使用案例 >

使用DarkIce中的aflibConverter进行音频重采样

姬心思
2023-12-01

在处理音频时候会碰见不同采样率的PCM数据转换问题,如44.1k hz采样率的数据需要转换为16k hz或8k hz采样率的PCM数据,这时候涉及到音频重采样问题。现将解决方法、碰到的问题和Demo贴出来,以便做同样工作的人解决这个问题更顺利快捷。

经测试,使用DarkIce中的aflibConverter可以实现上述功能,代码抽取量少,便于集成,接口使用简单,采样效果不错。DarkIce是一个开源音频流引擎,它可以从音频源(如声卡等)采集音频、音频编码、发送至流媒体服务器。 http://code.google.com/p/darkice/downloads/list 和http://darkice.org/  有DarkIce的详细介绍和开源代码。

   使用到的文件有 aflibConverter.h, aflibConverter.cpp ,aflibConverterLargeFilter.h,aflibConverterSmallFilter.h,调用接口在aflibConverter.h中提供,使用方法:

include "aflibConverter.h"

#include <iostream>
#include <fstream>
using namespace std;

void main(int argc, char* argv[])
{
// 检查命令行
if (argc != 3)
{
cout << "Convert a pcm file from rate 11.025kHz to 8kHz\n";
cout << "Usage : " << argv[0] << " input_file_name output_file_name" << endl;
return ;
}

// 读入输入数据文件
fstream fIn(argv[1], ios::in | ios::binary);
fIn.seekg(0, ios::end);
long size = fIn.tellg();
fIn.seekg(0, ios::beg);

long inCnt = size / 2;
short* pIn = new short[inCnt];
fIn.read((char*)pIn, size);
fIn.close();

//  创建或打开输出文件
fstream fOut(argv[2], ios::out | ios::binary);
long outCnt = inCnt * 8000 / 11025;
short* pOut = new short[outCnt];

// 定义转换器函数
aflibConverter converter(true, false, true);

// 初始化转换器
converter.initialize(8000.0 / 11025.0, 1);

// 重采样
int tempInCnt = inCnt;
converter.resample(tempInCnt, outCnt, pIn, pOut);

// 将结果写入输出文件
fOut.write((const char*)pOut, outCnt * 2);

fOut.close();

delete pOut;
delete pIn;

}

使用aflibConverter下采样时候,用上面代码举例,对于 long outCnt = inCnt * 8000 / 11025;要求 inCnt * 8000能被11025整除,否则在converter.resample(tempInCnt, outCnt, pIn, pOut);时候会报错。
来源:http://blog.csdn.net/lezhiyong

 类似资料: