Struggling with VA AHK - creating a script to swap between two default audio devices when the .ahk or .exe is run

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WarMom
Posts: 1
Joined: 25 Jul 2021, 12:31

Struggling with VA AHK - creating a script to swap between two default audio devices when the .ahk or .exe is run

Post by WarMom » 25 Jul 2021, 13:15

This is a script I've been struggling to wrap my head around and though there's got to be at least two ways to achieve what I want, I haven't been successful with either. I am not using AHK 2.0 beta, I am using the current stable release with VA 2.3 in my Lib folder.

Basically, I want to create a script that changes Windows (10) default playback device when I run the .ahk or compiled .exe. If it's my headphones (device 1 in soundcard analysis), it switches to my external speakers (device 6) and vice versa. I don't want it to trigger on a hotkey press - the exact 'problem' I have is that my current solution requires a hotkey press and I'd rather save as many physical keyboard and mouse inputs as I can for standardisation, instead I'd trigger the .ahk or .exe directly through a macro pad. I want the script to run it.

So far I've managed to grok this much:

Code: Select all

VA_SetDefaultEndpoint("playback:" (Toggle ? 1 : 6), 0)
This will flip my device from 1 (headphones) to 6 (speakers)

Code: Select all

VA_SetDefaultEndpoint("playback:" (Toggle ? 6 : 1), 0)
This does the opposite. So if I put these in a script with 'sleep 2000' between them it'd switch to speakers, wait 2 seconds, then back again. However, that's the relatively easy part; I have two .lnk files generated by SoundVolumeView for switching to a given device. It's getting it to automatically go between the two commands, or check what the current Default Playback Device is, that's causing me trouble.

I originally tried to bash something together to get the current default playback device name, store that as a variable and then use that variable to create an If statement ['if the current default device is headphones, then do this to switch it to speakers and vice versa'], but no luck. Best I could do was pilfer this to retrieve the device name:

Code: Select all

msgbox % va_getdevicename(va_getdevice("playback:1"))
But I couldn't really make anything of it. If I tried

Code: Select all

default := VA_GetDevice("playback:1")
MsgBox, %default%
to get a variable for the current default playback device, the msgbox would spit out a variety of different numbers, nothing consistent.

I did find this script to have a key toggle between two devices:

Code: Select all

*ScrollLock::
Toggle := !Toggle
VA_SetDefaultEndpoint("playback:" (Toggle ? 1:6), 1)
VA_SetDefaultEndpoint("playback:" (Toggle ? 1:6), 2)
KeyWait ScrollLock
return
But removing the scroll lock parts stops the script from doing anything at all.

I must be overlooking something obvious on both paths; could I get a hand?
jemmm
Posts: 1
Joined: 26 Mar 2019, 05:31

Re: Struggling with VA AHK - creating a script to swap between two default audio devices when the .ahk or .exe is run

Post by jemmm » 24 Aug 2021, 15:17

I can't offer help but I can offer a solution. I struggled with this for ages, and then found this open-source app. https://github.com/Belphemur/SoundSwitch

It lets you map hotkeys and apps to multiple audio outputs and inputs. It's fantastic.
Post Reply

Return to “Ask for Help (v1)”