声音捕获PCM数据回调函数(CAudioCapture.h):
#ifndef _WAVEFORMATEX_
#define _WAVEFORMATEX_
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef struct tWAVEFORMATEX
{
WORD wFormatTag; /* format type */
WORD nChannels; /* number of channels (i.e. mono, stereo...) */
DWORD nSamplesPerSec; /* sample rate */
DWORD nAvgBytesPerSec; /* for buffer estimation */
WORD nBlockAlign; /* block size of data */
WORD wBitsPerSample; /* Number of bits per sample of mono data */
WORD cbSize; /* The count in bytes of the size of
extra information (after cbSize) */
} WAVEFORMATEX;
typedef WAVEFORMATEX *PWAVEFORMATEX;
#endif /* _WAVEFORMATEX_ */
#ifndef _AUDIODATA_SOURCE_
#define _AUDIODATA_SOURCE_
enum IID_Audio_Source
{
IID_AD_SPEEKER, // 扬声器
IID_AD_MIC // 麦克风
}IID_Audio_Source;
#endif
// 音频流回调
#ifndef _AUDIODATA_CALLBACK_
#define _AUDIODATA_CALLBACK_
typedef void (*LPAudioDataRealCallback)(unsigned char *pPcmData, int size, WAVEFORMATEX* wfex, enum IID_Audio_Source iidAudioSource, void* pContext);
#endif
调用接口定义(CAudioCapture.h):
// 设置音频回调函数,并开始捕获音频数据
AVFILTER_API void* AudioSourceStartCapture(LPAudioDataRealCallback pCallback, void* pContext);
// 停止音频数据捕获
AVFILTER_API bool AudioSourceStopCapture(void* pSource);
实现源码(CAudioCapture.c):
#include <stdio.h>
#include "CAudioCapture.h"
#include <initguid.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#include <memory.h>
#include <process.h>
typedef struct tAudioSource
{
IMMDevice* m_pDevice;
IAudioClient* m_pClient;
IAudioCaptureClient* m_pCapture;
IAudioRenderClient* m_pRender;
WAVEFORMATEX* m_pFormat;
enum IID_Audio_Source m_iidAudioSource;//音频源采集方式-可以是MIC,也可以是声卡
bool m_bReconnecting;//设备连接标识
HANDLE m_hReconnectThread;//自动重连线程句柄
bool m_bActive;//设备是否激活
HANDLE m_hCaptureThread;//捕获线程句柄
HANDLE m_hStopSignal;//线程停止信号
HANDLE m_hReceiveSignal;//接受数据信号
CRITICAL_SECTION m_csMemLock;//数据访问互斥锁
void* m_memRawBuffer;//音频数据存放缓冲区
int m_nMemSize;//pcm数据缓冲区大小
LPAudioDataRealCallback m_pAudioDataRealCalBack;//实时捕获数据回调函数
void* m_pAudioDataRealCalBackUser;//实时捕获数据用户接受对象
}AudioSource, *LPAudioSource;
//申请一个音频源对象
void AllocAudioResource(LPAudioSource* ppSource);
//释放一个音频源对象
void ReleaseAudioResource(LPAudioSource pSource);
//开始捕获音频数据
void StartAudioCapture(LPAudioSource pSource, enum IID_Audio_Source iidAudioSource);
//停止捕获音频数据
void StopAudioCapture(LPAudioSource pSource);
//初始化
bool TryInitialize(LPAudioSource pSource);
bool Initialize(LPAudioSource pSource);
//初始化音频源设备
bool InitDevice(LPAudioSource pSource, IMMDeviceEnumerator *enumerator);
//初始化音频源设备
bool InitClient(LPAudioSource pSource);
bool InitRender(LPAudioSource pSource);
bool InitCapture(LPAudioSource pSource);
//自动重连线程
void Reconnect(LPAudioSource pSource);
DWORD WINAPI OnReconnectThread(LPVOID param);
//音频数据获取线程
DWORD WINAPI OnAudioCaptureThread(LPVOID param);
bool DoAudioCapture(LPAudioSource pSource);
//获取音频数据
bool ProcessAudioCaptureData(LPAudioSource pSource);
//相应数据回调
void OnCaptureDataCallback(LPAudioSource pSource, LPBYTE pData, INT nDataLen);
//所需接口GUID值定义
const IID IID_IAudioRenderClient = { 0xF294ACFC, 0x3146, 0x4483, { 0xA7, 0xBF, 0xAD, 0xDC,