定期送福利,今天给大家送上Windows中利用DirectShow采集microphone音频,并将采集到的pcm数据,利用FAAC库编码成AAC,进行本地存储或者网络传输。
直接贴代码,解析看注释:
/*
功能描述: 简单实现用DirectShow采集Windows麦克风音频PCM数据
经过FAAC编码成AAC存储文件,可根据项目修改成直播,对讲
等实时传输类应用。
From: http://blog.csdn.net/xiejiashu
Site: http://www.easydarwin.org
时间: 2014/07/28
*/
#include "../DSAudio/General.h"
#include<iostream>
#include<cstring>
#include<windows.h>
#include<conio.h>
#include "../DSAudio/dscapture.h"
#include "DSAudio/FAACEncoder.h"
unsigned char fBitsPerSample, fNumChannels;
unsigned fSamplingFrequency;
unsigned fGranularityInMS;
DSCapture* ds_capture_;
FAACEncoder* faac_encoder_;
unsigned long max_out_bytes;
unsigned char* outbuf;
FILE* f_aac_;
int main()
{
fBitsPerSample = 16; //单个采样音频信息位数
fNumChannels = 2; //通道数
fSamplingFrequency = 44100;//采样率
faac_encoder_ = new FAACEncoder(); //新建AAC编码对象
faac_encoder_->Init(fSamplingFrequency,fNumChannels,fBitsPerSample); //初始化AAC编码器
ds_capture_ = new DSCapture();
std::map<CString, CString> a_devices = ds_capture_->DShowGraph()->AudioCapDevices();
if(a_devices.size() == 0 ) ::exit(0);
DSAudioFormat audio_fmt;
audio_fmt.samples_per_sec = fSamplingFrequency;
audio_fmt.channels = fNumChannels;
audio_fmt.bits_per_sample = fBitsPerSample;
// 枚举音频采样设备,选择最后一个
CString audio_device_id;
for (std::map<CString, CString>::iterator it = a_devices.begin(); it != a_devices.end(); ++it)
{
audio_device_id = it->first;
}
ds_capture_->Create(audio_device_id, audio_fmt, NULL);
// 开始采集音频数据
ds_capture_->StartAudio();
::Sleep(10);
max_out_bytes = faac_encoder_->MaxOutBytes();
outbuf = (unsigned char*)malloc(max_out_bytes);
// 编码后存文件
f_aac_ = fopen("./audio.aac","wb");
printf("Recording...");
while(1)
{
int pcmLen = 0;
// 采集音频pcm数据
char* pcmBuf = ds_capture_->GetBuffer(pcmLen);
if(pcmLen > 0)
{
unsigned int sample_count = (pcmLen << 3)/fBitsPerSample;
unsigned int buf_size = 0;
// 编码AAC
faac_encoder_->Encode((unsigned char*)pcmBuf, sample_count, (unsigned char*)outbuf, buf_size);
if(buf_size > 0)
{
printf(".");
// 存文件或者自定义
fwrite(outbuf,1,buf_size,f_aac_);
}
}
// 释放采集的pcm数据
ds_capture_->ReleaseBuffer(pcmBuf);
::Sleep(1);
}
free(outbuf);
}
源码下载:WinAudioRecord.rar_免费高速下载|百度网盘-分享无限制
------------------------------------------------------------
本文转自www.easydarwin.org,更多开源流媒体解决方案,请关注我们的微信:EasyDarwin