Unit uMasterMuteW7;
{$MODE OBJFPC}{$H+}
Interface
USES
Classes, SysUtils, Forms, Controls,
Windows, ActiveX, ShellApi;
TYPE
TMyBOOL = (myFalse = Integer(0),
myTrue = Integer(1));
TYPE
TwndGUI = Class(TForm)
Procedure SetMasterVolume ( sinVol: Single );
Procedure GetMasterVolume (Out sinVol: Single );
Function GetMasterMute : TMyBOOL;
Procedure SetMasterMute ( Value: TMyBOOL);
Procedure FormKeyDown (Sender: TObject; Var Key: Word;
Shift : TShiftState);
End;
// WINDOWS 7 VOLUME BEGIN
TYPE
IAudioEndpointVolumeCallback = Interface(IUnknown)
['{657804FA-D6AD-4496-8A60-352752AF4F89}']
End;
IAudioEndpointVolume = Interface(IUnknown)
['{5CDF2C82-841E-4546-9722-0CF74078229A}']
Function RegisterControlChangeNotify(
AudioEndPtVol: IAudioEndpointVolumeCallback): HRESULT; StdCall;
Function UnregisterControlChangeNotify(
AudioEndPtVol: IAudioEndpointVolumeCallback): HRESULT; StdCall;
Function GetChannelCount(Out PInteger): HRESULT; StdCall;
Function SetMasterVolumeLevel(
fLevelDB: Single; pguidEventContext: PGUID): HRESULT; StdCall;
Function SetMasterVolumeLevelScalar(
fLevelDB: Single; pguidEventContext: PGUID): HRESULT; StdCall;
Function GetMasterVolumeLevel(
Out fLevelDB: Single): HRESULT; StdCall;
Function GetMasterVolumeLevelScalar(
Out fLevelDB: Single): HRESULT; StdCall;
Function SetChannelVolumeLevel(
nChannel: Integer; fLevelDB: Double; pguidEventContext: PGUID):
HRESULT; StdCall;
Function SetChannelVolumeLevelScalar(
nChannel: Integer; fLevelDB: Double; pguidEventContext: PGUID):
HRESULT; StdCall;
Function GetChannelVolumeLevel(
nChannel: Integer; Out fLevelDB: Double): HRESULT; StdCall;
Function GetChannelVolumeLevelScalar(
nChannel: Integer; Out fLevelDB: Double): HRESULT; StdCall;
Function SetMute(
bSetMute: Bool; pguidEventContext: PGUID): HRESULT; StdCall;
Function GetMute(
Out bGetMute: Bool): HRESULT; StdCall;
Function GetVolumeStepInfo(
pnStep: Integer; Out pnStepCount: Integer): HRESULT; StdCall;
Function VolumeStepUp(
pguidEventContext: PGUID): HRESULT; StdCall;
Function VolumeStepDown(
pguidEventContext: PGUID): HRESULT; StdCall;
Function GetVolumeRange(
Out pflVolumeMindB: Double; Out pflVolumeMaxdB: Double;
Out pflVolumeIncrementdB: Double): HRESULT; StdCall;
Function QueryHardwareSupport(
Out pdwHardwareSupportMask): HRESULT; StdCall;
End;
IAudioMeterInformation = Interface(IUnknown)
['{C02216F6-8C67-4B5B-9D00-D008E73E0064}']
End;
IPropertyStore = Interface(IUnknown)
End;
IMMDevice = Interface(IUnknown)
['{D666063F-1587-4E43-81F1-B948E807363F}']
Function Activate(
Const refId: TGUID; dwClsCtx: DWORD; pActivationParams: PInteger;
Out pEndpointVolume: IAudioEndpointVolume): HRESULT; StdCall;
Function OpenPropertyStore(
stgmAccess: DWORD; Out ppProperties: IPropertyStore):
HRESULT; StdCall;
Function GetId (Out ppstrId: PLPWSTR): HRESULT; StdCall;
Function GetState(Out State: Integer): HRESULT; StdCall;
End;
IMMDeviceCollection = interface(IUnknown)
['{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}']
End;
IMMNotificationClient = interface(IUnknown)
['{7991EEC9-7E89-4D85-8390-6C703CEC60C0}']
End;
IMMDeviceEnumerator = interface(IUnknown)
['{A95664D2-9614-4F35-A746-DE8DB63617E6}']
Function EnumAudioEndpoints(
dataFlow: TOleEnum; deviceState: SYSUINT; DevCollection:
IMMDeviceCollection): HRESULT; StdCall;
Function GetDefaultAudioEndpoint(
EDF: SYSUINT; ER: SYSUINT; Out Dev: IMMDevice): HRESULT; StdCall;
Function GetDevice(pwstrId: Pointer; Out Dev: IMMDevice): HRESULT; StdCall;
Function RegisterEndpointNotificationCallback(
pClient: IMMNotificationClient): HRESULT; StdCall;
End;
CONST
CLASS_IMMDeviceEnumerator: TGUID = '{BCDE0395-E52F-467C-8E3D-C4579291692E}';
IID_IMMDeviceEnumerator : TGUID = '{A95664D2-9614-4F35-A746-DE8DB63617E6}';
IID_IAudioEndpointVolume : TGUID = '{5CDF2C82-841E-4546-9722-0CF74078229A}';
// WINDOWS 7 VOLUME END
VAR
wndGUI: TwndGUI;
Implementation
{$R *.LFM}
Procedure TwndGUI.FormKeyDown(Sender: TObject; Var Key: Word;
Shift : TShiftState);
Var
myMute: TMyBOOL;
sinVol: Single;
Begin
// MIXER
If Key = Ord('V')
Then ShellExecute(0, Nil, PChar('sndvol.exe'), '', '', SW_SHOWNORMAL);
// MUTE
If Key = Ord('M')
Then
Begin
myMute:= GetMasterMute;
If myMute = myTrue
Then SetMasterMute(myFalse)
Else SetMasterMute(myTrue);
End;
// MAX VOLUME
If Key = Ord('F')
Then
Begin
myMute:= GetMasterMute;
If myMute = myTrue
Then SetMasterMute(myFalse);
SetMasterVolume(1.0);
End;
// VOLUME UP 1%
If Key = VK_UP
Then
Begin
GetMasterVolume(sinVol);
If (1-sinVol) < 0.01
Then sinVol:= 1
Else sinVol:= sinVol+0.01;
SetMasterVolume(sinVol);
End;
// VOLUME DOWN 1%
If Key = VK_DOWN
Then
Begin
GetMasterVolume(sinVol);
If (sinVol-0.01) < 0
Then sinVol:= 0
Else sinVol:= sinVol-0.01;
SetMasterVolume(sinVol);
End;
End;
Procedure TwndGUI.SetMasterVolume(sinVol: Single);
Var
EndpointVolume : IAudioEndpointVolume;
DeviceEnumerator: IMMDeviceEnumerator;
Device : IMMDevice;
Begin
CoCreateInstance(
CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
IID_IMMDeviceEnumerator, DeviceEnumerator);
DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
Device.Activate(
IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
EndpointVolume.SetMasterVolumeLevelScalar(sinVol, Nil);
End;
Procedure TwndGUI.GetMasterVolume(Out sinVol: Single);
Var
EndpointVolume : IAudioEndpointVolume;
DeviceEnumerator: IMMDeviceEnumerator;
Device : IMMDevice;
Begin
CoCreateInstance(
CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
IID_IMMDeviceEnumerator, DeviceEnumerator);
DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
Device.Activate(
IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
EndpointVolume.GetMasterVolumeLevelScalar(sinVol);
End;
Function TwndGUI.GetMasterMute: TMyBOOL;
Var
EndpointVolume : IAudioEndpointVolume;
DeviceEnumerator: IMMDeviceEnumerator;
Device : IMMDevice;
bRes: BOOL;
Begin
CoCreateInstance(
CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
IID_IMMDeviceEnumerator, DeviceEnumerator);
DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
Device.Activate(
IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
EndpointVolume.GetMute(bRes);
Result:= TMyBOOL(bRes);
End;
Procedure TwndGUI.SetMasterMute(Value: TMyBOOL);
Var
EndpointVolume : IAudioEndpointVolume;
DeviceEnumerator: IMMDeviceEnumerator;
Device : IMMDevice;
Begin
CoCreateInstance(
CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
IID_IMMDeviceEnumerator, DeviceEnumerator);
DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
Device.Activate(
IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
EndpointVolume.SetMute(BOOL(Value), Nil);
End;
Initialization
coInitialize(Nil);
Finalization
coUnInitialize;
End.