Identify default audio device in registry Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
NexusP
Posts: 27
Joined: 26 Nov 2020, 09:01

Identify default audio device in registry

Post by NexusP » 26 Nov 2022, 04:57

I use a script that directly controls my Audio Receiver volume with the mouse scroll wheel, or side scroll buttons using shortcuts that interface with Home Assistant via the Home Assistant Taskbar Menu app. The Receiver is connected to my PC via my Nvidia GPU's HDMI. With my current script, I can switch to windows volume control when I manually change the default device using a combo of regwrite and nircmd. My script is blind when a device switches to default automatically. In the registry, I can detect when my Bluetooth Headphones are connected because of consistent identifiers, but other audio devices use different values each time.

This is the registry address that contains changing level 1 and level 2 QWORD values. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render

I read that the default device has the highest number on the list and counts up with each change. Is there a way compare that information in Realtime (a loop?), and identify the the device using AHK?

Here's my code.

Code: Select all

bthpKey := "HKLM\Software\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{7e51e24c-1691-4d95-9a39-4826002a85ed}\Properties"
bthpVal := "{9c119480-ddc2-4954-a150-5bd240d454ad},1"

QuesthpKey := "HKEY_CURRENT_USER\SOFTWARE\AHKRegWrite\DefaultAudio"
QuesthpVal := "Headphones"

bthpstatus := 1

loop {
	RegRead, btHeadphones, %bthpKey%, %bthpVal%
	If (btHeadphones) {
		bthp_connect := true
		if bthpstatus {
			bthpstatus := 0 
			Run "D:\My Tools\AHK\Repo\Audio\Headphones.ahk"
			}
	} else {
		bthp_connect := false
		bthpstatus := 1
		}
		
	RegRead, QuestHeadphones, %QuesthpKey%, %QuesthpVal%	
	If (ErrorLevel) {
		qhp_connect := false
	} else {
		qhp_connect := true
		}
	}

#If WinActive("ahk_class MPC-BE") || WinActive("ahk_class {97E27FAA-C0B3-4b8e-A693-ED7881E99FC1}") 
WheelUp::
if (bthp_connect) or (qhp_connect) {
   soundset, +5
} else {
   run, "D:\My Tools\Shortcuts\Receiver Volume Up.lnk"
	 }
return

WheelDown::
if (bthp_connect) or (qhp_connect) {
	 soundset, -5
} else {
	 run, "D:\My Tools\Shortcuts\Receiver Volume Down.lnk"
	 }
return
#If

WheelRight::
if (bthp_connect) or (qhp_connect) {
   soundset, +5
} else {
   run, "D:\My Tools\Shortcuts\Receiver Volume Up.lnk"
	 }
return

WheelLeft::		
if (bthp_connect) or (qhp_connect) {
	 soundset, -5
} else {
	 run, "D:\My Tools\Shortcuts\Receiver Volume Down.lnk"
	 }
return

Code: Select all

DetectHiddenWindows, On

Run "D:\My Tools\System Tools\NirCMD\nircmdc.exe" "setdefaultsounddevice" "Oculus" , , Hide
RunWait, taskkill /f /t /im voicemeeter.exe

RegDelete, HKEY_CURRENT_USER\Software\AHKRegWrite\DefaultAudio, Receiver
RegWrite, REG_SZ, HKEY_CURRENT_USER\Software\AHKRegWrite\DefaultAudio, Headphones
I found a Powershell script that is able to identify the default audio device and display the friendly name with each run. Is there a way to grab the same information to use with with Autohotkey?
https://stackoverflow.com/questions/59393574/how-to-identify-the-default-audio-device-in-powershell
Last edited by NexusP on 26 Nov 2022, 16:50, edited 1 time in total.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Identify default audio device in registry  Topic is solved

Post by mikeyww » 26 Nov 2022, 06:58

Just an alternative with NirCmd since you use it (actually, this is a different program from the same author).

Code: Select all

s := speaker()
MsgBox, 64, Speaker, % s.1 "`n`nVolume = " s.2

speaker() {
 Static out := "d:\noBackup\sounds.csv"
 col := []
 RunWait, d:\utils\soundVolumeView\SoundVolumeView.exe /scomma "%out%"
 Loop, Read, %out%                                                          ; Process each line
 { lineNum := A_Index
   Loop, Parse, A_LoopReadLine, CSV                                         ; Process each column
    Switch {
     Case lineNum = 1                       : col.Push(A_LoopField)
     Case col[A_Index] = "Device Name"      : speaker := A_LoopField
     Case col[A_Index] = "Default"          : If (A_LoopField != "Render")
                                               Break                        ; Skip rest of current line
     Case col[A_Index] = "Channels  Percent": Return [speaker, Round(RegExReplace(A_LoopField, "%.+"))]
    }
 }
}
Easier:

Code: Select all

#Include d:\utils\vistaAudio\VA.ahk
; https://www.autohotkey.com/board/topic/21984-vista-audio-control-functions/
MsgBox, 64, Speaker, % VA_GetDeviceName(VA_GetDevice())

NexusP
Posts: 27
Joined: 26 Nov 2020, 09:01

Re: Identify default audio device in registry

Post by NexusP » 26 Nov 2022, 23:55

