Jump to content

Set Microphone for Playback device


argumentum
 Share

Recommended Posts

Today I decided to fix my problem of yelling on my headset's mic. to the point of ... coding a solution 

image.png.07baf8be252e5e8604d406eb5233725e.png

As I change the headset, the mic. don't change ( and I forget to do it manually ), so I searched the form for code and came up with the solution to my need.

#include <Debug.au3> ; for _DebugArrayDisplay()
#include "SetMicForPlayback.bulk.au3"

;~ Global $g_aEnumerateAudioDevices = _EnumerateAudioDevices()
;~ Global $g_aEnumerateAudioDevicesWas = $g_aEnumerateAudioDevices
;~ _DebugArrayDisplay($g_aEnumerateAudioDevicesWas, "$g_aEnumerateAudioDevicesWas")
;~  Row     |Col 0      |Col 1                                  |Col 2                                                  |Col 3                                                      |Col 4
;~  Row 0   |Playback   |{34d90035-0679-4b97-b868-ddba418b989b} |{0.0.0.00000000}.{34d90035-0679-4b97-b868-ddba418b989b}|Speakers (High Definition Audio Device)                    |1
;~  Row 1   |Playback   |{43d508ee-19c5-4513-bd52-e1b7ba57ee82} |{0.0.0.00000000}.{43d508ee-19c5-4513-bd52-e1b7ba57ee82}|Headset Earphone (2- Arctis Pro Wireless Chat)             |0
;~  Row 2   |Playback   |{a6f69bc4-f708-4a0f-bf58-995fe9d5d221} |{0.0.0.00000000}.{a6f69bc4-f708-4a0f-bf58-995fe9d5d221}|Headset Earphone (Corsair VOID Wireless Gaming Dongle)     |0
;~  Row 3   |Microphone |{a025c7e9-3655-474f-9643-8f05f1e4d591} |{0.0.1.00000000}.{a025c7e9-3655-474f-9643-8f05f1e4d591}|Headset Microphone (2- Arctis Pro Wireless Chat)           |0
;~  Row 4   |Microphone |{f9a630d2-44e9-4db5-b115-9fe7503a2ec7} |{0.0.1.00000000}.{f9a630d2-44e9-4db5-b115-9fe7503a2ec7}|Headset Microphone (Corsair VOID Wireless Gaming Dongle)   |1

;~ _SwitchToDevice($g_aEnumerateAudioDevices[4][2])

;~ + [1]"OnDefaultDeviceChanged", [2]"0", [3]"1", [4]"{0.0.0.00000000}.{43d508ee-19c5-4513-bd52-e1b7ba57ee82}"
;~ + [1]"OnDefaultDeviceChanged", [2]"0", [3]"1", [4]"{0.0.0.00000000}.{34d90035-0679-4b97-b868-ddba418b989b}"

main()
Func main()
    If WinExists(StringTrimRight(@ScriptName, 4) & "'s hidden window") Then Exit 0
    AutoItWinSetTitle(StringTrimRight(@ScriptName, 4) & "'s hidden window")
    Local $n, $a, $s, $b, $i
    While Sleep(50)
        If $g_aAudioSwitched[0] And TimerDiff($g_aAudioSwitched[0]) > 500 Then
            ConsoleWrite('+ [1]"' & $g_aAudioSwitched[1] & '", [2]"' & $g_aAudioSwitched[2] & '", [3]"' & $g_aAudioSwitched[3] & '", [4]"' & $g_aAudioSwitched[4] & '"' & @CRLF)
            Switch $g_aAudioSwitched[1]
                Case "OnDefaultDeviceChanged"
                    $s = ""
                    $a = _EnumerateAudioDevices()
                    For $n = 0 To UBound($a) - 1
                        If $a[$n][2] = $g_aAudioSwitched[4] Then
                            $b = StringSplit($a[$n][3], "()", 0)
                            If UBound($b) > 2 Then $s = $b[2]
                            ConsoleWrite($s & @CRLF)
                            ExitLoop
                        EndIf
                    Next
                    If $s = "" Then
                        ConsoleWrite(@TAB & $s & ' = not found ?, just 1 device ?, remote audio ?' & @CRLF)
                    Else
                        $i = 5
                        For $n = 0 To UBound($a) - 1
                            If $a[$n][0] = "Microphone" Then
                                If StringInStr($a[$n][3], $s) Then ; ObjCreateInterface: "Warning: This feature is experimental."
                                    $i = ShellExecuteWait(@ScriptFullPath, "/ErrorStdOut " & $a[$n][2], @ScriptDir)
                                    Switch $i
                                        Case 3
                                            ConsoleWrite(@TAB & $s & ": apply = True" & @CRLF)
                                        Case 4
                                            ConsoleWrite(@TAB & $s & ": apply = False" & @CRLF)
                                        Case 1
                                            ConsoleWrite(@TAB & $s & ": apply = Crashed" & @CRLF)
                                        Case Else
                                            ConsoleWrite(@TAB & $s & ": no clue !" & @CRLF)
                                    EndSwitch
                                    ExitLoop
                                EndIf
                            Else
                                ContinueLoop
                            EndIf
                        Next
                    EndIf
                    If $i = 5 Then ConsoleWrite(@TAB & $s & ' = ..have to code a "default mic" if none associated. ( you do it, I don''t have one. )' & @CRLF)
