1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
int audioDeviceID = 0; const long long audioCaptureRefreshFrequency = 10000000; winrt::com_ptr<IMMDeviceEnumerator> pEnumerator{ nullptr }; winrt::com_ptr<IMMDeviceCollection> pDeviceCollection{ nullptr }; winrt::com_ptr<IMMDevice> pDevice{ nullptr }; WAVEFORMATEX* pwfx{ nullptr }; winrt::com_ptr<IAudioCaptureClient> pCaptureClient{ nullptr }; winrt::com_ptr<IAudioClient> pAudioClient{ nullptr }; UINT32 packetLength = 0; std::unique_ptr<std::fstream> audioWriter; uint32_t audioSize = 0;
REFERENCE_TIME hnsRequestedDuration = audioCaptureRefreshFrequency; UINT32 bufferFrameCount; REFERENCE_TIME hnsActualDuration; UINT32 numFramesAvailable; BYTE* pData{ nullptr }; DWORD flags;
CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), pEnumerator.put_void()); pEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, pDeviceCollection.put()); pDeviceCollection->Item(audioDeviceID, pDevice.put()); pDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, pAudioClient.put_void()); pAudioClient->GetMixFormat(&pwfx); pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, hnsRequestedDuration, 0, pwfx, nullptr); pAudioClient->GetBufferSize(&bufferFrameCount); pAudioClient->GetService(__uuidof(IAudioCaptureClient), pCaptureClient.put_void());
hnsActualDuration = (double)audioCaptureRefreshFrequency * bufferFrameCount / pwfx->nSamplesPerSec; pAudioClient->Start();
audioWriter = std::make_unique<std::fstream>(std::filesystem::path(L"audio.wav").string(), std::ios::trunc | std::ios::out | std::ios::binary);
audioSize = 0;
audioWriter->write("\x52\x49\x46\x46\x00\x00\x00\x00\x57\x41\x56\x45\x66\x6D\x74\x20\x10\x00\x00\x00\x01\x00\x01\x00\x80\x3E\x00\x00\x00\x7D\x00\x00\x02\x00\x10\x00\x64\x61\x74\x61\x00\x00\x00\x00", 44);
while (isAudioCaptureEnabled && pCaptureClient != nullptr) { if (FAILED(pCaptureClient->GetNextPacketSize(&packetLength))) { continue; } pcmNew.clear(); while (packetLength != 0) { if (FAILED(pCaptureClient->GetBuffer(&pData, &numFramesAvailable, &flags, nullptr, nullptr))) { break; } if (flags & AUDCLNT_BUFFERFLAGS_SILENT) { pData = nullptr; }
float resamplingSum = 0.0f; int resamplingCounter = pwfx->nSamplesPerSec / 16000; for (int i = 0, c = 1; i < numFramesAvailable * pwfx->nBlockAlign; i += pwfx->nBlockAlign, c++) { memcpy(&tempAudioFrame, &(pData[i]), sizeof(tempAudioFrame)); resamplingSum += tempAudioFrame; if (c % resamplingCounter == 0) { pcmNew.push_back((int16_t)(resamplingSum / (float)resamplingCounter * 32767.0f)); resamplingSum = 0.0f; } } winrt::check_hresult(pCaptureClient->ReleaseBuffer(numFramesAvailable)); winrt::check_hresult(pCaptureClient->GetNextPacketSize(&packetLength)); } audioWriter->write((const char*)pcmNew.data(), pcmNew.size() * sizeof(pcmNew[0])); audioSize += pcmNew.size() * sizeof(pcmNew[0]);
Sleep(hnsActualDuration / audioCaptureRefreshFrequency * 40); }
|