Skip to content

Instantly share code, notes, and snippets.

@peteromano
Created August 27, 2019 19:03
Show Gist options
  • Save peteromano/183f71d4527bceddab9ce68c6c048ea4 to your computer and use it in GitHub Desktop.
Save peteromano/183f71d4527bceddab9ce68c6c048ea4 to your computer and use it in GitHub Desktop.
This script toggles muting the default audio input device (microphone), resets the input volume to 100% (when unmuted), and presents a tooltip balloon indicating mute status. Scripted for Windows 10, and primarily used with the Elgato Stream Deck (set as System script.)
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
// f(), g(), ... are unused COM method slots. Define these if you care
int f(); int g(); int h(); int i();
int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
int j();
int GetMasterVolumeLevelScalar(out float pfLevel);
int k(); int l(); int m(); int n();
int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
int GetMute(out bool pbMute);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
int f(); // Unused
int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
public class Audio {
static IAudioEndpointVolume Vol() {
var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
IMMDevice dev = null;
Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
IAudioEndpointVolume epv = null;
var epvid = typeof(IAudioEndpointVolume).GUID;
Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
return epv;
}
public static float Volume {
get {float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v;}
set {Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty));}
}
public static bool Mute {
get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
}
}
'@
Function Invoke-BalloonTip {
<#
.Synopsis
Display a balloon tip message in the system tray.
.Description
This function displays a user-defined message as a balloon popup in the system tray. This function
requires Windows Vista or later.
.Parameter Message
The message text you want to display. Recommended to keep it short and simple.
.Parameter Title
The title for the message balloon.
.Parameter MessageType
The type of message. This value determines what type of icon to display. Valid values are
.Parameter SysTrayIcon
The path to a file that you will use as the system tray icon. Default is the PowerShell ISE icon.
.Parameter Duration
The number of seconds to display the balloon popup. The default is 1000.
.Inputs
None
.Outputs
None
.Notes
NAME: Invoke-BalloonTip
VERSION: 1.0
AUTHOR: Boe Prox
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True,HelpMessage="The message text to display. Keep it short and simple.")]
[string]$Message,
[Parameter(HelpMessage="The message title")]
[string]$Title="Attention $env:username",
[Parameter(HelpMessage="The message type: Info,Error,Warning,None")]
[System.Windows.Forms.ToolTipIcon]$MessageType="Info",
[Parameter(HelpMessage="The path to a file to use its icon in the system tray")]
[string]$SysTrayIconPath='C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe',
[Parameter(HelpMessage="The number of milliseconds to display the message.")]
[int]$Duration=1000
)
Add-Type -AssemblyName System.Windows.Forms
If (-NOT $global:balloon) {
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
#Mouse double click on icon to dispose
[void](Register-ObjectEvent -InputObject $balloon -EventName MouseDoubleClick -SourceIdentifier IconClicked -Action {
#Perform cleanup actions on balloon tip
Write-Verbose 'Disposing of balloon'
$global:balloon.dispose()
Unregister-Event -SourceIdentifier IconClicked
Remove-Job -Name IconClicked
Remove-Variable -Name balloon -Scope Global
})
}
#Need an icon for the tray
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path
#Extract the icon from the file
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($SysTrayIconPath)
#Can only use certain TipIcons: [System.Windows.Forms.ToolTipIcon] | Get-Member -Static -Type Property
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]$MessageType
$balloon.BalloonTipText = $Message
$balloon.BalloonTipTitle = $Title
$balloon.Visible = $true
#Display the tip and specify in milliseconds on how long balloon will stay visible
$balloon.ShowBalloonTip($Duration)
Sleep $Duration
$balloon.Visible = $false
Write-Verbose "Ending function"
}
Add-Type -AssemblyName System.Windows.Forms
Set-AudioDevice -RecordingMuteToggle
Set-AudioDevice -RecordingVolume 100
Invoke-BalloonTip -Message "$(If (Get-AudioDevice -RecordingMute) {"Off"} Else {"On"})" -Title 'Microphone' -MessageType Info -Duration 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment