윈도우 10은 다양한 오디오 장치에 대해 개별적인 오디오 설정을 저장하는 기능을 제공합니다. 각 오디오 장치를 연결할 때마다 다른 볼륨 수준을 지정할 수 있으며, 장치가 연결되면 볼륨이 자동으로 조절됩니다. 일반적으로 사용자는 오디오 장치를 음소거 상태로 두지 않습니다. 볼륨을 조절하는 경우는 많지만, 의도적으로 음소거하는 경우는 드뭅니다.
만약 데스크톱 환경에서 헤드폰을 자주 사용하고, 연결을 자주 해제하는 사용자라면, 헤드폰을 뽑을 때 자동으로 소리가 음소거되는 간단한 PowerShell 스크립트를 활용할 수 있습니다.
이는 스마트폰이 헤드폰을 분리할 때 음악 재생을 자동으로 중단하는 것과 유사한 원리입니다. 사용자가 음악 감상을 마치거나 실수로 헤드폰을 뽑았을 때, 소리를 빠르게 끌 필요가 있을 때 유용합니다. 이 스크립트는 GEEKEEFY의 Prateek Singh의 아이디어를 기반으로 제작되었습니다.
자동 음소거 설정 방법
먼저, 메모장을 열고 아래 코드를 붙여넣으세요.
[cmdletbinding()] Param() #Adding definitions for accessing the Audio API 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)); } } } '@ -Verbose While($true) { #Clean all events in the current session since its in a infinite loop, to make a fresh start when loop begins Get-Event | Remove-Event -ErrorAction SilentlyContinue #Registering the Event and Waiting for event to be triggered Register-WmiEvent -Class Win32_DeviceChangeEvent Wait-Event -OutVariable Event |Out-Null $EventType = $Event.sourceargs.newevent | Sort-Object TIME_CREATED -Descending | Select-Object EventType -ExpandProperty EventType -First 1 #Conditional logic to handle, When to Mute/unMute the machine using Audio API If($EventType -eq 3) { [Audio]::Mute = $true Write-Verbose "Muted [$((Get-Date).tostring())]" } elseif($EventType -eq 2 -and [Audio]::Mute -eq $true) { [Audio]::Mute = $false Write-Verbose "UnMuted [$((Get-Date).tostring())]" } }
해당 코드를 .ps1 확장자를 가진 파일로 저장합니다. 파일 형식 드롭다운 메뉴에서 ‘모든 파일’을 선택해야 합니다. 파일 이름은 스크립트의 목적을 명확하게 보여주는 것으로 정하고, 필요할 때 쉽게 찾을 수 있는 위치에 저장하는 것이 좋습니다.
스크립트 실행 방법
PowerShell은 보안상의 이유로 스크립트의 자동 실행을 기본적으로 허용하지 않습니다. 하지만 이러한 제한을 우회하는 방법이 있습니다. 자세한 내용은 다른 관련 자료에서 확인할 수 있습니다. 해당 자료를 참고하여 만든 PowerShell 스크립트가 PC 부팅 시 자동으로 실행되도록 예약 작업을 설정할 수 있습니다.
또는, 시스템을 부팅할 때마다 스크립트를 수동으로 실행할 수도 있습니다. 일단 사용해 보면, 이전에는 어떻게 생활했는지 기억하기 어려울 정도로 편리할 것입니다.