Has anyone else experienced significant harmonic distortion with WASAPI loopback?

3 weeks ago 18
ARTICLE AD BOX

I am using WASAPI loopback for real time audio visualization, and I am seeing some pretty significant distortion. When I play back a 25Hz tone from Audacity (and other programs) and record the samples captured through WASAPI, I am seeing a lots of harmonics, with the highest being at -46db. I am using Windows 11, and my sound card it set to 16-bit 44100Hz and spatial sound is disabled, but I've tested other combinations with no effect. This seems incredibly bad to me.

My code looks something like this:

int main() { init_apartment(); com_ptr<IMMDeviceEnumerator> deviceEnumerator; check_hresult(CoCreateInstance( __uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), deviceEnumerator.put_void())); com_ptr<IMMDevice> device; check_hresult(deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, device.put())); com_ptr<IAudioClient> audioClient; check_hresult(device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, audioClient.put_void())); std::unique_ptr<WAVEFORMATEX, CoTaskMemDeleter> format; check_hresult(audioClient->GetMixFormat(std::out_ptr(format))); check_hresult(audioClient->Initialize( AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, 0, 0, format.get(), NULL)); while (true) { auto nextPacketSize = [&] { UINT32 packetSize = 0; check_hresult(captureClient->GetNextPacketSize(&packetSize)); return packetSize; }; while (nextPacketSize() > 0) { BYTE* pData = nullptr; UINT32 numFrames = 0; DWORD flags = 0; check_hresult(captureClient->GetBuffer(&pData, &numFrames, &flags, NULL, NULL)); auto samples = reinterpret_cast<float*>(pData); for (std::size_t i = 0; i < numFrames; ++i) { //process samples } check_hresult(captureClient->ReleaseBuffer(numFrames)); } } }

enter image description here

Read Entire Article