Got it working as desired. I initially had memory leak issues, but i found bit of code that helped with that. Memory spikes when repeating hotkeys but comes down instantly to a steady at 0.1mb and still behaves as expected. I was also able to use SoundVolumeView to target volume control for individual devices. I also switched from NirCMD to SoundVolumeView to set the default Sound Device for better consistency. I noticed that you don't see the blink of the cmd window as much.

Using RegRead\RegWrite\RegDelete is still needed when switching different VoiceMeeterDefault.xml files (acts as profiles).

Code: Select all

bthpKey := "HKLM\Software\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{7e51e24c-1691-4d95-9a39-4826002a85ed}\Properties"
bthpVal := "{9c119480-ddc2-4954-a150-5bd240d454ad},1"

vmKey := "HKEY_CURRENT_USER\SOFTWARE\AHKRegWrite\DefaultAudio"
vmVal := "vmReceiver"

bthpStatus := 1
ReceiverStatus := 1
Settimer, AudioDeviceTimer, 100

AudioDeviceTimer: 
	if RegExMatch(VA_GetDeviceName(VA_GetDevice()), "RX-V683") 
		ReceiverDefault := true
	else
		ReceiverDefault := false

	RegRead, btHeadphones, %bthpKey%, %bthpVal%
	If (btHeadphones) 
	{
		if (bthpStatus)
		{
			bthpStatus := 0 
			ReceiverStatus := 0
			Run "D:\My Tools\AHK\Repo\Audio\Headphones.ahk"
		}
	} 
	else
	{
		bthpStatus := 1
		if !(ReceiverStatus)
		{
			ReceiverStatus := 1
			Run "D:\My Tools\AHK\Repo\Audio\Receiver.ahk"
		}
	}

	RegRead, VoiceMeeter, %vmKey%, %vmVal%	
	if (ErrorLevel) 
		vmReceiverDefault := false
	else 
		vmReceiverDefault := true
		
	DllCall("SetProcessWorkingSetSize", Int,-1, Int,-1, Int,-1 )
Return

#If WinActive("ahk_class MPC-BE") or WinActive("ahk_class {97E27FAA-C0B3-4b8e-A693-ED7881E99FC1}") 
WheelUp::
if (ReceiverDefault) or (vmReceiverDefault) 
{
	sleep, 500
	run, "D:\My Tools\Shortcuts\Receiver Volume Up.lnk"
	gosub, BassShakerLvlDown
}
else 
{
  	soundset, +5
  	gosub, BassShakerLvlDown
]
  	return
	return

WheelDown::
if (ReceiverDefault) or (vmReceiverDefault) 
{
	sleep, 300
	run, "D:\My Tools\Shortcuts\Receiver Volume Down.lnk"
	gosub, BassShakerLvlUp
}
else 
{
	soundset, -5
	gosub, BassShakerLvlUp
}
	return
#If

WheelRight::
if (ReceiverDefault) or (vmReceiverDefault) 
	sleep, 500
	run, "D:\My Tools\Shortcuts\Receiver Volume Up.lnk"
	gosub, BassShakerLvlDown
}
else 
{
  	soundset, +5
  	gosub, BassShakerLvlDown
]
	return

WheelLeft::
if (ReceiverDefault) or (vmReceiverDefault) 
{
	sleep, 300
	run, "D:\My Tools\Shortcuts\Receiver Volume Down.lnk"
	gosub, BassShakerLvlUp
}
else 
{
	soundset, -5
	gosub, BassShakerLvlUp
}
	return
  
BassShakerLvlUp: 
run, D:\My Tools\System Tools\soundvolumeview\SoundVolumeView.exe /ChangeVolume "Chair Bass - Full | Btm" 1
return

BassShakerLvlDown:
run, D:\My Tools\System Tools\soundvolumeview\SoundVolumeView.exe /ChangeVolume "Chair Bass - Full | Btm" -1
return

Code: Select all

#NoTrayIcon

DetectHiddenWindows, On
run, D:\My Tools\System Tools\soundvolumeview\SoundVolumeView.exe /SetDefault "RX-V683" 0 /SetDefault "RX-V683" 2
RunWait, taskkill /f /t /im voicemeeter.exe
RegDelete, HKEY_CURRENT_USER\Software\AHKRegWrite\DefaultAudio, vmReceiver

Code: Select all

#NoTrayIcon

DetectHiddenWindows, On

run, D:\My Tools\System Tools\soundvolumeview\SoundVolumeView.exe /SetDefault "VoiceMeeter" 0 /SetDefault "VoiceMeeter" 2
run "D:\My Tools\Shortcuts\Voicemeeter\Receiver\Receiver.bat"
RunWait, taskkill /f /t /im voicemeeter.exe
sleep, 1000
run "D:\My Tools\System Tools\Trayconizer.exe" "C:\Program Files (x86)\VB\Voicemeeter\voicemeeter.exe"
sleep, 1000
WinMinimize, VoiceMeeter
RegWrite, REG_SZ, HKEY_CURRENT_USER\Software\AHKRegWrite\DefaultAudio, vmReceiver
Batch:

Code: Select all

@echo off
xcopy /s "D:\My Tools\Shortcuts\Voicemeeter\Receiver\VoiceMeeterDefault.xml" "C:\Users\HTPC\AppData\Roaming\VoiceMeeterDefault.xml" /y
exit

Post Reply

Return to “Ask for Help (v1)”