std::cout << "WASAPI Audio Recorder for Windows 10\n"; std::cout << "====================================\n"; std::cout << "1. Capture system audio (what you hear)\n"; std::cout << "2. Capture microphone input\n"; std::cout << "Choose mode (1 or 2): ";
#include <windows.h> #include <mmdeviceapi.h> #include <audioclient.h> #include <audiopolicy.h> #include <iostream> #include <vector> #include <fstream> #pragma comment(lib, "ole32.lib") #pragma comment(lib, "avrt.lib")
find_library(ole32_lib ole32) find_library(avrt_lib avrt) wasapi download windows 10
bool loopback = (mode == 1);
if (recorder.Initialize(loopback)) recorder.StartRecording("recording.wav"); recorder.Cleanup(); std::cout << "Recording saved as recording.wav\n"; else std::cout << "Failed to initialize WASAPI recording\n"; return 1; std::cout << "WASAPI Audio Recorder for Windows 10\n";
public: bool Initialize(bool captureLoopback = true) = AUDCLNT_STREAMFLAGS_EVENTCALLBACK; hr = pAudioClient->Initialize( AUDCLNT_SHAREMODE_SHARED, streamFlags, hnsRequestedDuration, 0, pwfx, NULL ); if (FAILED(hr)) return false; // Get buffer size hr = pAudioClient->GetBufferSize(&bufferFrameCount); if (FAILED(hr)) return false; // Create event handle hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if (hEvent == NULL) return false; hr = pAudioClient->SetEventHandle(hEvent); if (FAILED(hr)) return false; // Get capture client hr = pAudioClient->GetService(__uuidof(IAudioCaptureClient), (void**)&pCaptureClient); if (FAILED(hr)) return false; return true;
bool StartRecording(const char* filename) outputFile.open(filename, std::ios::binary); if (!outputFile.is_open()) return false; // Write WAV header (placeholder) WriteWAVHeader(0, pwfx->nSamplesPerSec, pwfx->nChannels, 16); // Start audio capture HRESULT hr = pAudioClient->Start(); if (FAILED(hr)) return false; std::cout << "Recording... Press Enter to stop\n"; BYTE* pData; UINT32 framesAvailable; DWORD flags; while (true) // Wait for audio data DWORD waitResult = WaitForSingleObject(hEvent, 1000); if (waitResult == WAIT_OBJECT_0) hr = pCaptureClient->GetBuffer(&pData, &framesAvailable, &flags, NULL, NULL); if (SUCCEEDED(hr)) if (framesAvailable > 0) // Calculate bytes to write UINT32 bytesToWrite = framesAvailable * pwfx->nBlockAlign; outputFile.write(reinterpret_cast<char*>(pData), bytesToWrite); // Check for silence (optional) if (flags & AUDCLNT_BUFFERFLAGS_SILENT) // Handle silent buffer if needed pCaptureClient->ReleaseBuffer(framesAvailable); // Check for Enter key if (GetAsyncKeyState(VK_RETURN) & 0x8000) break; // Stop recording pAudioClient->Stop(); // Update WAV header std::streampos fileSize = outputFile.tellp(); outputFile.close(); UpdateWAVHeader(filename, static_cast<UINT32>(fileSize) - 44); return true; Press Enter to stop\n"; BYTE* pData; UINT32 framesAvailable;
class WASAPIRecorder private: IMMDeviceEnumerator* pEnumerator = nullptr; IMMDevice* pDevice = nullptr; IAudioClient* pAudioClient = nullptr; IAudioCaptureClient* pCaptureClient = nullptr; WAVEFORMATEX* pwfx = nullptr; HANDLE hEvent = nullptr; UINT32 bufferFrameCount = 0; std::ofstream outputFile;