Jump to content

Set Microphone for Playback device

Recommended Posts


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


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} |{}.{34d90035-0679-4b97-b868-ddba418b989b}|Speakers (High Definition Audio Device)                    |1
;~  Row 1   |Playback   |{43d508ee-19c5-4513-bd52-e1b7ba57ee82} |{}.{43d508ee-19c5-4513-bd52-e1b7ba57ee82}|Headset Earphone (2- Arctis Pro Wireless Chat)             |0
;~  Row 2   |Playback   |{a6f69bc4-f708-4a0f-bf58-995fe9d5d221} |{}.{a6f69bc4-f708-4a0f-bf58-995fe9d5d221}|Headset Earphone (Corsair VOID Wireless Gaming Dongle)     |0
;~  Row 3   |Microphone |{a025c7e9-3655-474f-9643-8f05f1e4d591} |{}.{a025c7e9-3655-474f-9643-8f05f1e4d591}|Headset Microphone (2- Arctis Pro Wireless Chat)           |0
;~  Row 4   |Microphone |{f9a630d2-44e9-4db5-b115-9fe7503a2ec7} |{}.{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]"{}.{43d508ee-19c5-4513-bd52-e1b7ba57ee82}"
;~ + [1]"OnDefaultDeviceChanged", [2]"0", [3]"1", [4]"{}.{34d90035-0679-4b97-b868-ddba418b989b}"

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)
                    If $s = "" Then
                        ConsoleWrite(@TAB & $s & ' = not found ?, just 1 device ?, remote audio ?' & @CRLF)
                        $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)
                    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)
            $g_aAudioSwitched[0] = 0
            $g_aAudioSwitched[1] = ""
EndFunc   ;==>main

Func 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: 


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

Edited by argumentum
newer version of the code
  • Like 2

Share this post

Link to post
Share on other sites

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")

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.

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
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

  • Similar Content

    • Subz
      By Subz
      Hello all.
      in the process of packaging an application (Voip) and looking at trying to automate capturing the following information to add to a config.xml file:
      Default: Playback Device Name and Guid
      Name: Speakers (Realtek High Definition Audio)
      Guid: {}.{a46ce930-4dd3-49b5-8e8e-7c8a2fdc3925}
      Default Microphone Device Name and Guid
      Name: Microphone (Realtek High Definition Audio)
      Guid: {}.{cff6f838-33ce-4c2d-9f77-98f4e19e4a75}
      I've seen the following post by @trancexx, which gives me the Default Audit Device Name, but unsure how to get the Guid or the Microphone Name and Guid.  I believe to get the Guid, I would need to use PKEY_AudioEndpoint_GUID but unsure how to code this.  If anyone can assist or point me in the right direction it would be much appreciated.
      Cheers Subz
    • nullschritt
      By nullschritt
      Hey forum!
      I'm getting close to releasing a beta version of my encrypted p2p messenger I'm coding.
      One feature I'de really like to implement is audio chat.
      I've searched through the forum, and can't seem to find any examples of capturing audio from the microphone, without writing it to a file.
      Capturing it to a file makes the audio very choppy, as bits and pieces get cut out between audio files.
      I believe at one point I saw a udf that allows capturing audio directly as a stream(that I could then send as packets across the network)
      Could anyone point me in the right direction, as to how to go about capturing audio as a stream of data that I could then break into packets and send?
      Edit: There used to be an example in the BASS UDF, but I can't seem to locate it, or figure out exactly how to go about streaming audio with the included documentation.
    • richietheprogrammer
      By richietheprogrammer
      Hey all, I was wondering if there is a way to "automate" the microphone in an output way. Meaning, is there a way to write a function that will force a track to be played and sent to the microphone? Basically, instead of having to say a specific thing on the microphone over and over again, is there a way to automate the microphone to send a recorded track? Not sure if what Im asking makes sense, but any help would be greatly appreciated!
    • slammy
      By slammy
      I was wondering if there is a way to detect if a microphone or any hardware for that matter is plugged into the system using a autoit command.