Toggle Microphone Mute

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
AfterLemon
Posts: 85
Joined: 30 Sep 2013, 14:27
Location: Ohio, USA

Re: Toggle Microphone Mute

Post by AfterLemon » 02 Feb 2021, 23:51

You can replace the Tooltip line with this:

Code: Select all

ToolTip,% "Mic " (master_mute="On"?"Off":"On") ;use a tool tip at mouse pointer to show what state mic is after toggle
HOME: Windows 11 Pro | AMD Ryzen 7 5800X 8-Core @ 4.50GHZ | 64GB RAM
MOBILE: Samsung Galaxy Note 10+

kuz8
Posts: 1
Joined: 11 Mar 2021, 14:08

Re: Toggle Microphone Mute

Post by kuz8 » 11 Mar 2021, 18:18

On a desktop with 4 DP monitors, desktop motherboard soundcard and external USB mic + occasionally USB webcam (which has a microphone too) I have 9 to 11 "mixer" devices and every time I reboot or plug in the webcam the actual main mic changes the number and AHK stops working.

At the same time I have razer mouse with programmable keys - and it's able to mute the microphone all the time.

I wonder if there's a method to mute for AHK which doesn't require finding the mixer number?

As a potential solution I could note all possible numbers it ever takes after reboot and mute them all although the tooltip part would be unknown.

Thank you,

hanhzo
Posts: 1
Joined: 16 Mar 2021, 21:03

Re: Toggle Microphone Mute

Post by hanhzo » 16 Mar 2021, 22:41

Thanks OP, this was just what I was looking for! I combined your script along with this keyboard LED control script so that the caps lock indicator LED turns on when the mic is muted (can be done with scroll lock or numlock as well). Here's the script in case anyone is interested:

Code: Select all

Capslock:: 


SoundSet, +1, MASTER, mute, 5 ;5 is my mic ID number, must use a separate script to figure out your mic ID. then replace the 5 in this line and the line below it with your mic ID
SoundGet, master_mute, , mute, 5


if (master_mute = "On") {
Loop, 12 {
    KeyboardLED(4, "On", A_Index-1)
}

} else {
Loop, 12 {
    KeyboardLED(4, "Off", A_Index-1)
}

}


KeyboardLED(LEDvalue, Cmd, Kbd=0)
{
  SetUnicodeStr(fn,"\Device\KeyBoardClass" Kbd)
  h_device:=NtCreateFile(fn,0+0x00000100+0x00000080+0x00100000,1,1,0x00000040+0x00000020,0)

  If Cmd= switch  ;switches every LED according to LEDvalue
   KeyLED:= LEDvalue
  If Cmd= on  ;forces all choosen LED's to ON (LEDvalue= 0 ->LED's according to keystate)
   KeyLED:= LEDvalue | (GetKeyState("ScrollLock", "T") + 2*GetKeyState("NumLock", "T") + 4*GetKeyState("CapsLock", "T"))
  If Cmd= off  ;forces all choosen LED's to OFF (LEDvalue= 0 ->LED's according to keystate)
    {
    LEDvalue:= LEDvalue ^ 7
    KeyLED:= LEDvalue & (GetKeyState("ScrollLock", "T") + 2*GetKeyState("NumLock", "T") + 4*GetKeyState("CapsLock", "T"))
    }

  success := DllCall( "DeviceIoControl"
              ,  "ptr", h_device
              , "uint", CTL_CODE( 0x0000000b     ; FILE_DEVICE_KEYBOARD
                        , 2
                        , 0             ; METHOD_BUFFERED
                        , 0  )          ; FILE_ANY_ACCESS
              , "int*", KeyLED << 16
              , "uint", 4
              ,  "ptr", 0
              , "uint", 0
              ,  "ptr*", output_actual
              ,  "ptr", 0 )

  NtCloseFile(h_device)
  return success
}

CTL_CODE( p_device_type, p_function, p_method, p_access )
{
  Return, ( p_device_type << 16 ) | ( p_access << 14 ) | ( p_function << 2 ) | p_method
}


NtCreateFile(ByRef wfilename,desiredaccess,sharemode,createdist,flags,fattribs)
{
  VarSetCapacity(objattrib,6*A_PtrSize,0)
  VarSetCapacity(io,2*A_PtrSize,0)
  VarSetCapacity(pus,2*A_PtrSize)
  DllCall("ntdll\RtlInitUnicodeString","ptr",&pus,"ptr",&wfilename)
  NumPut(6*A_PtrSize,objattrib,0)
  NumPut(&pus,objattrib,2*A_PtrSize)
  status:=DllCall("ntdll\ZwCreateFile","ptr*",fh,"UInt",desiredaccess,"ptr",&objattrib
                  ,"ptr",&io,"ptr",0,"UInt",fattribs,"UInt",sharemode,"UInt",createdist
                  ,"UInt",flags,"ptr",0,"UInt",0, "UInt")
  return % fh
}

NtCloseFile(handle)
{
  return DllCall("ntdll\ZwClose","ptr",handle)
}


SetUnicodeStr(ByRef out, str_)
{
  VarSetCapacity(out,2*StrPut(str_,"utf-16"))
  StrPut(str_,&out,"utf-16")
}
The loop is included because sometimes on reboot the keyboard index number changes so this ensures it works even if that happens. I converted the script to an .exe and added the shortcut as a startup program so that it runs on boot. It seems to run well but I have limited experience with AHK so let me know if there are any improvements to be made. Hopefully this is useful for someone!

eye_in_the_sky
Posts: 4
Joined: 08 Jul 2020, 13:50

Re: Toggle Microphone Mute

Post by eye_in_the_sky » 06 Jul 2021, 11:21

OsquoWassen wrote:
17 Aug 2020, 06:08
Hi, i have the exact same problem as the last guy, (Script is muting my speakers instead of mic) difference is i cant figure it out. Could someone help please?
If you have more than one microphone to mute in the script, one of the IDs you have in there could be the ID for the Speaker (not the microphone).
Worth a check with the Mic Test script by setting the Speaker to a suitable level say 50 and see which ID is tagged to that. Then make sure your script IS NOT using that ID. If not, both your microphone and speakerphone would be muted.

Another tip would be to enable ALL your speakerphones and microphones device combo and run the Mic Test script to catch all IDs. That way you can prevent using the Speaker ID incorrectly for your Microphone! Thus preventing muting both your microphones and speakerphones together.

jaredarm
Posts: 1
Joined: 28 Mar 2022, 02:21

Re: Toggle Microphone Mute

Post by jaredarm » 28 Mar 2022, 02:31

Amazing! I created an account just to say thank you OP! This has opened my eyes to the power of AHK.

I've had the script running for a while but I was having some trouble where my mics wouldn't stay on the same ID number. I'm working from home a few days a week at the moment and the mixer values kept changing, probably from the different docking arrangements I have. I was getting tired of finding the mic IDs and editing the script, so I modified the script so that it runs the ID check when it's launched. It looks for any mics that mics with a volume of 80% (on a variable so that it can be changed easily). All I then need to do is change the volume of all the mics I want the script to mute to 80%. The script then mutes all the mics it found. I find it useful to mute all mics because I often change which mic I'm using for calls (headphones, laptop mic, webcam mic). I have it set to use the Pause key which I then have mapped to a spare mouse button.

My code below in case it helps anyone else.

Any suggestions for improvement would be appreciated. This is my first proper AHK script so it's probably a bit rough around the edges.

Code: Select all

MicList := GetMics(80)

; Unmute all mics so all mics are in same state
For k, v in MicList
{
	SoundSet, 0, MASTER, mute, v
}

Pause::
	For k, v in MicList
	{		
		SoundSet, +1, MASTER, mute, v
				
		SoundGet, master_mute, , mute, v
	}
	
	; change the message for the tool tip
	if (master_mute = "On")
		msg := "Mic is OFF"
	if (master_mute = "Off")
		msg := "Mic is ON"
	
	ToolTip, %msg% ; use a tool tip at mouse pointer to show what state mic is after toggle
		SetTimer, RemoveToolTip, 500
		return

	RemoveToolTip:
		SetTimer, RemoveToolTip, Off
		ToolTip
		return


