-
Notifications
You must be signed in to change notification settings - Fork 0
/
audioswitcher.pb
266 lines (231 loc) · 9.12 KB
/
audioswitcher.pb
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
; AudioSwitcher - Switch audio from speakers & webcam to headset
; 2020 by Alex
; Variables
Global NewMap Config$()
; Procedures
Declare ReadPreference(Map PrefMap$())
Declare SoundDevice(Command$, Device$, Param$ = "", Wait = #False)
Declare WritePreference(Key$, Device$)
Declare.s findDevice(device.s, type.s)
; Config
ReadPreference(Config$())
Restore profileTemplate
Read.s profileTemplate$
; GUI
XIncludeFile "bevelbutton.pb"
XIncludeFile "audioswitcher-window.pbf"
; Modules
XIncludeFile "Registry.pbi"
UseModule Registry
; Fonts
gadgetFont = LoadFont(#PB_Any, Config$("font"), Val(Config$("fontSize")), #PB_Font_HighQuality)
font = FontID(gadgetFont)
gadgetFont = LoadFont(#PB_Any, Config$("font"), Val(Config$("fontSizeHeadline")), #PB_Font_HighQuality)
fontHeadline = FontID(gadgetFont)
; Window
OpenWindowMain()
If Config$("windowOnTop") = "true"
StickyWindow(WindowMain, #True)
EndIf
; Set text
SetGadgetFont(HeadlineDevices, fontHeadline)
SetGadgetText(HeadlineDevices, Config$("devicesHeadline"))
BevelButton::SetText(ButtonDevices1, Config$("devices1Text"), font)
BevelButton::SetText(ButtonDevices2, Config$("devices2Text"), font)
SetGadgetFont(HeadlineVolumes, fontHeadline)
SetGadgetText(HeadlineVolumes, Config$("volumesHeadline"))
BevelButton::SetText(ButtonVolumes1, Config$(Config$("lastDevice") + "volumes1Text"), font)
BevelButton::SetText(ButtonVolumes2, Config$(Config$("lastDevice") + "volumes2Text"), font)
; Set current state
Select Config$("lastDevice")
Case "devices1"
BevelButton::Enable(ButtonDevices1)
BevelButton::Disable(ButtonDevices2)
Case "devices2"
BevelButton::Enable(ButtonDevices2)
BevelButton::Disable(ButtonDevices1)
EndSelect
Select Config$("lastVolume")
Case "volumes1"
BevelButton::Enable(ButtonVolumes1)
BevelButton::Disable(ButtonVolumes2)
Case "volumes2"
BevelButton::Enable(ButtonVolumes2)
BevelButton::Disable(ButtonVolumes1)
EndSelect
; ----------------------------------------------------------------------------------------------------------------------------------
; Loop
HideWindow(WindowMain, #False)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
; Toogle Buttons
LockWindowUpdate_(WindowID(WindowMain))
Select EventGadget()
Case BevelButton::GetGadgetID(ButtonDevices1)
Config$("lastDevice") = "devices1"
BevelButton::Enable(ButtonDevices1)
BevelButton::Disable(ButtonDevices2)
; Set Volume-Buttons
BevelButton::SetText(ButtonVolumes1, Config$(Config$("lastDevice") + "volumes1Text"))
BevelButton::SetText(ButtonVolumes2, Config$(Config$("lastDevice") + "volumes2Text"))
Case BevelButton::GetGadgetID(ButtonDevices2)
Config$("lastDevice") = "devices2"
BevelButton::Enable(ButtonDevices2)
BevelButton::Disable(ButtonDevices1)
; Set Volume-Buttons
BevelButton::SetText(ButtonVolumes1, Config$(Config$("lastDevice") + "volumes1Text"))
BevelButton::SetText(ButtonVolumes2, Config$(Config$("lastDevice") + "volumes2Text"))
Case BevelButton::GetGadgetID(ButtonVolumes1)
Config$("lastVolume") = "volumes1"
BevelButton::Enable(ButtonVolumes1)
BevelButton::Disable(ButtonVolumes2)
Case BevelButton::GetGadgetID(ButtonVolumes2)
Config$("lastVolume") = "volumes2"
BevelButton::Enable(ButtonVolumes2)
BevelButton::Disable(ButtonVolumes1)
EndSelect
; Dark button
If BevelButton::GetButton(EventGadget())
BevelButton::ColorDark(BevelButton::GetButton(EventGadget()))
EndIf
LockWindowUpdate_(0)
UpdateWindow_(WindowID(WindowMain))
; Set Device/Volume
Select EventGadget()
Case BevelButton::GetGadgetID(ButtonDevices1), BevelButton::GetGadgetID(ButtonDevices2), BevelButton::GetGadgetID(ButtonVolumes1), BevelButton::GetGadgetID(ButtonVolumes2)
template$ = profileTemplate$
template$ = ReplaceString(template$, "###deviceRender###", Config$(Config$("lastDevice") + "SpeakerID"))
template$ = ReplaceString(template$, "###deviceCapture###", Config$(Config$("lastDevice") + "MicID"))
template$ = ReplaceString(template$, "###volume###", FormatNumber(ValF(Config$(Config$("lastDevice") + Config$("lastVolume") + "Value"))/100, 2))
; Write Profile
If OpenFile(0, "profile.spr")
WriteString(0, template$)
CloseFile(0)
Else
MessageRequester("Error", "Unable to write profile-file!")
Continue
EndIf
; Set Profile
SoundDevice("LoadProfile", "profile.spr", "", #True)
; Finished setting
BevelButton::ColorNormal(BevelButton::GetButton(EventGadget()))
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
HideWindow(WindowMain, #True)
; Write last state
WritePreference("lastDevice", Config$("lastDevice"))
WritePreference("lastVolume", Config$("lastVolume"))
; Free Memory
BevelButton::Delete(ButtonDevices2)
BevelButton::Delete(ButtonDevices1)
BevelButton::Delete(ButtonVolumes2)
BevelButton::Delete(ButtonVolumes1)
End
; ----------------------------------------------------------------------------------------------------------------------------------
Procedure SoundDevice(Command$, Device$, Param$ = "", Wait = #False)
allParams$ = "/" + Command$ + " " + Chr(34) + Device$ + Chr(34) + " " + Param$
; Flags
Flags = 0
If Wait = #True
Flags = #PB_Program_Wait
EndIf
; Run SoundVolumeView
RunProgram("SoundVolumeView/SoundVolumeView", allParams$, "", Flags)
EndProcedure
Procedure ReadPreference(Map PrefMap$())
; Open Preferences
FileIni.s = GetFilePart(ProgramFilename(),#PB_FileSystem_NoExtension) + ".ini"
If (OpenPreferences(FileIni) = 0)
MessageRequester("Error", "Can not open ini-file: "+FileIni)
End
EndIf
; Read
ExaminePreferenceGroups()
While NextPreferenceGroup()
ExaminePreferenceKeys()
While NextPreferenceKey()
; Debug PreferenceGroupName() + ": " + PreferenceKeyName() + "=" + PreferenceKeyValue()
PrefMap$(PreferenceKeyName())=PreferenceKeyValue()
Wend
Wend
; Get device IDs
For i=1 To 2
key.s = "devices" + Str(i) + "Speaker"
PrefMap$(key+"ID") = findDevice(PrefMap$(key), "Render")
; Debug key+"ID" + "=" + PrefMap$(key+"ID")
key.s = "devices" + Str(i) + "Mic"
PrefMap$(key+"ID") = findDevice(PrefMap$(key), "Capture")
; Debug key+"ID" + "=" + PrefMap$(key+"ID")
Next
; Close
ClosePreferences()
EndProcedure
Procedure WritePreference(Key$, Device$)
; Open Preferences
FileIni.s = GetFilePart(ProgramFilename(),#PB_FileSystem_NoExtension) + ".ini"
If (OpenPreferences(FileIni) = 0)
MessageRequester("Error", "Can not open ini-file: "+FileIni)
End
EndIf
; Write
PreferenceGroup("base")
WritePreferenceString(Key$, Device$)
; Close
ClosePreferences()
EndProcedure
DataSection
profileTemplate:
Data.s "" +
; Render Device
"[ProfileItem0]" + #CRLF$ +
"ID=###deviceRender###" + #CRLF$ +
"DefaultRender=1" + #CRLF$ +
"DefaultRenderCommunications=1" + #CRLF$ +
"DefaultRenderMultimedia=1" + #CRLF$ +
"VolumeScalar=###volume###" + #CRLF$ +
; Capture Device
"[ProfileItem1]" + #CRLF$ +
"ID=###deviceCapture###" + #CRLF$ +
"DefaultCapture=1" + #CRLF$ +
"DefaultCaptureCommunications=1" + #CRLF$ +
"DefaultCaptureMultimedia=1" + #CRLF$ +
; General
"[General]" + #CRLF$ +
"ItemsCount=2" + #CRLF$
EndDataSection
Procedure.s findDevice(device.s, type.s)
Protected devicenameKey.s = "{b3f8fa53-0004-438e-9003-51a46e139bfc},6"
Protected nameKey.s = "{a45c254e-df1c-4efd-8020-67d146a850e0},2"
Protected basekey.s = "SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\" + type
Protected NewMap devices.s()
Protected deviceID.s, devicePath.s
Protected i
; All devices
; Example: Logitech Speaker\Device\Speaker\Render
For i = 0 To CountSubKeys(#HKEY_LOCAL_MACHINE, basekey, #True ) - 1
deviceID = ListSubKey(#HKEY_LOCAL_MACHINE, basekey, i, #True )
devicePath = basekey + "\" + deviceID + "\Properties"
devices(ReadValue(#HKEY_LOCAL_MACHINE, devicePath, devicenameKey, #True) + "\Device\" + ReadValue(#HKEY_LOCAL_MACHINE, devicePath, nameKey, #True) + "\" + type) = deviceID
Next
; Return ID
If FindMapElement(devices(), device)
If type = "Render"
ProcedureReturn "{0.0.0.00000000}." + devices(device)
Else
ProcedureReturn "{0.0.1.00000000}." + devices(device)
EndIf
Else
MessageRequester("Error", "Unable to find device '" + device + "' with type '" + type + "'!")
EndIf
EndProcedure
; IDE Options = PureBasic 5.73 LTS (Windows - x86)
; CursorPosition = 33
; FirstLine = 8
; Folding = -
; EnableXP
; UseIcon = icon.ico
; Executable = audioswitcher.exe
; DisableDebugger