;~                  AdlibRegister("did_it_work") ; ...to have some feedback while in development, but the "Sound"("Recording" tab), shows the change on the fly.
                Case Else
                    ConsoleWrite("! no code written for this event" & @CRLF)
            EndSwitch
            $g_aAudioSwitched[0] = 0
            $g_aAudioSwitched[1] = ""
        EndIf
    WEnd
EndFunc   ;==>main

Func did_it_work()
    AdlibUnRegister("did_it_work")
    Local $a = _EnumerateAudioDevices()
    _DebugArrayDisplay($a, "_EnumerateAudioDevices()")
EndFunc   ;==>did_it_work

 

SetMicForPlayback.bulk.au3 The "bulk" file has the real code, ( not mine, references are in the code )

SetMicForPlayback.au3 this is the "main()" ( the code you can read above ).

Works for me on Win10 v???.???.?? ( I'm not good with updates ).

Hope it helps the next guy playing TF2 as an engineer, finding a headset close to you, to tell your team color to use the teleport that goes to the enemy base to get the intel., no one listens, you get aggravated, #%#$#@$ .... :mad2: 

:imwithstupid:
 

PS: ..the above code misbehaves a lot and is left for the adventurous.
Use the current code in the downloads section for a functional script.

Edited by argumentum
newer version of the code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 1 month later...

What's New in Version 0.2018.12.30
Found the reason for the bug and coded to minimize the effects by restarting
 when "OnDeviceStateChanged" is triggered and at midnight.
This bug, in my view, writes on the memory space used by the script.
 In any case, nasty. Use at your own peril. (I use it but "I know what I'm doing")
https://www.autoitscript.com/forum/files/file/476-setmicforplayback/ 

PS: I'm posting here too for a request: if anyone can find a fix to the bug in the code, I'll me most grateful   :console:
It shows itself after many events, or certain events, no clue, and it'd give me such errors as:

Sleep(250)^ ERROR   
Error: Variable used without being declared.

or any other random error.
The only event that is needed for my needs is "OnDefaultDeviceChanged", but when I tried to leave just the one event, it would not run  :baby:
Thanks for reading.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 3 weeks later...

What's New in Version 0.2019.1.16
Took a different approach to trigger the "Device: StateChanged". Now it watches the registry and the mishaps are minimal.
Added to open the SndVol.exe ( Volume Mixer ) on click. Right-Click shows the context menu.
( so it might as well hide the volume icon on the tray for those that prefer to use the Volume Mixer )
Since this release is good enough in my view, the source is also for download.
The file name is now "MicSwitcher". ( I like it better )
If an Icon with "MicSwitcher.type.ico" is found, it'll use that. ( there are 2 sample icons in the zip )
Added to select the mic. independently.

The files are in the downloads section.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 1 year later...

What's New in Version 0.2020.4.12
Added: Default microphone. If none fit the name, use previously selected on mic. menu.
Added: TaskSched at Log on.

What's New in Version 0.2019.2.20
Added: Sort the devices so they show and some logical order every time ( as in updates, the order of the devices may change, making the relative position you used to click elsewhere, hence, annoying )
Added: All of the right-click options in the speaker volume context menu, to have no need for the original OS' volume control at all.

What's New in Version 0.2019.1.17
Fixed an annoyance: When auto-close Vol. Mix. is selected, would have to click the icon twice to close it.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 3 years later...

What's New in Version 0.2024.1.6
Added: Move "Volume Mixer" to right-bottom corner option ( mostly for Win11 )
Added: Expand the "Volume Mixer" window by N pixels. ( set in the ini file to change or disable )
Added: "CTRL + click tray" or "middle mouse button (three-button mouse)", will load "App volume and device preferences".
Fixed: running as script now works without the need to compile first.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...