DirectShow音频采集pcm,实时编码AAC,附源码

定期送福利,今天给大家送上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 
 

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值