GetMics(vol)
{
	SetBatchLines -1
	SplashTextOn,,, % "Finding Mics Set to " . vol . "%..."
	
	
	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

	Mics := []

	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 Setting != %vol% ; Only allow values that have been set to vol
						continue             
					Mics.Push(CurrMixer)
				}  ; For each control type.
			}  ; For each component instance.
		}  ; For each component type.
	}  ; For each mixer.
	
	SplashTextOff
	
	SetBatchLines 10
	
	return Mics
}

Lisa19
Posts: 48
Joined: 07 Apr 2019, 22:25

Re: Toggle Microphone Mute

Post by Lisa19 » 30 Mar 2022, 02:56

⚠ In my case the chanel number change when Bluetooth is enabled or not, and/or, when the internal mic is enabled or not.

To find the right chanel :
; 1) start: run AUDIOfINDER.ahk
; 2) to find the chanel number just enable then disable the mic (task bar sound icon/sound/recording), and see which number appear the disappear. (Restart AUDIOfINDER to refresh).

golfman
Posts: 1
Joined: 04 Jul 2022, 12:15

Re: Toggle Microphone Mute

Post by golfman » 04 Jul 2022, 12:20

I am having the same problem as the above 2 users that my mixer will change.

Is there a workaround to use an Alias instead of mixer #?
Also, can you 'reserve' a mixer in Windows?

Great script and thread!

Novocaine
Posts: 1
Joined: 12 Jul 2022, 04:19

Re: Toggle Microphone Mute

Post by Novocaine » 12 Jul 2022, 04:24

jaredarm wrote:
28 Mar 2022, 02:31
Amazing! I created an account just to say thank you OP! This has opened my eyes to the power of AHK.

I've had the script running for a while but I was having some trouble where my mics wouldn't stay on the same ID number. I'm working from home a few days a week at the moment and the mixer values kept changing, probably from the different docking arrangements I have. I was getting tired of finding the mic IDs and editing the script, so I modified the script so that it runs the ID check when it's launched. It looks for any mics that mics with a volume of 80% (on a variable so that it can be changed easily). All I then need to do is change the volume of all the mics I want the script to mute to 80%. The script then mutes all the mics it found. I find it useful to mute all mics because I often change which mic I'm using for calls (headphones, laptop mic, webcam mic). I have it set to use the Pause key which I then have mapped to a spare mouse button.

My code below in case it helps anyone else.

.....
Great stuff, thanks for this tweak. I too was having issues with the id changing using docks etc...
The only formatting tweak I made to the script was to make the message use a shorthand if like so. Functionally it works great so far. I went with 99% volume so that my output is not really affected much.

Code: Select all

ToolTip, % "Microphone " (master_mute = "On" ? "OFF" : "ON")

surfernv
Posts: 1
Joined: 30 Sep 2022, 11:43

Re: Toggle Microphone Mute

Post by surfernv » 30 Sep 2022, 11:44

Created an account here just so I could say thank you. This is awesome and so useful for me. My mic was lucky #12 as well!

defaultusername
Posts: 2
Joined: 08 Dec 2022, 06:30

Re: Toggle Microphone Mute

Post by defaultusername » 08 Dec 2022, 06:35

All rights reserved to "Firenyth" for the code, original page where I found the code here:
vie...


in the YouTube video description says he used this code and gives Firenyth credits for the code

defaultusername
Posts: 2
Joined: 08 Dec 2022, 06:30

Re: Toggle Microphone Mute

Post by defaultusername » 08 Dec 2022, 06:37

thanks op this works great !! is there a way i can change the text in the tooltip; as "Mute Off" = "Mic On" and "Mute On" = "Mic Off" ?

GioEarthling2
Posts: 2
Joined: 14 Mar 2023, 06:15

Re: Toggle Microphone Mute

Post by GioEarthling2 » 14 Mar 2023, 06:18

Code: Select all

if (master_mute = "Off") {
        SoundBeep, 1500, 300
        Menu, Tray, Icon, C:\Windows\System32\imageres.dll, 233
    }

    if  (master_mute = "On") {
        SoundBeep, 500, 300
        Menu, Tray, Icon, C:\Windows\System32\imageres.dll, 230
    }
added this beep from willzen and a change in tray icon to show when muted or not
thanks :)

GioEarthling2
Posts: 2
Joined: 14 Mar 2023, 06:15

