@@ -50,6 +50,26 @@ local function internalOrExternalMic()
50
50
_log (" Unable to set any mic as default" )
51
51
return 1
52
52
end
53
+ local function perDeviceWatcher (dev_uid , event_name , event_scope , event_element )
54
+ local device = hs .audiodevice .findDeviceByUID (dev_uid )
55
+ if device and event_name == " mute" then
56
+ _log (" \" " .. device :name () .. " \" mute state has changed." )
57
+ audioControl .matchInputMuteToOutputMute ()
58
+ sleep (1 )
59
+ end
60
+ end
61
+
62
+ local function startOutputWatcher ()
63
+ local defaultOutput = hs .audiodevice .defaultOutputDevice ()
64
+
65
+ if defaultOutput :watcherIsRunning () then
66
+ _log (" Audio watcher for " .. defaultOutput :name () .. " already exists." )
67
+ else
68
+ _log (" No audio watcher found for " .. defaultOutput :name () .. " starting one." )
69
+ defaultOutput :watcherCallback (perDeviceWatcher )
70
+ defaultOutput :watcherStart ()
71
+ end
72
+ end
53
73
54
74
local function audioDeviceChanged (arg )
55
75
local outputRetval = 1
@@ -66,6 +86,7 @@ local function audioDeviceChanged(arg)
66
86
sleep (5 )
67
87
68
88
_log (" New audio device detected. Current values: Speaker: " .. hs .audiodevice .defaultOutputDevice ():name () .. " Mic: " .. hs .audiodevice .defaultInputDevice ():name ())
89
+ startOutputWatcher ()
69
90
70
91
outputRetval = internalOrExternalSpeaker ()
71
92
micRetval = internalOrExternalMic ()
@@ -89,14 +110,12 @@ local function trapVolumeControls()
89
110
90
111
-- Send mute to external monitor if connected and it's the default audio output
91
112
if event [" key" ] == " MUTE" then
92
- if isMuted == false then
93
- isMuted = true
94
- run .cmd (string.format (" %s/bin/m1ddc" , Homedir ), { " set" , " mute" , " on" })
95
- _log (" Muted external monitor." )
96
- else
97
- isMuted = false
98
- run .cmd (string.format (" %s/bin/m1ddc" , Homedir ), { " set" , " mute" , " off" })
113
+ if hs .audiodevice .defaultOutputDevice ():outputMuted () then
114
+ run .cmd (" /Users/patrickking/bin/m1ddc" , { " set" , " mute" , " off" })
99
115
_log (" Unmuted external monitor." )
116
+ else
117
+ run .cmd (" /Users/patrickking/bin/m1ddc" , { " set" , " mute" , " on" })
118
+ _log (" Muted external monitor." )
100
119
end
101
120
return true
102
121
end
@@ -128,6 +147,7 @@ function audioControl.init()
128
147
local initStart = os.clock ()
129
148
hs .audiodevice .watcher .setCallback (audioDeviceChanged )
130
149
hs .audiodevice .watcher .start ()
150
+ startOutputWatcher ()
131
151
132
152
trapVolumeControls ()
133
153
hs .hotkey .bind ({' cmd' , ' shift' }, " k" , function () audioControl .mediaControls (" PLAY" ) end )
@@ -137,6 +157,14 @@ function audioControl.init()
137
157
_log (debug.getinfo (1 , " S" ).short_src :gsub (" .*/" , " " ) .. " loaded in " .. (os.clock () - initStart ) .. " seconds." )
138
158
end
139
159
160
+ function audioControl .matchInputMuteToOutputMute ()
161
+ if hs .audiodevice .defaultOutputDevice ():muted () then
162
+ audioControl .muteInputs ()
163
+ else
164
+ audioControl .unmuteInputs ()
165
+ end
166
+ end
167
+
140
168
function audioControl .muteInputs ()
141
169
for _ , device in pairs (hs .audiodevice .allInputDevices ()) do
142
170
device :setInputMuted (true )
0 commit comments