Advertisement
hiro1357

command-line volume control tool for windows (vctl.cs)

Jul 7th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. # https://stackoverflow.com/questions/21355891/change-audio-level-from-powershell
  2. # csc -out:vctl.exe vctl.cs
  3. # usage: vctl.exe 30 0   ...set to 30%, un-mute
  4. #        vctl.exe 30 1   ...set to 30%, mute
  5.  
  6. using System;
  7. using System.Runtime.InteropServices;
  8. [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  9. interface IAudioEndpointVolume
  10. {
  11.     // f(), g(), ... are unused COM method slots. Define these if you care
  12.     int f(); int g(); int h(); int i();
  13.     int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
  14.     int j();
  15.     int GetMasterVolumeLevelScalar(out float pfLevel);
  16.     int k(); int l(); int m(); int n();
  17.     int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
  18.     int GetMute(out bool pbMute);
  19. }
  20. [Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  21. interface IMMDevice
  22. {
  23.     int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
  24. }
  25. [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  26. interface IMMDeviceEnumerator
  27. {
  28.     int f(); // Unused
  29.     int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
  30. }
  31. [ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
  32. public class Audio
  33. {
  34.     static IAudioEndpointVolume Vol()
  35.     {
  36.         var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
  37.         IMMDevice dev = null;
  38.         Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
  39.         IAudioEndpointVolume epv = null;
  40.         var epvid = typeof(IAudioEndpointVolume).GUID;
  41.         Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
  42.         return epv;
  43.     }
  44.     public static float Volume
  45.     {
  46.         get { float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v; }
  47.         set { Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty)); }
  48.     }
  49.     public static bool Mute
  50.     {
  51.         get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
  52.         set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
  53.     }
  54. }
  55.  
  56. public class VCtl
  57. {
  58.     public static void Main(string[] args)
  59.     {
  60.         int vol;
  61.         int mute;
  62.         if (args.Length < 2) {
  63.             return;
  64.         }
  65.  
  66.         Int32.TryParse(args[0], out vol);
  67.         Int32.TryParse(args[1], out mute);
  68.         Audio.Volume = vol/ (float)100;
  69.         if (0 < mute) {
  70.             Audio.Mute = true;
  71.         } else {
  72.             Audio.Mute = false;
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement