-
Notifications
You must be signed in to change notification settings - Fork 0
/
audioDeviceSwapper.ahk
172 lines (136 loc) · 4.33 KB
/
audioDeviceSwapper.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
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
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance ; Only allows one instance of the script to run.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; AUDIO DEVICE SWAPPER by ImOnMars ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Created so that you can iterate through multiple sound devices on Windows with hotkeys
;
; To make this run automatically when your PC starts, put it (or a shortcut to it) in the Startup folder (For Windows 10: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp)
;
; Requirements:
; - AutoHotkey
; - nircmd
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; CONFIG OPTIONS ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Redshift Settings
NIRCMD_PATH = nircmd ; The path where nircmd is, if nircmd is in your path environment variable, then you can leave as is
; Options File
SAVE_OPTIONS := true ; Whether or not to save options to a file after script restart or PC reboot
OPTIONS_PATH = C:\audioDeviceSwapperOptions.ini ; Path of where to save options if SAVE_OPTIONS is true
; Sound Devices Options
DEVICES := ["Speakers", "Headphones"] ; A list of device names to switch to
INITIAL_DEVICE_INDEX = 1 ; The initial device when SAVE_OPTIONS is false or the options file does not exist
; Hotkeys, see here for key combinations: https://autohotkey.com/docs/Hotkeys.htm
NEXT_SOUND_DEVICE_HOTKEY = +!v ; Default: (Shift + Alt + v)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; INITIAL RUN ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Defaults
currentDeviceIndex := INITIAL_DEVICE_INDEX
; If necessary, read or write all of the options
if (SAVE_OPTIONS)
{
if (FileExist(OPTIONS_PATH))
{
readAllOptionsFromFile()
}
else
{
writeAllOptionsToFile()
}
}
; Set the initial audio device
device := DEVICES[currentDeviceIndex]
Run, %NIRCMD_PATH% setdefaultsounddevice "%device%"
soundDeviceBox(DEVICES[currentDeviceIndex])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; HOTKEY BINDINGS ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;`
Hotkey, %NEXT_SOUND_DEVICE_HOTKEY%, NextSoundDevice
return
NextSoundDevice:
currentDeviceIndex++
if (currentDeviceIndex = DEVICES.length() + 1)
{
currentDeviceIndex = 1
}
device := DEVICES[currentDeviceIndex]
Run, %NIRCMD_PATH% setdefaultsounddevice "%device%"
soundDeviceBox(DEVICES[currentDeviceIndex])
writeOptionToFile("currentDeviceIndex", currentDeviceIndex)
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; INI FILE MANAGEMENT ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
writeOptionToFile(optionName, optionValue)
{
global SAVE_OPTIONS
global OPTIONS_PATH
if (NOT SAVE_OPTIONS)
{
return
}
IniWrite, %optionValue%, %OPTIONS_PATH%, General, %optionName%
}
readOptionFromFile(optionName, ByRef optionValue)
{
global SAVE_OPTIONS
global OPTIONS_PATH
if (NOT SAVE_OPTIONS)
{
return
}
IniRead, optionValue, %OPTIONS_PATH%, General, %optionName%
}
writeAllOptionsToFile()
{
global SAVE_OPTIONS
global currentDeviceIndex
if (NOT SAVE_OPTIONS)
{
return
}
writeOptionToFile("currentDeviceIndex", currentDeviceIndex)
}
readAllOptionsFromFile()
{
global SAVE_OPTIONS
global currentDeviceIndex
if (NOT SAVE_OPTIONS)
{
return
}
readOptionFromFile("currentDeviceIndex", currentDeviceIndex)
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; GUI ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Sound device GUI
soundDeviceBox(device)
{
IfWinExist, soundDeviceWindow
{
Gui, Destroy
}
Gui, +ToolWindow -Caption +0x400000 +AlwaysOnTop
Gui, Color, FFFFFF,
Gui, Font, s12, Calibri
Gui, Add, Text, x5 y5, Sound Device:
Gui, Font, Bold
Gui, Add, Text, x100 y5, %device%
SysGet, screenx, 0
SysGet, screeny, 1
xpos:=screenx-275
ypos:=screeny-100
Gui, Show, NoActivate x%xpos% y%ypos% h30 w260, soundDeviceWindow
SetTimer, soundDeviceClose, 2250
}
soundDeviceClose:
SetTimer, soundDeviceClose, Off
Gui, Destroy
return