• 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);
    }

    源码下载:http://pan.baidu.com/s/1kTj05Fp

    ------------------------------------------------------------
    本文转自www.easydarwin.org,更多开源流媒体解决方案,请关注我们的微信:EasyDarwin 

  • 相关阅读:
    Java基础——java中String、StringBuffer、StringBuilder的区别
    Java基础——深入剖析Java中的装箱和拆箱
    Java内存分配全面浅析
    基于Appium的移动端UI自动化测试
    测试比对工具,辅助型QA转型之路
    读懂了腾讯和阿里的区别,就读懂了中国商业的秘密!
    Python中的基础数据类型(List,Tuple,Dict)及其常用用法简析
    阿里妈妈技术质量再度重磅开源:国内首个智能化功能测试开源平台Markov
    卧槽,极客时间今天专栏课程突然免费,啥情况?
    Flask 与 Django 先学哪个呢
  • 原文地址:https://www.cnblogs.com/babosa/p/5904707.html
Copyright © 2020-2023  润新知