资源简介

这个代码可以实时录制声卡或者麦克风,并重新采集为44100,FLTP,然后保存为test44100.pcm,可以使用ffplay播放。

资源截图

代码片段和文件信息

// audiocap.cpp : 定义控制台应用程序的入口点。
//

#include “stdafx.h“
#include “AudioCapT.h“
#include 

extern “C“
{
#include 
#include 
#include 
#include 
#include 
#include 
}

#include 
using namespace std;
#pragma comment(lib“avformat.lib“)
#pragma comment(lib“avcodec.lib“)
#pragma comment(lib“avutil.lib“)
#pragma comment(lib“swscale.lib“)
#pragma comment(lib“swresample.lib“)

bool FillFltpSample(double *dst int nb_samples int nb_channels char * pPcmBuffer int nPcmBufLen)
{
int nTotal = nb_samples*nb_channels * 4;

if (!dst || !pPcmBuffer)
return false;

memset(dst 0 nb_samples*nb_channels * 4);

int nTmp = 0;

if (nTotal >= nPcmBufLen)
{
nTmp = nPcmBufLen;
}
else
{
nTmp = nTotal;
}

memcpy(dst pPcmBuffer nTmp);

return true;
}


int _tmain(int argc _TCHAR* argv[])
{
int64_t src_ch_layout = av_get_default_channel_layout(2) dst_ch_layout = AV_CH_LAYOUT_STEREO;
int src_rate = 48000 dst_rate = 44100;
uint8_t **src_data = NULL **dst_data = NULL;
int src_nb_channels = 0 dst_nb_channels = 0;
int src_linesize dst_linesize;
int src_nb_samples = 1024 dst_nb_samples max_dst_nb_samples;
enum AVSampleFormat src_sample_fmt = AV_SAMPLE_FMT_FLT dst_sample_fmt = AV_SAMPLE_FMT_S16;
const char * pszDstFileName = “D:\\test44100.pcm“;
FILE * fDstFile = NULL;
int nDstBufSize = 0;
const char * strFmt = NULL;
struct SwrContext * swr_ctx = NULL;
int nRet = 0;

CAudioCapT ac;

CoInitialize(NULL);

av_register_all();

avcodec_register_all();

fDstFile = fopen(pszDstFileName “wb“);
if (!fDstFile) {
printf(“open file failed.\n“);
return -1;
}

//创建重采样上下文

swr_ctx = swr_alloc();
if (!swr_ctx) 
{
printf(“swr_alloc failed.\n“);
goto end;
}

av_opt_set_int(swr_ctx “in_channel_layout“ src_ch_layout 0);
av_opt_set_int(swr_ctx “in_sample_rate“ src_rate 0);
av_opt_set_sample_fmt(swr_ctx “in_sample_fmt“ src_sample_fmt 0);
av_opt_set_int(swr_ctx “out_channel_layout“ dst_ch_layout 0);
av_opt_set_int(swr_ctx “out_sample_rate“ dst_rate 0);
av_opt_set_sample_fmt(swr_ctx “out_sample_fmt“ dst_sample_fmt 0);


if ((nRet = swr_init(swr_ctx)) < 0)
{
printf(“Failed to initialize the resampling context\n“);
goto end;
}

src_nb_channels = av_get_channel_layout_nb_channels(src_ch_layout);

nRet = av_samples_alloc_array_and_samples(&src_data &src_linesize src_nb_channels
src_nb_samples src_sample_fmt 0);

if (nRet < 0)
{
printf(“Could not allocate source samples\n“);
goto end;
}

max_dst_nb_samples = dst_nb_samples = av_rescale_rnd(src_nb_samples dst_rate src_rate AV_ROUND_UP);
dst_nb_channels = av_get_channel_layout_nb_channels(dst_ch_layout);
nRet = av_samples_alloc_array_and_samples(&dst_data &dst_linesi

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       5904  2018-07-10 15:16  audiocap\audiocap\audiocap.cpp

     文件       4778  2018-07-09 10:51  audiocap\audiocap\audiocap.vcxproj

     文件       1499  2018-07-05 15:48  audiocap\audiocap\audiocap.vcxproj.filters

     文件       7927  2018-07-10 11:45  audiocap\audiocap\AudioCapT.cpp

     文件       1293  2018-07-09 14:58  audiocap\audiocap\AudioCapT.h

     文件       1517  2018-07-04 12:13  audiocap\audiocap\ReadMe.txt

     文件        214  2018-07-04 12:13  audiocap\audiocap\stdafx.cpp

     文件        234  2018-07-04 12:13  audiocap\audiocap\stdafx.h

     文件        236  2018-07-04 12:13  audiocap\audiocap\targetver.h

     文件        970  2018-07-04 12:13  audiocap\audiocap.sln

     文件         43  2018-07-06 14:26  audiocap\播放命令.txt

     目录          0  2018-07-10 15:27  audiocap\audiocap

     目录          0  2018-07-10 15:26  audiocap

----------- ---------  ---------- -----  ----

                24615                    13


评论

共有 条评论