自动挂机麦克风录音测试
2023-12-11
29
0
要则试麦克风重复打开关闭的功能,如果手动也太麻烦了。
为了让打开关闭的时间更随机一点,使用WASAPI做了这么一个小功能。
#include <iostream>
#include <Shlwapi.h>
#include <MMDeviceAPI.h>
#include <AudioClient.h>
#define SAFE_RELEASE(punk) \
if ((punk) != NULL) \
{ (punk)->Release(); (punk) = NULL; }
void MicTest(ULONGLONG EndTieme)
{
IMMDeviceEnumerator* deviceEnumerator = NULL;
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&deviceEnumerator));
if (FAILED(hr))
{
printf("Unable to retrieve CoCreateInstance %x\n", hr);
return;
}
IAudioClient* _AudioClient;
IAudioCaptureClient* _CaptureClient;
IMMDevice* _Device;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eCapture, eMultimedia, &_Device);
if (FAILED(hr))
{
printf("Unable to retrieve device %x\n", hr);
return;
}
HANDLE _AudioSamplesReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (_AudioSamplesReadyEvent == NULL)
{
printf("Unable to create samples ready event: %d.\n", GetLastError());
return;
}
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;
}
WAVEFORMATEX* _MixFormat;
hr = _AudioClient->GetMixFormat(&_MixFormat);
if (FAILED(hr))
{
printf("Unable to get mix format on audio client: %x.\n", hr);
return ;
}
REFERENCE_TIME hnsDefaultDevicePeriod;
hr = _AudioClient->GetDevicePeriod(&hnsDefaultDevicePeriod, NULL);
if (FAILED(hr))
{
return ;
}
hr = _AudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,AUDCLNT_STREAMFLAGS_EVENTCALLBACK, hnsDefaultDevicePeriod, hnsDefaultDevicePeriod, _MixFormat, NULL);
if (FAILED(hr))
{
printf("Unable to initialize audio client: %x.\n", hr);
return ;
}
UINT32 _BufferSize;
hr = _AudioClient->GetBufferSize(&_BufferSize);
if (FAILED(hr))
{
printf("Unable to get audio client buffer: %x. \n", hr);
return ;
}
hr = _AudioClient->SetEventHandle(_AudioSamplesReadyEvent);
if (FAILED(hr))
{
printf("Unable to set ready event: %x.\n", hr);
return ;
}
hr = _AudioClient->GetService(IID_PPV_ARGS(&_CaptureClient));
if (FAILED(hr))
{
printf("Unable to get new capture client: %x.\n", hr);
return ;
}
hr = _AudioClient->Reset();
if (FAILED(hr))
{
printf("Unable to get new capture client: %x.\n", hr);
return ;
}
hr = _AudioClient->Start();
if (FAILED(hr))
{
printf("Unable to get new capture client: %x.\n", hr);
return ;
}
while (GetTickCount64() < EndTieme)
{
DWORD waitResult = WaitForSingleObject(_AudioSamplesReadyEvent, INFINITE);
BYTE* pData;
UINT32 framesAvailable;
DWORD flags;
hr = _CaptureClient->GetBuffer(&pData, &framesAvailable, &flags, NULL, NULL);
if (SUCCEEDED(hr))
{
hr = _CaptureClient->ReleaseBuffer(framesAvailable);
if (FAILED(hr))
{
printf("Unable to release capture buffer: %x!\n", hr);
}
}
}
hr = _AudioClient->Stop();
CloseHandle(_AudioSamplesReadyEvent);
SAFE_RELEASE(deviceEnumerator);
SAFE_RELEASE(_Device);
SAFE_RELEASE(_AudioClient);
SAFE_RELEASE(_CaptureClient);
}
int main()
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
for(int i=0;;i++)
{
ULONG range = rand() % 100000;
ULONGLONG EndTime = GetTickCount64() + range;
printf("%d %dms test\n", i, range);
MicTest(EndTime);
}
return 0;
}