Subversion Repositories alarming

Rev

Blame | Last modification | View Log | RSS feed

  1. unit AudioVolCntrl;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, SysUtils;
  7.  
  8. procedure MuteAllAudioDevices(bMute: boolean);
  9.  
  10. implementation
  11.  
  12. uses
  13.   ActiveX, ComObj;
  14.  
  15. type
  16.   // https://stackoverflow.com/questions/49310147/iaudioendpointvolume-setmode-fails-with-a-call-to-an-os-function-failed
  17.   BOOL = Cardinal;
  18.  
  19. const
  20.   CLSID_MMDeviceEnumerator : TGUID = '{BCDE0395-E52F-467C-8E3D-C4579291692E}';
  21.   IID_IMMDeviceEnumerator : TGUID = '{A95664D2-9614-4F35-A746-DE8DB63617E6}';
  22.   IID_IMMDevice : TGUID = '{D666063F-1587-4E43-81F1-B948E807363F}';
  23.   IID_IMMDeviceCollection : TGUID = '{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}';
  24.   IID_IAudioEndpointVolume : TGUID = '{5CDF2C82-841E-4546-9722-0CF74078229A}';
  25.   IID_IAudioMeterInformation : TGUID = '{C02216F6-8C67-4B5B-9D00-D008E73E0064}';
  26.   IID_IAudioEndpointVolumeCallback : TGUID = '{657804FA-D6AD-4496-8A60-352752AF4F89}';
  27.   IID_IMMNotificationClient : TGUID = '{7991EEC9-7E89-4D85-8390-6C703CEC60C0}';
  28.  
  29.   DEVICE_STATE_ACTIVE = $00000001;
  30.   DEVICE_STATE_UNPLUGGED = $00000002;
  31.   DEVICE_STATE_NOTPRESENT = $00000004;
  32.   DEVICE_STATEMASK_ALL = $00000007;
  33.  
  34. type
  35.   EDataFlow = TOleEnum;
  36.  
  37. const
  38.   eRender = $00000000;
  39.   eCapture = $00000001;
  40.   eAll = $00000002;
  41.   EDataFlow_enum_count = $00000003;
  42.  
  43. type
  44.   ERole = TOleEnum;
  45.  
  46. const
  47.   eConsole = $00000000;
  48.   eMultimedia = $00000001;
  49.   eCommunications = $00000002;
  50.   ERole_enum_count = $00000003;
  51.  
  52. type
  53.   IAudioEndpointVolumeCallback = interface(IUnknown)
  54.   ['{657804FA-D6AD-4496-8A60-352752AF4F89}']
  55.   end;
  56.  
  57.   IAudioEndpointVolume = interface(IUnknown)
  58.   ['{5CDF2C82-841E-4546-9722-0CF74078229A}']
  59.     function RegisterControlChangeNotify(AudioEndPtVol: IAudioEndpointVolumeCallback): HRESULT; stdcall;
  60.     function UnregisterControlChangeNotify(AudioEndPtVol: IAudioEndpointVolumeCallback): HRESULT; stdcall;
  61.     function GetChannelCount(out PInteger): HRESULT; stdcall;
  62.     function SetMasterVolumeLevel(fLevelDB: single; pguidEventContext: PGUID): HRESULT; stdcall;
  63.     function SetMasterVolumeLevelScalar(fLevelDB: single; pguidEventContext: PGUID): HRESULT; stdcall;
  64.     function GetMasterVolumeLevel(out fLevelDB: single): HRESULT; stdcall;
  65.     function GetMasterVolumeLevelScaler(out fLevelDB: single): HRESULT; stdcall;
  66.     function SetChannelVolumeLevel(nChannel: Integer; fLevelDB: Single; pguidEventContext: PGUID): HRESULT; stdcall;
  67.     function SetChannelVolumeLevelScalar(nChannel: Integer; fLevelDB: Single; pguidEventContext: PGUID): HRESULT; stdcall;
  68.     function GetChannelVolumeLevel(nChannel: Integer; out fLevelDB: Single): HRESULT; stdcall;
  69.     function GetChannelVolumeLevelScalar(nChannel: Integer; out fLevel: Single): HRESULT; stdcall;
  70.     function SetMute(bMute: Boolean(*!*); pguidEventContext: PGUID): HRESULT; stdcall;
  71.     function GetMute(out bMute: Boolean(*!*)): HRESULT; stdcall;
  72.     function GetVolumeStepInfo(pnStep: Integer; out pnStepCount: Integer): HRESULT; stdcall;
  73.     function VolumeStepUp(pguidEventContext: PGUID): HRESULT; stdcall;
  74.     function VolumeStepDown(pguidEventContext: PGUID): HRESULT; stdcall;
  75.     function QueryHardwareSupport(out pdwHardwareSupportMask): HRESULT; stdcall;
  76.     function GetVolumeRange(out pflVolumeMindB: Single; out pflVolumeMaxdB: Single; out pflVolumeIncrementdB: Single): HRESULT; stdcall;
  77.   end;
  78.  
  79.   IAudioMeterInformation = interface(IUnknown)
  80.   ['{C02216F6-8C67-4B5B-9D00-D008E73E0064}']
  81.     function GetPeakValue(out Peak: Real): HRESULT; stdcall;
  82.   end;
  83.  
  84.   IPropertyStore = interface(IUnknown)
  85.   end;
  86.  
  87.   IMMDevice = interface(IUnknown)
  88.   ['{D666063F-1587-4E43-81F1-B948E807363F}']
  89.     function Activate(const refId: TGUID; dwClsCtx: DWORD; pActivationParams: PInteger; out pEndpointVolume: IAudioEndpointVolume): HRESULT; stdCall;
  90.     function OpenPropertyStore(stgmAccess: DWORD; out ppProperties: IPropertyStore): HRESULT; stdcall;
  91.     function GetId(out ppstrId: PLPWSTR): HRESULT; stdcall;
  92.     function GetState(out State: Integer): HRESULT; stdcall;
  93.   end;
  94.  
  95.   IMMDeviceCollection = interface(IUnknown)
  96.   ['{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}']
  97.     function GetCount(out pcDevices: UINT): HRESULT; stdcall;
  98.     function Item(nDevice: UINT; out ppDevice: IMMDevice): HRESULT; stdcall;
  99.   end;
  100.  
  101.   IMMNotificationClient = interface(IUnknown)
  102.   ['{7991EEC9-7E89-4D85-8390-6C703CEC60C0}']
  103.   end;
  104.  
  105.   IMMDeviceEnumerator = interface(IUnknown)
  106.   ['{A95664D2-9614-4F35-A746-DE8DB63617E6}']
  107.     function EnumAudioEndpoints(dataFlow: EDataFlow; deviceState: SYSUINT; out DevCollection: IMMDeviceCollection): HRESULT; stdcall;
  108.     function GetDefaultAudioEndpoint(EDF: SYSUINT; ER: SYSUINT; out Dev: IMMDevice): HRESULT; stdcall;
  109.     function GetDevice(pwstrId: pointer; out Dev: IMMDevice): HRESULT; stdcall;
  110.     function RegisterEndpointNotificationCallback(pClient: IMMNotificationClient): HRESULT; stdcall;
  111.     function UnregisterEndpointNotificationCallback(pClient: IMMNotificationClient): HRESULT; stdcall;
  112.   end;
  113.  
  114. procedure MuteAllAudioDevices(bMute: boolean);
  115. var
  116.   MMDeviceCollection: IMMDeviceCollection;
  117.   MMDeviceEnumerator: IMMDeviceEnumerator;
  118.   nDevCount: UINT;
  119.   dev: IMMdevice;
  120.   pEndpointVolume: IAudioEndpointVolume;
  121.   iDev: Integer;
  122. begin
  123.   MMDeviceEnumerator := nil;
  124.   OleCheck(CoCreateInstance(CLSID_MMDeviceEnumerator, nil, CLSCTX_ALL, IID_IMMDeviceEnumerator, MMDeviceEnumerator));
  125.  
  126.   MMDeviceCollection := nil;
  127.   OleCheck(MMDeviceEnumerator.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, MMDeviceCollection));
  128.  
  129.   OleCheck(MMDeviceCollection.GetCount(nDevCount));
  130.  
  131.   for iDev := 0 to nDevCount - 1 do
  132.   begin
  133.     OleCheck(MMDeviceCollection.item(iDev, dev));
  134.     OleCheck(Dev.Activate(IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, nil, pEndpointVolume));
  135.     OleCheck(pEndpointVolume.SetMute(bMute, nil));
  136.   end;
  137. end;
  138.  
  139. end.
  140.