Skip to content

Instantly share code, notes, and snippets.

@tarach
Last active January 16, 2025 18:05
Show Gist options
  • Save tarach/7c7fec4712cec6fbce5847047de53e55 to your computer and use it in GitHub Desktop.
Save tarach/7c7fec4712cec6fbce5847047de53e55 to your computer and use it in GitHub Desktop.
When starting SteamVR on Quest 3 the default playback and recording switches to oculus. This is a workaround for that issue
; Script Requires a powershell extension
; https://www.powershellgallery.com/packages/AudioDeviceCmdlets/3.1.0.2
; https://github.com/frgnca/AudioDeviceCmdlets
; Configuration
global logMessages2File := true
; Declare global variables for the original Playback and Recording device indices
global originalPlayback := ""
global originalRecording := ""
; Initialize the script by getting the original Playback and Recording devices
GetOriginalDevices()
; Set a timer to check the devices every second (1000 milliseconds)
SetTimer(CheckAudioDevices, 1000)
; Function to get and store the original Playback and Recording device indices
GetOriginalDevices() {
global originalPlayback, originalRecording
; Get the original Playback device index
originalPlayback := GetAudioDeviceIndex("Playback")
; Get the original Recording device index
originalRecording := GetAudioDeviceIndex("Recording")
WriteLog("originalRecording: " . originalRecording.Name . "(" . originalRecording.Id . "), " . "originalPlayback: " . originalPlayback.Name . "(" . originalPlayback.Id . ")")
}
; Function to periodically check the current Playback and Recording devices
CheckAudioDevices() {
global originalPlayback, originalRecording
; Get the current Playback and Recording device indices
currentPlayback := GetAudioDeviceIndex("Playback")
currentRecording := GetAudioDeviceIndex("Recording")
; If the current Playback device has changed, restore the original
if (currentPlayback.Id != originalPlayback.Id) {
WriteLog("Current playback device has changed to: " . currentPlayback.Id . ", restoring to: " . originalPlayback.Id)
RestoreAudioDevice(originalPlayback.Id)
}
; If the current Recording device has changed, restore the original
if (currentRecording.Id != originalRecording.Id) {
WriteLog("Current recording device has changed to: " . currentRecording.Id . ", restoring to: " . originalRecording.Id)
RestoreAudioDevice(originalRecording.Id)
}
}
; Function to get the index of the default audio device (Playback or Recording)
GetAudioDeviceIndex(deviceType) {
; Use PowerShell to get the device information
RunWait('powershell -command "Get-AudioDevice -' deviceType ' | Where-Object {$_.Default -eq $true} | Out-String" > currentDevice.txt', , "Hide")
; Read the output from the file
output := FileRead("currentDevice.txt")
FileDelete("currentDevice.txt")
id := ""
name := ""
; Extract the index by searching for the line starting with 'Index'
for line in StrSplit(output, "`r`n") {
if RegExMatch(line, "^Name") {
name := Trim(RegExReplace(line, "^Name\s+: ", ""))
}
if RegExMatch(line, "^ID") {
id := Trim(RegExReplace(line, "^ID\s+: ", ""))
}
if name != "" && id != "" {
return { Name: name, Id: id}
}
}
return ""
}
; Function to restore the original device using its index
RestoreAudioDevice(deviceId) {
; Use PowerShell to set the audio device by ID
Run('powershell -command "Set-AudioDevice -ID \"' deviceId '\""', , "Hide")
}
WriteLog(message) {
global logMessages2File
if true != logMessages2File {
return ""
}
timeString := FormatTime(, "yyyy-MM-dd HH:mm:ss")
fp := FileOpen("audio.log", "a", "UTF-8")
fp.Write("[" . timeString . "] " . message . "`r`n")
fp.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment