Last active
June 11, 2021 06:05
-
-
Save mr-salty/b76cb505acebe58ad57e832f018053b5 to your computer and use it in GitHub Desktop.
Prints a table mapping Windows volume level (0-100) to dB for the default audio device
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 characters
// volume_test.cpp: prints a level (0-100) to dB table for the default audio device | |
// I just built this as a console app in VS2019. | |
// Original code from https://github.com/shubham-theway/Codes-here-there/blob/master/volctrl.cpp | |
// | |
// TODO: error checking, device selection, print more device info, ? | |
#include <stdio.h> | |
#include <windows.h> | |
#include <mmdeviceapi.h> | |
#include <endpointvolume.h> | |
int main() { | |
HRESULT hr; | |
hr = CoInitialize(nullptr); | |
IMMDeviceEnumerator* deviceEnumerator; | |
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID*)&deviceEnumerator); | |
IMMDevice* defaultDevice; | |
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice); | |
deviceEnumerator->Release(); | |
deviceEnumerator = nullptr; | |
IAudioEndpointVolume* endpointVolume; | |
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID*)&endpointVolume); | |
defaultDevice->Release(); | |
defaultDevice = nullptr; | |
float min, max, inc; | |
endpointVolume->GetVolumeRange(&min, &max, &inc); | |
printf("volume range in db: min %f max %f inc %f\n", min, max, inc); | |
// store original volume to restore it later | |
float original; | |
endpointVolume->GetMasterVolumeLevel(&original); | |
// cycle through the entire volume range, printing scalar and DB values | |
endpointVolume->SetMasterVolumeLevel(min, NULL); | |
float cur; | |
do { | |
endpointVolume->GetMasterVolumeLevel(&cur); | |
float scalar; | |
endpointVolume->GetMasterVolumeLevelScalar(&scalar); | |
printf("%3d %8.2f\n", static_cast<int>(scalar * 100), cur); | |
endpointVolume->VolumeStepUp(NULL); | |
} while (cur < max); | |
endpointVolume->SetMasterVolumeLevel(original, NULL); | |
endpointVolume->Release(); | |
CoUninitialize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote this because I was trying to figure out how much attenuation I needed to match the output of my PC/DAC to my turntable, so I could just mix them passively together. The results from my machine (windows 10, Schiit Modi 3 so I think it's the software volume control) are below. I had been keeping the volume around 50 so I probably need a ~10dB attenuator