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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| #include <mmdeviceapi.h> #include <audioclient.h> #include <avrt.h> #include <endpointvolume.h>
extern "C" { #pragma comment(lib, "avrt.lib") #pragma comment(lib, "ole32.lib") }
struct AudioClock { atomic<double> pts{ 0.0 }; atomic<bool> valid{ false };
void set(double p) { pts.store(p); valid.store(true); } double get() const { return pts.load(); } };
class WasapiPlayer { public: IMMDeviceEnumerator* pEnum = nullptr; IMMDevice* pDevice = nullptr; IAudioClient* pClient = nullptr; IAudioRenderClient* pRender = nullptr; IAudioEndpointVolume* pVolume = nullptr;
WAVEFORMATEX wfx{}; UINT32 bufferFrames = 0; bool initialized = false;
bool init(int sampleRate, int channels) { CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnum))) return false; if (FAILED(pEnum->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice))) return false; if (FAILED(pDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, (void**)&pClient))) return false;
wfx.wFormatTag = WAVE_FORMAT_PCM; wfx.nChannels = (WORD)channels; wfx.nSamplesPerSec = (DWORD)sampleRate; wfx.wBitsPerSample = 16; wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
REFERENCE_TIME hnsReq = 10000000; if (FAILED(pClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM | AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY, hnsReq, 0, &wfx, nullptr))) return false;
pClient->GetBufferSize(&bufferFrames); if (FAILED(pClient->GetService(__uuidof(IAudioRenderClient), (void**)&pRender))) return false;
pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, nullptr, (void**)&pVolume);
pClient->Start(); initialized = true; return true; }
int write(const uint8_t* data, int frames) { if (!initialized) return 0; UINT32 padding = 0; pClient->GetCurrentPadding(&padding); UINT32 avail = bufferFrames - padding; UINT32 toWrite = min((UINT32)frames, avail); if (toWrite == 0) return 0;
BYTE* buf = nullptr; if (FAILED(pRender->GetBuffer(toWrite, &buf))) return 0; memcpy(buf, data, toWrite * wfx.nBlockAlign); pRender->ReleaseBuffer(toWrite, 0); return (int)toWrite; }
double getBufferedSeconds() const { if (!initialized) return 0.0; UINT32 padding = 0; pClient->GetCurrentPadding(&padding); return (double)padding / (double)wfx.nSamplesPerSec; }
void setVolume(float vol) { if (pVolume) { vol = max(0.0f, min(1.0f, vol)); pVolume->SetMasterVolumeLevelScalar(vol, nullptr); } }
float getVolume() const { float vol = 1.0f; if (pVolume) pVolume->GetMasterVolumeLevelScalar(&vol); return vol; }
~WasapiPlayer() { if (pClient) { pClient->Stop(); pClient->Release(); } if (pRender) pRender->Release(); if (pVolume) pVolume->Release(); if (pDevice) pDevice->Release(); if (pEnum) pEnum->Release(); CoUninitialize(); } };
|