作者: 使徒保罗
声明: 欢迎交流学习,如有任何疑问,请通过邮箱联系本人
环境: win10 64位+qt5.7(MINGW)
参考:[微软还回录音文档](https://msdn.microsoft.com/en-us/library/dd316551%28v=vs.85%29.aspx)
功能: qt调用The Windows Audio Session API (WASAPI) 采集声卡示例代码
最低系统要求: 客户端Windows Vista,服务器Windows Server 2008,手机Windows Phone 8
pro文件内容:
QT += core
QT -= gui
CONFIG += c++11
TARGET = WindowsCoreAduioDemo
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
LIBS += -L$$PWD/lib/ -lOle32
main.cpp文件内容:
#include
#include
#include
#include//WSAPI
#include//WSAPI
#define MAX_AUDIO_FRAME_SIZE 192000
#define SAFE_RELEASE(punk) \
if((NULL != punk)) \
{ (punk)->Release(); (punk)=NULL; }
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
IAudioClient * _AudioClient = NULL;
IAudioCaptureClient *_CaptureClient = NULL;
IMMDevice * _Device = NULL;
IMMDeviceEnumerator * _DeviceEnumerator = NULL;
HANDLE _AudioSamplesReadyEvent = NULL;
WAVEFORMATEX * _MixFormat = NULL;
BYTE *pBuffer = NULL;
//初始化Com库
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if(hr != S_OK)
{
printf("初始化Com库失败\n");
return -1;
}
//创建Com对象IMMDeviceEnumerator
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&_DeviceEnumerator));
if (FAILED(hr))
{
printf("Unable to retrieve CoCreateInstance %x\n", (long)hr);
return false;
}
//获取声音播放设备对象IMMDevice
hr = _DeviceEnumerator->GetDefaultAudioEndpoint(eRender,eMultimedia,&_Device);
if (FAILED(hr))
{
printf("Unable to retrieve device %x\n", hr);
return false;
}
//释放IMMDeviceEnumerator对象
SAFE_RELEASE(_DeviceEnumerator)
//在音频数据拷贝之前要添加一个引用,
_Device->AddRef();
//创建Com对象IAudioClient
hr = _Device->Activate(__uuidof(IAudioClient), CLSCTX_INPROC_SERVER, NULL, reinterpret_cast<void **>(&_AudioClient));
if (FAILED(hr))
{
printf("Unable to activate audio client: %x.\n", hr);
return false;
}
//获取音频引擎内如共享模式程序的音频流格式
hr = _AudioClient->GetMixFormat(&_MixFormat);
if (FAILED(hr))
{
printf("Unable to get mix format on audio client: %x.\n", hr);
return false;
}
//wBitsPerSample是采样深度(位深) nChannels是音频通道数 _FrameSize一个采样的大小(字节)
size_t _FrameSize = (_MixFormat->wBitsPerSample / 8) * _MixFormat->nChannels;
//初始化音频引擎
/*
*AUDCLNT_SHAREMODE_SHARED只用共享模式才能在还回(loopback)模式下起作用
*AUDCLNT_STREAMFLAGS_EVENTCALLBACK允许设置事件通知回调 SetEventHandle才会有效果
*AUDCLNT_STREAMFLAGS_LOOPBACK设置音频为还回模式,
*这样WSAAPI客户端就能捕获渲染断点设备播放的音频流
*/
hr = _AudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK|AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 0, 0, _MixFormat, NULL);
if (FAILED(hr))
{
printf("Unable to initialize audio client: %x.\n", hr);
return false;
}
_AudioSamplesReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (_AudioSamplesReadyEvent == NULL)
{
printf("Unable to create samples ready event: %d.\n", GetLastError());
return false;
}
//设置事件通知对象
hr = _AudioClient->SetEventHandle(_AudioSamplesReadyEvent);
if (FAILED(hr))
{
printf("Unable to set ready event: %x.\n", hr);
return false;
}
//生成采集服务
hr = _AudioClient->GetService(IID_PPV_ARGS(&_CaptureClient));
if (FAILED(hr))
{
printf("Unable to get new capture client: %x.\n", hr);
return false;
}
//开始采集
hr = _AudioClient->Start();
if (FAILED(hr))
{
printf("Unable to get new capture client: %x.\n", hr);
return false;
}
pBuffer=new BYTE[MAX_AUDIO_FRAME_SIZE];
while (TRUE)
{
DWORD waitResult = WaitForSingleObject(_AudioSamplesReadyEvent, INFINITE);
BYTE *pData = NULL;
INT nBufferLenght = 0;
UINT32 framesAvailable = 0;
DWORD flags = 0;
hr = _CaptureClient->GetBuffer(&pData, &framesAvailable, &flags, NULL, NULL);
if (SUCCEEDED(hr))
{
if (framesAvailable!=0)
{
if (flags & AUDCLNT_BUFFERFLAGS_SILENT)
{
//告诉Copy Data写静音
pData = NULL;
}
else
{
//Copy data from the audio engine buffer to the output buffer.
int nDataLen = framesAvailable*_FrameSize;
CopyMemory(pBuffer,pData,nDataLen);
printf("get capture audio len: %d!\n", nDataLen);
}
}
}
_CaptureClient->ReleaseBuffer(framesAvailable);
}
delete[] pBuffer;
SAFE_RELEASE(_DeviceEnumerator)
SAFE_RELEASE(_Device)
SAFE_RELEASE(_DeviceEnumerator)
SAFE_RELEASE(_DeviceEnumerator)
SAFE_RELEASE(_DeviceEnumerator)
CoUninitialize();
return a.exec();
}