-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch_audio_device-ctrl+shift+alt+m.ahk
More file actions
230 lines (189 loc) · 11.3 KB
/
switch_audio_device-ctrl+shift+alt+m.ahk
File metadata and controls
230 lines (189 loc) · 11.3 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#Requires AutoHotkey v2.0-a136-feda41f4
; ──────────────────────────────────────────────────────────────
; Source: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=49980
; Related references:
; - http://www.daveamenta.com/2011-05/programmatically-or-command-line-change-the-default-sound-playback-device-in-windows-7/
; - https://web.archive.org/web/20190317012739/http://www.daveamenta.com/2011-05/programmatically-or-command-line-change-the-default-sound-playback-device-in-windows-7/
; ──────────────────────────────────────────────────────────────
; ──────────────────────────────────────────────────────────────
; INITIALIZATION AND SETUP
; ──────────────────────────────────────────────────────────────
#SingleInstance force ; Replace existing instance if running
#NoTrayIcon ; Hide tray icon
; Include GUI helper for notifications
#Include %A_ScriptDir%\NotificationGUIs\WiseGUI.ahk
; Get a list of all audio endpoints (input/output devices)
List := EnumAudioEndpoints()
; ──────────────────────────────────────────────────────────────
; DEVICE CONFIGURATION
; ──────────────────────────────────────────────────────────────
; DEBUG STEP (DO THIS FIRST):
; To display the exact names and IDs of all I/O devices in a MsgBox, uncomment the below:
;
; Devices := ""
; for Device in List
; Devices .= Format("{} ({})`n`n", Device["Name"], Device["ID"])
; MsgBox(Devices)
; Device definitions (modify these to match your system)
Device1 := "1 - LG HDR WFHD (AMD High Definition Audio Device)" ; My Speaker
Device2 := "Speakers (Realtek(R) Audio)" ; My Headphones (Backside)
; Device2 := "Realtek HD Audio 2nd output (Realtek(R) Audio)" ; My Headphones (Frontside)
; Set initial device and tracking variable
SetDefaultEndpoint(GetDeviceID(List, Device2)) ; Sets default to headphones on startup
selectedDevice := 2
; ──────────────────────────────────────────────────────────────
; HOTKEY DEFINITIONS
; ──────────────────────────────────────────────────────────────
; Audio Device Toggle (Ctrl + LShift + Alt + M)
^<+!m::
{
global selectedDevice
if (selectedDevice = 1) {
; Switch to headphones
SetDefaultEndpoint(GetDeviceID(List, Device2))
selectedDevice := 2
WiseGui("Info", "Margins: 4,4,-3,3", "MainText: Switched to Headphones", "Move: , 10", "TextWidth: 155", "Timer: 1000")
}
else {
; Switch to speakers
SetDefaultEndpoint(GetDeviceID(List, Device1))
selectedDevice := 1
WiseGui("Info", "Margins: 4,4,-3,3", "MainText: Switched to Speakers", "Move: , 10", "TextWidth: 155", "Timer: 1000")
}
; Optional: Set microphone (uncomment if needed)
; SetDefaultEndpoint(GetDeviceID(List, "Microphone (Realtek(R) Audio)"))
Return
}
; ──────────────────────────────────────────────────────────────
; AUDIO ENDPOINT ENUMERATION
; ──────────────────────────────────────────────────────────────
/*
Generates a collection of audio endpoint devices that meet the specified criteria.
Parameters:
DataFlow:
The data-flow direction for the endpoint devices in the collection.
0 Audio rendering stream. Audio data flows from the application to the audio endpoint device, which renders the stream.
1 Audio capture stream. Audio data flows from the audio endpoint device that captures the stream, to the application.
2 Audio rendering or capture stream. Audio data can flow either from the application to the audio endpoint device, or from the audio endpoint device to the application.
StateMask:
The state or states of the endpoints that are to be included in the collection.
1 Active. The audio adapter that connects to the endpoint device is present and enabled. In addition, if the endpoint device plugs into a jack on the adapter, then the endpoint device is plugged in.
2 Disabled. The user has disabled the device in the Windows multimedia control panel (Mmsys.cpl).
4 Not present. The audio adapter that connects to the endpoint device has been removed or disabled.
8 Unplugged. The audio adapter that contains the jack for the endpoint device is present and enabled, but the endpoint device is not plugged into the jack. Only a device with jack-presence detection can be in this state.
Return value:
Returns an array of Map objects with the following keys:
ID Endpoint ID string that identifies the audio endpoint device.
Name The friendly name of the endpoint device.
*/
EnumAudioEndpoints(DataFlow := 2, StateMask := 1)
{
List := []
; IMMDeviceEnumerator interface.
; https://docs.microsoft.com/en-us/windows/win32/api/mmdeviceapi/nn-mmdeviceapi-immdeviceenumerator
IMMDeviceEnumerator := ComObject("{BCDE0395-E52F-467C-8E3D-C4579291692E}", "{A95664D2-9614-4F35-A746-DE8DB63617E6}")
; IMMDeviceEnumerator::EnumAudioEndpoints method.
; https://docs.microsoft.com/en-us/windows/win32/api/mmdeviceapi/nf-mmdeviceapi-immdeviceenumerator-enumaudioendpoints
ComCall(3, IMMDeviceEnumerator, "UInt", DataFlow, "UInt", StateMask, "UPtrP", &IMMDeviceCollection:=0)
; IMMDeviceCollection::GetCount method.
; https://docs.microsoft.com/en-us/windows/win32/api/mmdeviceapi/nf-mmdeviceapi-immdevicecollection-getcount
ComCall(3, IMMDeviceCollection, "UIntP", &DevCount:=0) ; Retrieves a count of the devices in the device collection.
loop DevCount
{
List.Push(Device := Map())
; IMMDeviceCollection::Item method.
; https://docs.microsoft.com/en-us/windows/win32/api/mmdeviceapi/nf-mmdeviceapi-immdevicecollection-item
ComCall(4, IMMDeviceCollection, "UInt", A_Index-1, "UPtrP", &IMMDevice:=0)
; IMMDevice::GetId method.
; https://docs.microsoft.com/en-us/windows/win32/api/mmdeviceapi/nf-mmdeviceapi-immdevice-getid
ComCall(5, IMMDevice, "PtrP", &pBuffer:=0)
Device["ID"] := StrGet(pBuffer)
DllCall("Ole32.dll\CoTaskMemFree", "UPtr", pBuffer)
; MMDevice::OpenPropertyStore method.
; https://docs.microsoft.com/en-us/windows/win32/api/mmdeviceapi/nf-mmdeviceapi-immdevice-openpropertystore
ComCall(4, IMMDevice, "UInt", 0x00000000, "UPtrP", &IPropertyStore:=0)
Device["Name"] := GetDeviceProp(IPropertyStore, "{A45C254E-DF1C-4EFD-8020-67D146A850E0}", 14)
ObjRelease(IPropertyStore)
ObjRelease(IMMDevice)
}
ObjRelease(IMMDeviceCollection)
return List
}
; ──────────────────────────────────────────────────────────────
; DEFAULT ENDPOINT MANAGEMENT
; ──────────────────────────────────────────────────────────────
/*
Set default audio render endpoint.
Role:
0x1 Default Device.
0x2 Default Communication Device.
*/
SetDefaultEndpoint(DeviceID, Role := 3)
{
; Undocumented COM-interface IPolicyConfig.
IPolicyConfig := ComObject("{870AF99C-171D-4F9E-AF0D-E63Df40c2BC9}", "{F8679F50-850A-41CF-9C72-430F290290C8}")
if (Role & 0x1)
ComCall(13, IPolicyConfig, "Str", DeviceID, "Int", 0) ; Default Device
if (Role & 0x2)
ComCall(13, IPolicyConfig, "Str", DeviceID, "Int", 2) ; Default Communication Device
}
; ──────────────────────────────────────────────────────────────
; DEVICE PROPERTY UTILITIES
; ──────────────────────────────────────────────────────────────
/*
Device Properties (Core Audio APIs)
https://docs.microsoft.com/en-us/windows/win32/coreaudio/device-properties
026E516E-B814-414B-83CD-856D6FEF4822, 2 The friendly name of the audio adapter to which the endpoint device is attached.
A45C254E-DF1C-4EFD-8020-67D146A850E0, 2 The device description of the endpoint device.
A45C254E-DF1C-4EFD-8020-67D146A850E0,14 The friendly name of the endpoint device.
*/
InitDeviceProp(clsid, n)
{
clsid := CLSIDFromString(clsid, Buffer(16+4))
NumPut("Int", n, clsid, 16)
return clsid
}
GetDeviceProp(ptr, clsid, n)
{
; IPropertyStore::GetValue method.
; https://docs.microsoft.com/en-us/windows/win32/api/propsys/nf-propsys-ipropertystore-getvalue
ComCall(5, ptr, "Ptr", InitDeviceProp(clsid, n), "Ptr", pvar := PropVariant())
return String(pvar)
}
GetDeviceID(list, name)
{
for device in list
if InStr(device["Name"], name)
return device["ID"]
throw
}
; ──────────────────────────────────────────────────────────────
; COM UTILITY FUNCTIONS
; ──────────────────────────────────────────────────────────────
CLSIDFromString(Str, Buffer := 0)
{
if (!Buffer)
Buffer := Buffer(16)
DllCall("Ole32\CLSIDFromString", "Str", Str, "Ptr", Buffer, "HRESULT")
return Buffer
}
; ──────────────────────────────────────────────────────────────
; PROPVARIANT CLASS
; ──────────────────────────────────────────────────────────────
class PropVariant
{
__New()
{
this.buffer := Buffer(A_PtrSize == 4 ? 16 : 24)
this.ptr := this.buffer.ptr
this.size := this.buffer.size
}
__Delete()
{
DllCall("Ole32\PropVariantClear", "Ptr", this.ptr, "HRESULT")
}
ToString()
{
return StrGet(NumGet(this.ptr, 8, "UPtr")) ; LPWSTR PROPVARIANT.pwszVal
}
}