I have a setup where I have two audio devices, one is my headphones and the other is my speakers. I use headphones for gaming and speakers for everything else. I have a SteelSeries GG Sonar program that allows me to switch between audio devices. The problem is that I have to click on multiple categories (game, media, chat, aux) to switch between output devices. I wanted to automate this process so I don’t have to click on the category every time I want to switch between output devices.
First, I have started looking if anything like this already exists and I found this GitHub repo by adirh3 who gave me an idea how to do this. I saw that he used wireshark to find a link on which he can send a request to change the audio device. I have done the same thing and found out that I can send a request to http://localhost:57953
. From there I have found endpoint to list all audio devices and endpoint to change the audio device. With a little bit more investigation and restart of a system I have found out that the port is not always the same, and noticed that ports always binds to processId 4 which is owned by system. So I have created this command to get the port number:
$port = (Get-NetTCPConnection | Where-Object { $_.State -eq "LISTEN" } | select @{Name="SteelSeriesSonar";Expression={(Get-Process -Id $_.OwningProcess).ProcessName}}, localaddress, localport | Where-Object { $_.SteelSeriesSonar -eq "SteelSeriesSonar" }).localport
$port
To combine it all together I have created a Powershell script that will change all audio devices based on the input parameter. You can find the script here
Then I have combine the powershell script with autohotkey script that will run the powershell script when I press a key combination CTRL + F1 and ask for input. You can find the autohotkey script here
You can change the input parameter to be whatever you want, I have used regex to match the input with my native language.
Endpoints I am using Link to heading
List all audio devices
Invoke-WebRequest -Uri http://localhost:$port/audioDevices
Change audio devices
Invoke-WebRequest -Uri http://localhost:$port/classicRedirections/{ game,media,chat,aux }/deviceId/{0.0.0.00000000}.{deviceId}