3

How can I get the current master volume of the system in Windows 7?

I looked up in Google, but every solution returned values like -1 or 4686346 without a clear explanation of what they mean.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Code0987
  • 2,598
  • 3
  • 33
  • 51
  • 2
    I suggest you read your question and ask yourself - will anyone understand my question and be able to answer it? http://tinyurl.com/so-hints – Oded Jan 09 '11 at 16:00
  • Please define "current sound". It sounds like you found a solution to return discrete sample values - if you don't need that, what do you need ? – driis Jan 09 '11 at 16:00
  • Your question isn't particularly clear on what the problem is. Please re-edit your question and include things like: What, exactly, it is you're trying to do. What things you've tried (a short code sample would help, or a link to another site that shows someone else doing something similar). What didn't work with the things you've tried: What were you expecting to see, and what was the actual result? –  Jan 09 '11 at 16:01
  • Do you want the current volume setting? – Cody Gray - on strike Jan 09 '11 at 16:03
  • http://answers.yahoo.com/question/index?qid=20100622135700AAWFVpi This should help, I guess – Mahesh Jan 09 '11 at 16:09
  • I think is the same question with this [one][1]! [1]: http://stackoverflow.com/questions/2534595/get-master-sound-volume-in-c – Iraklis Jan 10 '11 at 12:05

4 Answers4

10

Since you put C# as a tag, here is a small C# console app that gets it. It's based on the GetMasterVolumeLevelScalar method (Vista or higher).

The GetMasterVolumeLevelScalar method gets the master volume level of the audio stream that enters or leaves the audio endpoint device. The volume level is expressed as a normalized, audio-tapered value in the range from 0.0 to 1.0.

  class Program
  {
      static void Main(string[] args)
      {
          Console.WriteLine(VolumeUtilities.GetMasterVolume());
      }
  }


  public static class VolumeUtilities
  {
      public static float GetMasterVolume()
      {
          // get the speakers (1st render + multimedia) device
          IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
          IMMDevice speakers;
          const int eRender = 0;
          const int eMultimedia = 1;
          deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out speakers);

          object o;
          speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out o);
          IAudioEndpointVolume aepv = (IAudioEndpointVolume)o;
          float volume = aepv.GetMasterVolumeLevelScalar();
          Marshal.ReleaseComObject(aepv);
          Marshal.ReleaseComObject(speakers);
          Marshal.ReleaseComObject(deviceEnumerator);
          return volume;
      }

      [ComImport]
      [Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
      private class MMDeviceEnumerator
      {
      }

      [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      private interface IAudioEndpointVolume
      {
          void _VtblGap1_6();
          float GetMasterVolumeLevelScalar();
      }

      [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      private interface IMMDeviceEnumerator
      {
          void _VtblGap1_1();

          [PreserveSig]
          int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice ppDevice);
      }

      [Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      private interface IMMDevice
      {
          [PreserveSig]
          int Activate([MarshalAs(UnmanagedType.LPStruct)] Guid iid, int dwClsCtx, IntPtr pActivationParams, [MarshalAs(UnmanagedType.IUnknown)] out object ppInterface);
      }
  }
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
10

You're looking for the EndpointVolume API. This is part of the new audio APIs that were released in Windows Vista, and it can be used to get or set the master volume.

This undertaking is made considerably easier by the fact that you do not need to support versions of Windows prior to Vista (namely Windows XP), because there were substantial changes made to the relevant infrastructure between those OS releases. This is likely the reason why the existing samples you've tried didn't work properly.

There's a complete managed wrapper library available on CodeProject: Vista Core Audio API Master Volume Control. It probably implements more functionality than you need, but you can get an idea of what you need to do to determine the master system volume from your C# application.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
9

Ok, for those looking for a command line or C++ option, here's some source using the IAudioEndpointVolume API.

Turns out there is are "scalar" methods that work well, and returns a number from 0 to 100 (with getters and setters).

source: https://gist.github.com/rdp/8363580

Command line build of it here.

See also possibly https://stackoverflow.com/a/7550110/32453

Community
  • 1
  • 1
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • Can't seem to capture output, eg using `getvol.exe > file` the file is empty. Any ideas? – NateS Jun 14 '15 at 03:28
  • [Here](http://stackoverflow.com/a/17578048/187883) is the problem. Can you pretty please add a flush and recompile? My mingw32 can't build it and I don't want to break my working mingw32 setup playing with mingw64. Cheers! – NateS Jun 14 '15 at 03:34
  • 1
    hmm seems to work OK here [vista] redirected to a file, this one has an added flush: https://sourceforge.net/projects/mplayer-edl/files/current_system_volume_vista_plus_flush.exe/download – rogerdpack Jun 24 '15 at 10:55
  • Great, thank you! I've verified your new version can now be redirected to a file. Cheers! – NateS Jun 24 '15 at 14:41
0

You can get & set Windows volume via Windows Core Audio API
C++ program example:

#include <cstdio>
#include <Windows.h>
#include <MMDeviceAPI.h>
#include <EndpointVolume.h>

int main(int argc, char* argv[])
{
    // Initialize the COM library
    CoInitialize(NULL);  

    IMMDeviceEnumerator* pEnumerator;
    IMMDevice* pDevice;
    IAudioEndpointVolume* pEndpointVolume;

    // Create a device enumerator
    CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnumerator);

    // Get the default audio endpoint device
    pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);

    // Activate the IAudioEndpointVolume interface
    pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&pEndpointVolume);

    if ( argc == 1 ) {
        // Get the master volume level
        float currentVolume;
        pEndpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
        printf("Current sound volume: %.1f\n\n", currentVolume);
    }

    if ( argc == 2 ) {
        float volume = strtof(argv[1], NULL);
        // Verify valid input range 0~1
        if ( ! ( 0.f <= volume && volume <= 1.f ) )
            return 1;
        pEndpointVolume->SetMasterVolumeLevelScalar(volume, NULL);
    }

    CoUninitialize();
}