Skip to content

Instantly share code, notes, and snippets.

@Leokuma
Last active April 4, 2023 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leokuma/c773fdbe89ac02e2bc4185c9f8d7a4ab to your computer and use it in GitHub Desktop.
Save Leokuma/c773fdbe89ac02e2bc4185c9f8d7a4ab to your computer and use it in GitHub Desktop.
[AHK] Mute/Unmute mic (Win11)
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force ; Skips the dialog box and replaces the old instance automatically
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir, %A_ScriptDir% ; Set default directory to where this script file is located. (Note %% because it's expecting and unquoted string)
UpdateIcon()
NumPadClear::
SoundSet, +1, MASTER, mute, 9 ; Replace with your channel
SoundGet, master_mute, , mute, 9 ; Replace with your channel
if (master_mute = "on")
SoundPlay *16
else
SoundPlay *-1
UpdateIcon()
return
UpdateIcon() {
RegRead, light_theme, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize, SystemUsesLightTheme
SoundGet, master_mute, , mute, 9 ; Replace with your channel
if (master_mute = "on") {
if (light_theme)
Menu, Tray, Icon, mic-off-black.png
else
Menu, Tray, Icon, mic-off-white.png
} else {
if (light_theme)
Menu, Tray, Icon, mic-on-black.png
else
Menu, Tray, Icon, mic-on-white.png
}
}
SetBatchLines -1
SplashTextOn,,, Gathering Soundcard Info...
; Most of the pure numbers below probably don't exist in any mixer, but they're queried for completeness.
; The numbers correspond to the following items (in order): CUSTOM, BOOLEANMETER, SIGNEDMETER, PEAKMETER,
; UNSIGNEDMETER, BOOLEAN, BUTTON, DECIBELS, SIGNED, UNSIGNED, PERCENT, SLIDER, FADER, SINGLESELECT, MUX,
; MULTIPLESELECT, MIXER, MICROTIME, MILLITIME
ControlTypes := "VOLUME,ONOFF,MUTE,MONO,LOUDNESS,STEREOENH,BASSBOOST,PAN,QSOUNDPAN,BASS,TREBLE,EQUALIZER,0x00000000, 0x10010000,0x10020000,0x10020001,0x10030000,0x20010000,0x21010000,0x30040000,0x30020000,0x30030000,0x30050000,0x40020000,0x50030000,0x70010000,0x70010001,0x71010000,0x71010001,0x60030000,0x61030000"
ComponentTypes := "MASTER,HEADPHONES,DIGITAL,LINE,MICROPHONE,SYNTH,CD,TELEPHONE,PCSPEAKER,WAVE,AUX,ANALOG,N/A"
; Create a ListView and prepare for the main loop:
Gui, Add, ListView, w400 h400 vMyListView, Component Type|Control Type|Setting|Mixer
LV_ModifyCol(4, "Integer")
SetFormat, Float, 0.2 ; Limit number of decimal places in percentages to two.
Loop ; For each mixer number that exists in the system, query its capabilities.
{
CurrMixer := A_Index
SoundGet, Setting,,, %CurrMixer%
if (ErrorLevel = "Can't Open Specified Mixer") ; Any error other than this indicates that the mixer exists.
break
; For each component type that exists in this mixer, query its instances and control types:
Loop, parse, ComponentTypes, `,
{
CurrComponent := A_LoopField
; First check if this component type even exists in the mixer:
SoundGet, Setting, %CurrComponent%,, %CurrMixer%
if (ErrorLevel = "Mixer Doesn't Support This Component Type")
continue ; Start a new iteration to move on to the next component type.
Loop ; For each instance of this component type, query its control types.
{
CurrInstance := A_Index
; First check if this instance of this instance even exists in the mixer:
SoundGet, Setting, %CurrComponent%:%CurrInstance%,, %CurrMixer%
; Checking for both of the following errors allows this script to run on older versions:
if ErrorLevel in Mixer Doesn't Have That Many of That Component Type,Invalid Control Type or Component Type
break ; No more instances of this component type.
; Get the current setting of each control type that exists in this instance of this component:
Loop, parse, ControlTypes, `,
{
CurrControl := A_LoopField
SoundGet, Setting, %CurrComponent%:%CurrInstance%, %CurrControl%, %CurrMixer%
; Checking for both of the following errors allows this script to run on older versions:
if ErrorLevel in Component Doesn't Support This Control Type,Invalid Control Type or Component Type
continue
if ErrorLevel ; Some other error, which is unexpected so show it in the results.
Setting := ErrorLevel
ComponentString := CurrComponent
if (CurrInstance > 1)
ComponentString := ComponentString ":" CurrInstance
LV_Add("", ComponentString, CurrControl, Setting, CurrMixer)
} ; For each control type.
} ; For each component instance.
} ; For each component type.
} ; For each mixer.
Loop % LV_GetCount("Col") ; Auto-size each column to fit its contents.
LV_ModifyCol(A_Index, "AutoHdr")
SplashTextOff
Gui, Show
return
GuiClose:
ExitApp
@Leokuma
Copy link
Author

Leokuma commented Oct 19, 2022

This script allows the key 5 from the numeric keyboard to be used as a shortcut to mute/unmute your mic on Windows. NumLock must be turned off. A sound is played on toggling, and a mic icon is displayed on the tray to indicate whether the mic is turned on or off.

  1. Run SoundSet-Script.ahk (it's from the official AHK documentation) to see what your mic channel is. ℹ️Tip: set your mic level to a specific number so that you can easily spot it in the list;
  2. In mic.ahk, replace all the occurrences of the number 9 with your channel. That number may change from time to time, especially after connecting and disconnecting devices or after a Windows update;
  3. Save the mic images below in the same folder as the mic.ahk script;
  4. Run mic.ahk.

If you want to autorun mic.ahk via Windows Task Scheduler, apply a delay of a few seconds, otherwise you may see a glitch on the tray icon.

mic-off-black
mic-off-white
mic-on-black
mic-on-white

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment