-
Notifications
You must be signed in to change notification settings - Fork 58
/
mute_current_application.ahk
119 lines (103 loc) · 4.71 KB
/
mute_current_application.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#Include %A_ScriptDir%\VA.ahk
F1:: ; F1 hotkey - toggle mute state of active window
WindowEXE := WinExist("A")
ControlGetFocus, FocusedControl, ahk_id %WindowEXE%
ControlGet, Hwnd, Hwnd,, %FocusedControl%, ahk_id %WindowEXE%
WinGet, simplexe, processname, ahk_id %Hwnd%
if !(Volume := GetVolumeObject(simplexe))
ToolTip, There was a problem retrieving the application volume interface
SetTimer, RemoveToolTip, 500 ; Display the tooltip for 3 seconds
VA_ISimpleAudioVolume_GetMute(Volume, Mute) ;Get mute state
; Msgbox % "Application " simplexe " is currently " (mute ? "muted" : "not muted")
VA_ISimpleAudioVolume_SetMute(Volume, !Mute) ;Toggle mute state
ObjRelease(Volume)
return
GetVolumeObject(targetExeName) {
static IID_IASM2 := "{77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F}"
, IID_IASC2 := "{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}"
, IID_ISAV := "{87CE5498-68D6-44E5-9215-6DA47EF883D8}"
; Get all audio devices
Loop, 10 ; Change the loop limit based on the number of audio devices you have
{
DAE := VA_GetDevice(A_Index)
if (DAE)
{
; Check if the device is active and a rendering endpoint
VA_IMMDevice_GetState(DAE, State)
VA_IConnector_GetDataFlow(DAE, DataFlow)
if (State == 1 && DataFlow == 0) ; Check if the device is active and rendering
{
; Activate the session manager
VA_IMMDevice_Activate(DAE, IID_IASM2, 0, 0, IASM2)
; Enumerate sessions for the current device
VA_IAudioSessionManager2_GetSessionEnumerator(IASM2, IASE)
VA_IAudioSessionEnumerator_GetCount(IASE, Count)
; Search for an audio session with the required name for the current device
Loop, % Count
{
VA_IAudioSessionEnumerator_GetSession(IASE, A_Index-1, IASC)
IASC2 := ComObjQuery(IASC, IID_IASC2)
; If IAudioSessionControl2 is queried successfully
if (IASC2)
{
VA_IAudioSessionControl2_GetProcessID(IASC2, SPID)
ProcessNameFromPID := GetProcessNameFromPID(SPID)
; If the process name matches the one we are looking for
if (ProcessNameFromPID == targetExeName)
{
; Check if the session is active before retrieving volume interface
VA_IAudioSessionControl_GetState(IASC2, SessionState)
if (SessionState == 1) ; AudioSessionStateActive
{
ISAV := ComObjQuery(IASC2, IID_ISAV)
if (ISAV)
{
return ISAV ;
}
else
{
return
}
}
ObjRelease(IASC2)
}
ObjRelease(IASC2)
}
ObjRelease(IASC)
}
}
ObjRelease(IASE)
ObjRelease(IASM2)
ObjRelease(DAE)
}
}
; MsgBox No active audio session found for the specified process: %targetExeName%
return ; Return 0 if there's an issue retrieving the interface
}
GetProcessNameFromPID(PID)
{
hProcess := DllCall("OpenProcess", "UInt", 0x0400 | 0x0010, "Int", false, "UInt", PID)
VarSetCapacity(ExeName, 260, 0)
DllCall("Psapi.dll\GetModuleFileNameEx", "UInt", hProcess, "UInt", 0, "Str", ExeName, "UInt", 260)
DllCall("CloseHandle", "UInt", hProcess)
return SubStr(ExeName, InStr(ExeName, "\", false, -1) + 1)
}
RemoveToolTip:
ToolTip ; Remove the tooltip
SetTimer, RemoveToolTip, Off ; Turn off the timer
return
;
; ISimpleAudioVolume : {87CE5498-68D6-44E5-9215-6DA47EF883D8}
;
VA_ISimpleAudioVolume_SetMasterVolume(this, ByRef fLevel, GuidEventContext="") {
return DllCall(NumGet(NumGet(this+0)+3*A_PtrSize), "ptr", this, "float", fLevel, "ptr", VA_GUID(GuidEventContext))
}
VA_ISimpleAudioVolume_GetMasterVolume(this, ByRef fLevel) {
return DllCall(NumGet(NumGet(this+0)+4*A_PtrSize), "ptr", this, "float*", fLevel)
}
VA_ISimpleAudioVolume_SetMute(this, ByRef Muted, GuidEventContext="") {
return DllCall(NumGet(NumGet(this+0)+5*A_PtrSize), "ptr", this, "int", Muted, "ptr", VA_GUID(GuidEventContext))
}
VA_ISimpleAudioVolume_GetMute(this, ByRef Muted) {
return DllCall(NumGet(NumGet(this+0)+6*A_PtrSize), "ptr", this, "int*", Muted)
}