From 166f6981de05d809fbd3d19bbb4e0ba21f1f9b66 Mon Sep 17 00:00:00 2001 From: Patrick King Date: Mon, 1 Jul 2024 10:02:56 -0700 Subject: [PATCH] More Muting I wanted to sync muting audio input devices when I mute audio output devices. --- tilde/.hammerspoon/init.lua | 2 ++ tilde/.hammerspoon/system/audioControl.lua | 40 ++++++++++++++++++---- tilde/.hammerspoon/system/keytrap.lua | 16 +++++++++ 3 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 tilde/.hammerspoon/system/keytrap.lua diff --git a/tilde/.hammerspoon/init.lua b/tilde/.hammerspoon/init.lua index 9f22c2cd..9269c865 100644 --- a/tilde/.hammerspoon/init.lua +++ b/tilde/.hammerspoon/init.lua @@ -110,6 +110,7 @@ Dock = require("system/dock") networking = require("system/networking") -- Ping = require("system/Ping") -- needs work videoCalls = require("system/videoCalls") +-- Keytrap = require("system/keytrap") audioControl.init() @@ -120,6 +121,7 @@ Dock.init() networking.init() -- Ping.init() -- needs work videoCalls.init() +-- Keytrap.init() -- ========================================================================= }}} -- App configuration ======================================================= {{{ diff --git a/tilde/.hammerspoon/system/audioControl.lua b/tilde/.hammerspoon/system/audioControl.lua index b3dcc3ea..5476140e 100644 --- a/tilde/.hammerspoon/system/audioControl.lua +++ b/tilde/.hammerspoon/system/audioControl.lua @@ -50,6 +50,26 @@ local function internalOrExternalMic() _log("Unable to set any mic as default") return 1 end +local function perDeviceWatcher(dev_uid, event_name, event_scope, event_element) + local device = hs.audiodevice.findDeviceByUID(dev_uid) + if device and event_name == "mute" then + _log("\"" .. device:name() .. "\" mute state has changed.") + audioControl.matchInputMuteToOutputMute() + sleep(1) + end +end + +local function startOutputWatcher() + local defaultOutput = hs.audiodevice.defaultOutputDevice() + + if defaultOutput:watcherIsRunning() then + _log("Audio watcher for " .. defaultOutput:name() .. " already exists.") + else + _log("No audio watcher found for " .. defaultOutput:name() .. " starting one.") + defaultOutput:watcherCallback(perDeviceWatcher) + defaultOutput:watcherStart() + end +end local function audioDeviceChanged(arg) local outputRetval = 1 @@ -66,6 +86,7 @@ local function audioDeviceChanged(arg) sleep(5) _log("New audio device detected. Current values: Speaker: " .. hs.audiodevice.defaultOutputDevice():name() .. " Mic: " .. hs.audiodevice.defaultInputDevice():name()) + startOutputWatcher() outputRetval = internalOrExternalSpeaker() micRetval = internalOrExternalMic() @@ -89,14 +110,12 @@ local function trapVolumeControls() -- Send mute to external monitor if connected and it's the default audio output if event["key"] == "MUTE" then - if isMuted == false then - isMuted = true - run.cmd("/Users/patrickking/bin/m1ddc", { "set", "mute", "on" }) - _log("Muted external monitor.") - else - isMuted = false + if hs.audiodevice.defaultOutputDevice():outputMuted() then run.cmd("/Users/patrickking/bin/m1ddc", { "set", "mute", "off" }) _log("Unmuted external monitor.") + else + run.cmd("/Users/patrickking/bin/m1ddc", { "set", "mute", "on" }) + _log("Muted external monitor.") end return true end @@ -128,6 +147,7 @@ function audioControl.init() local initStart = os.clock() hs.audiodevice.watcher.setCallback(audioDeviceChanged) hs.audiodevice.watcher.start() + startOutputWatcher() trapVolumeControls() hs.hotkey.bind({'cmd', 'shift'}, "k", function() audioControl.mediaControls("PLAY") end) @@ -137,6 +157,14 @@ function audioControl.init() _log(debug.getinfo(1, "S").short_src:gsub(".*/", "") .. " loaded in " .. (os.clock() - initStart) .. " seconds.") end +function audioControl.matchInputMuteToOutputMute() + if hs.audiodevice.defaultOutputDevice():muted() then + audioControl.muteInputs() + else + audioControl.unmuteInputs() + end +end + function audioControl.muteInputs() for _, device in pairs(hs.audiodevice.allInputDevices()) do device:setInputMuted(true) diff --git a/tilde/.hammerspoon/system/keytrap.lua b/tilde/.hammerspoon/system/keytrap.lua new file mode 100644 index 00000000..067bcf11 --- /dev/null +++ b/tilde/.hammerspoon/system/keytrap.lua @@ -0,0 +1,16 @@ +local keytrap = {} + +local function keytrap_router() + systemeventtap = hs.eventtap.new({ hs.eventtap.event.types.systemDefined }, function(mainEvent) + local event = mainEvent:systemKey() + local flags = hs.eventtap.checkKeyboardModifiers() + + end) + systemeventtap:start() +end + +function keytrap.init() + keytrap_router() +end + +return keytrap