Last active
          August 29, 2025 12:13 
        
      - 
      
- 
        Save d7samurai/6d24d0ba93f8c8fe6b4f02b92d4c8ce0 to your computer and use it in GitHub Desktop. 
Revisions
- 
        d7samurai revised this gist Mar 2, 2024 . 1 changed file with 1 addition and 7 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -43,13 +43,7 @@ int main() audioClient->GetMixFormat(&mixFormat); audioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_EVENTCALLBACK, minimumPeriod, 0, mixFormat, nullptr); IAudioRenderClient* audioRenderClient; 
- 
        d7samurai revised this gist Jun 23, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Minimal WASAPI Minimal WASAPI reference implementation. Runnable console application contained in a single function and laid out in a linear, step-by-step fashion. No modern C++ / OOP / obscuring cruft. Produces a steady sine wave sound. (This is a re-post of the same gist I posted a few years earlier, simply due to me wanting the Minimal D3D11 series to be listed contiguously and it's not possible to hide or rearrange gists). 
- 
        d7samurai revised this gist Apr 26, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Minimal WASAPI Minimal WASAPI reference implementation. Runnable console application contained in a single function and laid out in a linear, step-by-step fashion. No modern C++ / OOP / obscuring cruft. Produces a steady sine wave sound. (This is a re-post of the same gist I posted a few years earlier, since it's not possible to hide or rearrange gists). 
- 
        d7samurai revised this gist Apr 26, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Minimal WASAPI Minimal WASAPI reference implementation. Runnable console application contained in a single function and laid out in a linear, step-by-step fashion. No modern C++ / OOP / obscuring cruft. Produces a steady sine wave sound. (This is a re-post of the same gist I posted a few years earlier, since it's not possible to hide or re-arrange gists). 
- 
        d7samurai revised this gist Apr 26, 2023 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,2 +1,4 @@ # Minimal WASAPI Minimal WASAPI reference implementation. Runnable console application contained in a single function and laid out in a linear, step-by-step fashion. No modern C++ / OOP / obscuring cruft. Produces a steady sine wave sound. (This is a re-post of the same gist I posted a few years earlier, since it's not possible to hide or re-arrange the order of gists). 
- 
        d7samurai created this gist Dec 28, 2022 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ # Minimal WASAPI Minimal WASAPI reference implementation. Runnable console application contained in a single function and laid out in a linear, step-by-step fashion. No modern C++ / OOP / obscuring cruft. Produces a steady sine wave sound. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,101 @@ #pragma comment(lib, "ole32") /////////////////////////////////////////////////////////////////////////////////////////////////// #include <windows.h> #include <mmdeviceapi.h> #include <audioclient.h> #include <math.h> // for sine /////////////////////////////////////////////////////////////////////////////////////////////////// int main() { CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE); /////////////////////////////////////////////////////////////////////////////////////////////// IMMDeviceEnumerator* deviceEnumerator; CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), reinterpret_cast<void**>(&deviceEnumerator)); /////////////////////////////////////////////////////////////////////////////////////////////// IMMDevice* audioDevice; deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &audioDevice); /////////////////////////////////////////////////////////////////////////////////////////////// IAudioClient3* audioClient; audioDevice->Activate(__uuidof(IAudioClient3), CLSCTX_INPROC_SERVER, nullptr, reinterpret_cast<void**>(&audioClient)); /////////////////////////////////////////////////////////////////////////////////////////////// REFERENCE_TIME defaultPeriod; REFERENCE_TIME minimumPeriod; audioClient->GetDevicePeriod(&defaultPeriod, &minimumPeriod); WAVEFORMATEX* mixFormat; audioClient->GetMixFormat(&mixFormat); WAVEFORMATEXTENSIBLE mixFormatEx; memcpy(&mixFormatEx, mixFormat, sizeof(mixFormatEx)); CoTaskMemFree(mixFormat); audioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_EVENTCALLBACK, minimumPeriod, 0, reinterpret_cast<WAVEFORMATEX*>(&mixFormatEx), nullptr); IAudioRenderClient* audioRenderClient; audioClient->GetService(__uuidof(IAudioRenderClient), reinterpret_cast<void**>(&audioRenderClient)); UINT32 bufferSize; audioClient->GetBufferSize(&bufferSize); HANDLE bufferReady = CreateEventA(nullptr, FALSE, FALSE, nullptr); audioClient->SetEventHandle(bufferReady); /////////////////////////////////////////////////////////////////////////////////////////////// double time = 0.0; audioClient->Start(); while (WaitForSingleObject(bufferReady, INFINITE) == WAIT_OBJECT_0) { UINT32 bufferPadding; audioClient->GetCurrentPadding(&bufferPadding); UINT32 frameCount = bufferSize - bufferPadding; float* buffer; audioRenderClient->GetBuffer(frameCount, reinterpret_cast<BYTE**>(&buffer)); for (UINT32 frameIndex = 0; frameIndex < frameCount; frameIndex++) { float amplitude = static_cast<float>(sin(time)); *buffer++ = amplitude; // left *buffer++ = amplitude; // right time += 0.05; } audioRenderClient->ReleaseBuffer(frameCount, 0); } audioClient->Stop(); /////////////////////////////////////////////////////////////////////////////////////////////// return 0; }