本篇博客使用core audio api录制声卡和麦克风,但是不能同时来,因为这里没有做混音,所以同时只能录制一种,录制的音频会保存成test.pcm,格式是PCM,参数是(48000 32 2),不多说了,直接上代码,希望对各位小伙伴有用。
AudioCapT.h
#pragma once
#include <windows.h>
#include <windowsx.h>
#include <mmdeviceapi.h>
#include <Audioclient.h>
#include <process.h>
#include <avrt.h>
#include <list>
typedef struct
{
INT iDataLen;
LPBYTE pData;
}Audio_Data, * PAudio_Data;
typedef std::list<PAudio_Data> AudioList;
class CAudioCapT
{
public:
enum {SPEAKER = 1,MICPHONE};
public:
CAudioCapT();
~CAudioCapT();
//开始捕获
bool Start();
//停止捕获
bool Stop();
void SaveFormat(WAVEFORMATEX * wf);
void SetDeiveType(int nType);
int GetDeviceType();
Audio_Data * GetAudio();
bool Init();
IMMDevice * GetDevice();
HANDLE GetStartEventHandle();
HANDLE GetStopEventHandle();
WAVEFORMATEX * GetWaveFormat();
AudioList * GetAudioList();
void ClearAudioList();
DWORD GetDataSize();
bool m_bStop;
protected:
bool m_bInit;
HANDLE m_hThreadCapture;
static UINT __stdcall _CaptureThreadProc(LPVOID param);
void OnCaptureData(LPBYTE pData, INT iDataLen);
WAVEFORMATEX m_WaveFormat;
int m_nDeviceType = 0;
AudioList m_al;
CRITICAL_SECTION m_cs;
IMMDevice * m_pDevice = NULL;
HANDLE m_hEventStarted = NULL;
HANDLE m_hEventStop = NULL;
IMMDevice * GetDefaultDevice(int nType);
DWORD m_dwDataSize;
};
AudioCapT.cpp
#include "stdafx.h"
#include "AudioCapT.h"
#define REFTIMES_PER_SEC 10000000
#define REFTIMES_PER_MILLISEC 10000
#pragma comment(lib, "Avrt.lib")
CAudioCapT::CAudioCapT()
{
m_dwDataSize = 0;
m_bInit = false;
m_hThreadCapture = NULL;
m_bStop = false;
::InitializeCriticalSection(&m_cs);
}
CAudioCapT::~CAudioCapT()
{
Audio_Data * pTmp = NULL;
for (std::list<Audio_Data *>::iterator it = m_al.begin(); it != m_al.end(); it++)
{
pTmp = *it;
if (pTmp)
{
if (pTmp->pData)
delete[] pTmp->pData;
delete pTmp;
}
}
m_al.clear();
::DeleteCriticalSection(&m_cs);
}
voi