Skip to content

Instantly share code, notes, and snippets.

@mr-salty
Last active June 11, 2021 06:05
Show Gist options
  • Save mr-salty/b76cb505acebe58ad57e832f018053b5 to your computer and use it in GitHub Desktop.
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
// 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;
}
@mr-salty
Copy link
Author

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

volume range in db: min -96.000000 max 0.000000 inc 1.500000
  0   -96.00
  2   -58.17
  4   -48.28
  6   -42.34
  8   -38.08
 10   -34.75
 12   -32.03
 14   -29.72
 15   -27.71
 18   -25.94
 20   -24.35
 22   -22.92
 24   -21.61
 25   -20.40
 28   -19.28
 29   -18.24
 32   -17.26
 34   -16.34
 36   -15.48
 38   -14.66
 40   -13.89
 42   -13.15
 44   -12.44
 46   -11.77
 48   -11.13
 50   -10.51
 52    -9.91
 54    -9.34
 56    -8.79
 58    -8.26
 60    -7.75
 62    -7.25
 64    -6.77
 66    -6.30
 68    -5.85
 70    -5.41
 72    -4.98
 74    -4.57
 76    -4.16
 78    -3.77
 80    -3.38
 82    -3.01
 84    -2.64
 86    -2.29
 88    -1.94
 90    -1.60
 92    -1.27
 94    -0.94
 96    -0.62
 98    -0.31
100     0.00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment