Skip to content

Instantly share code, notes, and snippets.

@rdp
Last active July 12, 2022 18:48
Show Gist options
  • Save rdp/8363580 to your computer and use it in GitHub Desktop.
Save rdp/8363580 to your computer and use it in GitHub Desktop.

Revisions

  1. rdp revised this gist Jun 24, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions output_or_adjust_current_master_volume.cpp
    Original file line number Diff line number Diff line change
    @@ -70,6 +70,7 @@ int main(int argc, char** argv) {
    else {
    printf("wrong v of windows, req. vista+ ! \n");
    }
    fflush(stdout); // just in case

    Exit:
    SAFE_RELEASE(pEnumerator)
  2. rdp revised this gist Jan 13, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions output_or_adjust_current_master_volume.cpp
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@
    // g++ this_filename.c -lole32
    // outputs current system volume (out of 0-100) to stdout, ex: output "23"
    // mostly gleaned from examples here: http://msdn.microsoft.com/en-us/library/windows/desktop/dd370839(v=vs.85).aspx
    // download a compiled version here:
    // https://sourceforge.net/projects/mplayer-edl/files/adjust_get_current_system_volume_vista_plus.exe.exe (updated, can set it too now!)
    #include <windows.h>
    #include <commctrl.h>
    #include <mmdeviceapi.h>
  3. rdp renamed this gist Jan 13, 2014. 1 changed file with 11 additions and 4 deletions.
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,6 @@
    // g++ this_filename.c -lole32
    // outputs current system volume (out of 0-100) to stdout, ex: output "23"
    // mostly gleaned from examples here: http://msdn.microsoft.com/en-us/library/windows/desktop/dd370839(v=vs.85).aspx
    // download a compiled version here:
    // https://sourceforge.net/projects/mplayer-edl/files/current_system_volume_vista_plus.exe
    #include <windows.h>
    #include <commctrl.h>
    #include <mmdeviceapi.h>
    @@ -12,14 +10,14 @@
    #include <math.h> /* log */

    #define EXIT_ON_ERROR(hr) \
    if (FAILED(hr)) { goto Exit; }
    if (FAILED(hr)) { printf("error %d occurred\n", -hr); goto Exit; }


    #define SAFE_RELEASE(punk) \
    if ((punk) != NULL) \
    { (punk)->Release(); (punk) = NULL; }

    int main(int argc, char** args) {
    int main(int argc, char** argv) {
    IAudioEndpointVolume *g_pEndptVol = NULL;
    bool WindowsLH;
    HRESULT hr = S_OK;
    @@ -54,6 +52,15 @@ int main(int argc, char** args) {
    CLSCTX_ALL, NULL, (void**)&g_pEndptVol);
    EXIT_ON_ERROR(hr)
    float currentVal;
    if(argc == 2) {
    if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "/?")==0 || strcmp(argv[1], "--help")==0 ) {
    printf("sets/gest current master volume level [vista+]\nSyntax: either no args to display current value, or a number to set it, 0-100. It prints out the new or current value.");
    goto Exit;
    }
    float got = (float) atof(argv[1])/100.0; // needs to be within 1.0 to 0.0
    hr = g_pEndptVol->SetMasterVolumeLevelScalar(got, NULL);
    EXIT_ON_ERROR(hr)
    }
    hr = g_pEndptVol->GetMasterVolumeLevelScalar(&currentVal);
    EXIT_ON_ERROR(hr)
    printf("%d", (int) round(100 * currentVal)); // 0.839999 to 84 :)
  4. rdp revised this gist Jan 10, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions output_current_master_volume.cpp
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@
    // g++ this_filename.c -lole32
    // outputs current system volume (out of 0-100) to stdout, ex: output "23"
    // mostly gleaned from examples here: http://msdn.microsoft.com/en-us/library/windows/desktop/dd370839(v=vs.85).aspx
    // download a compiled version here:
    // https://sourceforge.net/projects/mplayer-edl/files/current_system_volume_vista_plus.exe
    #include <windows.h>
    #include <commctrl.h>
    #include <mmdeviceapi.h>
  5. rdp revised this gist Jan 10, 2014. 1 changed file with 5 additions and 22 deletions.
    27 changes: 5 additions & 22 deletions output_current_master_volume.cpp
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // to compile (using mingw-w64)
    // g++ this_filename.c -lole32
    // outputs current system volume (out of 0-100) to stdout
    // mostly gleaned via "copy and paste" from http://msdn.microsoft.com/en-us/library/windows/desktop/dd370839(v=vs.85).aspx
    // outputs current system volume (out of 0-100) to stdout, ex: output "23"
    // mostly gleaned from examples here: http://msdn.microsoft.com/en-us/library/windows/desktop/dd370839(v=vs.85).aspx
    #include <windows.h>
    #include <commctrl.h>
    #include <mmdeviceapi.h>
    @@ -51,30 +51,13 @@ int main(int argc, char** args) {
    hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),
    CLSCTX_ALL, NULL, (void**)&g_pEndptVol);
    EXIT_ON_ERROR(hr)
    float pfLevelMinDB;
    float pfLevelMaxDB;
    float pfVolumeIncrementDB;
    hr = g_pEndptVol->GetVolumeRange(&pfLevelMinDB, &pfLevelMaxDB, &pfVolumeIncrementDB);
    EXIT_ON_ERROR(hr)
    float currentVal;
    hr = g_pEndptVol->GetMasterVolumeLevel(&currentVal);
    hr = g_pEndptVol->GetMasterVolumeLevelScalar(&currentVal);
    EXIT_ON_ERROR(hr)
    printf("current master volume is %f (min %f max %f inc %f)\n", currentVal, pfLevelMinDB, pfLevelMaxDB, pfVolumeIncrementDB);
    // 50% is like current master volume is -10.419663 (min -74.000000 max 0.000000 inc 0.031250)
    // so range is 74
    // log(74) == 4.3
    // current value is log(10.4) = 2.3
    // so divide those 2 values [?]... 2.3/4.3
    float totalRange = pfLevelMaxDB - pfLevelMinDB;
    // not sure why this doesn't quite add up to the right value though...
    if(currentVal == 0.0) {
    printf("100"); // avoid log(0) below which is INF
    } else {
    printf("%f", 100-round(100*log(-currentVal-pfLevelMaxDB)/log(totalRange)));
    }
    printf("%d", (int) round(100 * currentVal)); // 0.839999 to 84 :)
    }
    else {
    printf("wrong v of windows, req. vista+\n");
    printf("wrong v of windows, req. vista+ ! \n");
    }

    Exit:
  6. rdp revised this gist Jan 10, 2014. 1 changed file with 18 additions and 4 deletions.
    22 changes: 18 additions & 4 deletions output_current_master_volume.cpp
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    // compile (mingw-w64)
    // g++ this_filename.c -luuid -loleaut32 -lole32
    // to compile (using mingw-w64)
    // g++ this_filename.c -lole32
    // outputs current system volume (out of 0-100) to stdout
    // mostly gleaned via "copy and paste" from http://msdn.microsoft.com/en-us/library/windows/desktop/dd370839(v=vs.85).aspx
    #include <windows.h>
    #include <commctrl.h>
    #include <mmdeviceapi.h>
    #include <endpointvolume.h>
    #include <stdio.h>
    // mostly gleaned via "copy and paste" from http://msdn.microsoft.com/en-us/library/windows/desktop/dd370839(v=vs.85).aspx
    #include <math.h> /* log */

    #define EXIT_ON_ERROR(hr) \
    if (FAILED(hr)) { goto Exit; }
    @@ -57,7 +59,19 @@ int main(int argc, char** args) {
    float currentVal;
    hr = g_pEndptVol->GetMasterVolumeLevel(&currentVal);
    EXIT_ON_ERROR(hr)
    printf("current master volume is %f (min %f max %f inc %f)", currentVal, pfLevelMinDB, pfLevelMaxDB, pfVolumeIncrementDB);
    printf("current master volume is %f (min %f max %f inc %f)\n", currentVal, pfLevelMinDB, pfLevelMaxDB, pfVolumeIncrementDB);
    // 50% is like current master volume is -10.419663 (min -74.000000 max 0.000000 inc 0.031250)
    // so range is 74
    // log(74) == 4.3
    // current value is log(10.4) = 2.3
    // so divide those 2 values [?]... 2.3/4.3
    float totalRange = pfLevelMaxDB - pfLevelMinDB;
    // not sure why this doesn't quite add up to the right value though...
    if(currentVal == 0.0) {
    printf("100"); // avoid log(0) below which is INF
    } else {
    printf("%f", 100-round(100*log(-currentVal-pfLevelMaxDB)/log(totalRange)));
    }
    }
    else {
    printf("wrong v of windows, req. vista+\n");
  7. rdp revised this gist Jan 10, 2014. 1 changed file with 13 additions and 17 deletions.
    30 changes: 13 additions & 17 deletions output_current_master_volume.cpp
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    // compile (mingw-w64)
    // g++ this_filename.c -luuid -loleaut32 -lole32
    #include <windows.h>
    #include <commctrl.h>
    #include <mmdeviceapi.h>
    #include <endpointvolume.h>
    #include <stdio.h>

    static IAudioEndpointVolume *g_pEndptVol = NULL;
    bool WindowsLH;
    // mostly gleaned via "copy and paste" from http://msdn.microsoft.com/en-us/library/windows/desktop/dd370839(v=vs.85).aspx

    #define EXIT_ON_ERROR(hr) \
    if (FAILED(hr)) { goto Exit; }
    @@ -16,6 +16,8 @@ bool WindowsLH;
    { (punk)->Release(); (punk) = NULL; }

    int main(int argc, char** args) {
    IAudioEndpointVolume *g_pEndptVol = NULL;
    bool WindowsLH;
    HRESULT hr = S_OK;
    IMMDeviceEnumerator *pEnumerator = NULL;
    IMMDevice *pDevice = NULL;
    @@ -25,7 +27,7 @@ int main(int argc, char** args) {
    VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&VersionInfo);
    if (VersionInfo.dwMajorVersion > 5)
    WindowsLH = true;
    WindowsLH = true; // vista+
    else
    WindowsLH = false;

    @@ -50,24 +52,18 @@ int main(int argc, char** args) {
    float pfLevelMinDB;
    float pfLevelMaxDB;
    float pfVolumeIncrementDB;
    g_pEndptVol->GetVolumeRange(&pfLevelMinDB, &pfLevelMaxDB, &pfVolumeIncrementDB);
    hr = g_pEndptVol->GetVolumeRange(&pfLevelMinDB, &pfLevelMaxDB, &pfVolumeIncrementDB);
    EXIT_ON_ERROR(hr)
    float currentVal;
    g_pEndptVol->GetMasterVolumeLevel(&currentVal);
    hr = g_pEndptVol->GetMasterVolumeLevel(&currentVal);
    EXIT_ON_ERROR(hr)
    printf("current master volume is %f (min %f max %f inc %f)", currentVal, pfLevelMinDB, pfLevelMaxDB, pfVolumeIncrementDB);
    }
    else {
    printf("wrong v of windows, req. vista+\n");
    }
    else {
    printf("wrong v of windows, req. vista+\n");
    }

    Exit:
    if (FAILED(hr))
    {
    MessageBox(NULL, TEXT("This program requires Windows Vista."),
    TEXT("Error termination"), MB_OK);
    }
    if (pEnumerator != NULL)
    {
    }
    SAFE_RELEASE(pEnumerator)
    SAFE_RELEASE(pDevice)
    SAFE_RELEASE(g_pEndptVol)
  8. rdp created this gist Jan 10, 2014.
    76 changes: 76 additions & 0 deletions output_current_master_volume.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    #include <windows.h>
    #include <commctrl.h>
    #include <mmdeviceapi.h>
    #include <endpointvolume.h>
    #include <stdio.h>

    static IAudioEndpointVolume *g_pEndptVol = NULL;
    bool WindowsLH;

    #define EXIT_ON_ERROR(hr) \
    if (FAILED(hr)) { goto Exit; }


    #define SAFE_RELEASE(punk) \
    if ((punk) != NULL) \
    { (punk)->Release(); (punk) = NULL; }

    int main(int argc, char** args) {
    HRESULT hr = S_OK;
    IMMDeviceEnumerator *pEnumerator = NULL;
    IMMDevice *pDevice = NULL;
    OSVERSIONINFO VersionInfo;

    ZeroMemory(&VersionInfo, sizeof(OSVERSIONINFO));
    VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&VersionInfo);
    if (VersionInfo.dwMajorVersion > 5)
    WindowsLH = true;
    else
    WindowsLH = false;

    if (WindowsLH)
    {
    CoInitialize(NULL);

    // Get enumerator for audio endpoint devices.
    hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
    NULL, CLSCTX_INPROC_SERVER,
    __uuidof(IMMDeviceEnumerator),
    (void**)&pEnumerator);
    EXIT_ON_ERROR(hr)

    // Get default audio-rendering device.
    hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
    EXIT_ON_ERROR(hr)

    hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),
    CLSCTX_ALL, NULL, (void**)&g_pEndptVol);
    EXIT_ON_ERROR(hr)
    float pfLevelMinDB;
    float pfLevelMaxDB;
    float pfVolumeIncrementDB;
    g_pEndptVol->GetVolumeRange(&pfLevelMinDB, &pfLevelMaxDB, &pfVolumeIncrementDB);
    float currentVal;
    g_pEndptVol->GetMasterVolumeLevel(&currentVal);
    printf("current master volume is %f (min %f max %f inc %f)", currentVal, pfLevelMinDB, pfLevelMaxDB, pfVolumeIncrementDB);
    }
    else {
    printf("wrong v of windows, req. vista+\n");
    }

    Exit:
    if (FAILED(hr))
    {
    MessageBox(NULL, TEXT("This program requires Windows Vista."),
    TEXT("Error termination"), MB_OK);
    }
    if (pEnumerator != NULL)
    {
    }
    SAFE_RELEASE(pEnumerator)
    SAFE_RELEASE(pDevice)
    SAFE_RELEASE(g_pEndptVol)
    CoUninitialize();
    return 0;
    }