Re: Toggle Microphone Mute

Post by GioEarthling2 » 14 Mar 2023, 06:45

Code: Select all

#Requires AutoHotkey v2.0-beta
#SingleInstance force
TraySetIcon "C:\Windows\System32\imageres.dll", 250
A_IconTip := "Microphone Status"

Pause:: {
if (SoundGetMute( , "Microphone") = 1) {
   SoundSetMute 0, , "Microphone"
    ToolTip "Microphone Mute Off"
    SetTimer () => ToolTip(), -1000
    TraySetIcon "C:\Windows\System32\imageres.dll", 233
    SoundBeep 1500, 300
}
else {
    SoundSetMute 1, , "Microphone"
    ToolTip "Microphone Mute On"
    SetTimer () => ToolTip(), -1000
    TraySetIcon "C:\Windows\System32\imageres.dll", 230
    SoundBeep 500, 300
}
}
converted it to v2
addded beep from willzen and tray icon status

thanks <3

mattofamillion
Posts: 2
Joined: 23 Dec 2023, 11:47

Re: Toggle Microphone Mute

Post by mattofamillion » 02 Jan 2024, 20:52

GioEarthling2 wrote:
14 Mar 2023, 06:45

Code: Select all

#Requires AutoHotkey v2.0-beta
#SingleInstance force
TraySetIcon "C:\Windows\System32\imageres.dll", 250
A_IconTip := "Microphone Status"

Pause:: {
if (SoundGetMute( , "Microphone") = 1) {
   SoundSetMute 0, , "Microphone"
    ToolTip "Microphone Mute Off"
    SetTimer () => ToolTip(), -1000
    TraySetIcon "C:\Windows\System32\imageres.dll", 233
    SoundBeep 1500, 300
}
else {
    SoundSetMute 1, , "Microphone"
    ToolTip "Microphone Mute On"
    SetTimer () => ToolTip(), -1000
    TraySetIcon "C:\Windows\System32\imageres.dll", 230
    SoundBeep 500, 300
}
}
converted it to v2
addded beep from willzen and tray icon status

thanks <3
This is working well on my work and personal laptops but isn't working on my home desktop. The mics on the laptops are integrated, but I have a Logi USB camera/mic combo on the desktop. Is that potentially affecting things? All devices are on Windows 10, 64 bit, same version of AHK (2.0.11.0), etc. I haven't tried the option of using a device ID from page 1 since this worked so well on the other devices.

mattofamillion
Posts: 2
Joined: 23 Dec 2023, 11:47

Re: Toggle Microphone Mute

Post by mattofamillion » 03 Jan 2024, 08:37

mattofamillion wrote:
02 Jan 2024, 20:52
GioEarthling2 wrote:
14 Mar 2023, 06:45

Code: Select all

#Requires AutoHotkey v2.0-beta
#SingleInstance force
TraySetIcon "C:\Windows\System32\imageres.dll", 250
A_IconTip := "Microphone Status"

Pause:: {
if (SoundGetMute( , "Microphone") = 1) {
   SoundSetMute 0, , "Microphone"
    ToolTip "Microphone Mute Off"
    SetTimer () => ToolTip(), -1000
    TraySetIcon "C:\Windows\System32\imageres.dll", 233
    SoundBeep 1500, 300
}
else {
    SoundSetMute 1, , "Microphone"
    ToolTip "Microphone Mute On"
    SetTimer () => ToolTip(), -1000
    TraySetIcon "C:\Windows\System32\imageres.dll", 230
    SoundBeep 500, 300
}
}
converted it to v2
addded beep from willzen and tray icon status

thanks <3
This is working well on my work and personal laptops but isn't working on my home desktop. The mics on the laptops are integrated, but I have a Logi USB camera/mic combo on the desktop. Is that potentially affecting things? All devices are on Windows 10, 64 bit, same version of AHK (2.0.11.0), etc. I haven't tried the option of using a device ID from page 1 since this worked so well on the other devices.
UPDATE: even though the mics on all devices were just labelled "microphone" renaming the one on my desktop and updating "Microphone" to the new name got it to start working on the desktop as well. Thanks again for posting this GioEarthling2!

Post Reply

Return to “Scripts and Functions (v1)”