-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmic.lua
215 lines (183 loc) · 6.7 KB
/
mic.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
-- Tastaturkürzel "m" für Mic
moduleString = moduleString .. "🎙️ - Mic\n"
function micToggle()
local micDevice = hs.audiodevice.defaultInputDevice()
if (micDevice:inputMuted()) then
-- unmute
micDevice:setInputMuted(false)
hs.alert.show("️🎙️ - Mic: Active! ⚡")
else
-- mute
micDevice:setInputMuted(true)
hs.alert.show("🔇 - Mic: Muted 🤫")
end
end
hs.hotkey.bind(hyperNoShift, "m", micToggle)
hs.hotkey.bind(hyper, "m", micToggle)
--
-- Simple Hammerspoon script to create Push-To-Talk functionality
-- Press and hold fn key to talk
--
local log = hs.logger.new('PushToTalk', 'debug')
local settings = {pushToTalk = true}
local modifierKeys = {}
local inputVolumes = {}
local muted = false
-- local menubarIcon = nil
-- local icons = {microphone = nil, mutedMicrophone = nil}
function updateInputVolumes()
local activeUids = {}
for index, device in ipairs(hs.audiodevice.allInputDevices()) do
activeUids[device:uid()] = true
if inputVolumes[device:uid()] == nil then
local inputVolume = device:inputVolume()
if inputVolume == 0 then inputVolume = 100 end
inputVolumes[device:uid()] = inputVolume
-- log.i("Setting unmuted volume for " .. device:uid() .. ": " ..
-- inputVolumes[device:uid()])
end
if not device:watcherIsRunning() then
device:watcherCallback(onInputDeviceChanged)
device:watcherStart()
end
end
for uid, volume in pairs(inputVolumes) do
if activeUids[uid] == nil then
inputVolumes[uid] = nil
log.i("Removed unmuted volume for no longer active device " .. uid)
end
end
end
function onInputDeviceChanged(uid, name, scope, element)
if name ~= "vmvc" then return end
if scope ~= "inpt" then return end
local device = hs.audiodevice.findDeviceByUID(uid)
local newVolume = device:inputVolume()
if newVolume == 0 or newVolume == inputVolumes[uid] then return end
inputVolumes[uid] = newVolume
log.i("User changed unmuted volume for " .. uid .. ": " .. newVolume)
end
function onSystemAudioDeviceChanged(name)
if name ~= "dev#" then return end
updateInputVolumes()
changeMicrophoneState(muted)
end
function installSystemAudioWatcher()
hs.audiodevice.watcher.setCallback(onSystemAudioDeviceChanged)
hs.audiodevice.watcher.start()
end
function changeMicrophoneState(mute)
if mute then
log.i('Muting audio')
for index, device in ipairs(hs.audiodevice.allInputDevices()) do
device:setInputVolume(0)
end
-- Hack to really mute the microphone
hs.applescript('set volume input volume 0')
-- menubarIcon:setIcon(icons.mutedMicrophone)
else
for index, device in ipairs(hs.audiodevice.allInputDevices()) do
if inputVolumes[device:uid()] == nil then
log.i("Device with unknown inputVolume")
else
log.i('Unmuting audio: ' .. inputVolumes[device:uid()])
device:setInputVolume(inputVolumes[device:uid()])
end
end
-- Hack to really unmute the microphone
local defaultInputDevice = hs.audiodevice.defaultInputDevice()
local defaultVolumne = inputVolumes[defaultInputDevice:uid()]
hs.applescript('set volume input volume ' .. defaultVolumne)
-- menubarIcon:setIcon(icons.microphone)
end
end
local keyPressed = false
local modifiersChangedTap = hs.eventtap.new(
{hs.eventtap.event.types.flagsChanged},
function(event)
local modifiers = event:getFlags()
local stateChanged = false
local modifiersMatch = true
for index, key in ipairs(modifierKeys) do
if modifiers[key] ~= true then modifiersMatch = false end
end
if modifiersMatch then
if keyPressed ~= true then stateChanged = true end
keyPressed = true
else
if keyPressed ~= false then stateChanged = true end
keyPressed = false
end
if stateChanged then
if keyPressed then
muted = not settings.pushToTalk
changeMicrophoneState(muted)
else
muted = settings.pushToTalk
changeMicrophoneState(muted)
end
end
end)
-- function initMenubarIcon()
-- menubarIcon = hs.menubar.new()
-- menubarIcon:setIcon(icons.microphone)
-- menubarIcon:setMenu(function()
-- return {
-- {
-- title = "Push to talk",
-- checked = settings.pushToTalk,
-- fn = function()
-- if settings.pushToTalk == false then
-- muted = true
-- changeMicrophoneState(true)
-- settings.pushToTalk = true
-- end
-- end
-- }, {
-- title = "Push to mute",
-- checked = not settings.pushToTalk,
-- fn = function()
-- if settings.pushToTalk == true then
-- muted = false
-- changeMicrophoneState(false)
-- settings.pushToTalk = false
-- end
-- end
-- }, {title = "-"},
-- {title = "Hotkey: " .. table.concat(modifierKeys, " + ")}
-- }
-- end)
-- end
-- function loadIcons()
-- local iconPath = hs.configdir .. "/pushToTalk/icons"
-- icons.microphone =
-- hs.image.imageFromPath(iconPath .. "/microphone.pdf"):setSize(
-- {w = 16, h = 16})
-- icons.mutedMicrophone = hs.image.imageFromPath(
-- iconPath .. "/microphone-slash.pdf"):setSize(
-- {w = 16, h = 16})
-- end
function loadSettings()
local loadedSettings = hs.settings.get('pushToTalk.settings')
if loadedSettings ~= nil then settings = loadedSettings end
end
function saveSettings() hs.settings.set('pushToTalk.settings', settings) end
-- Public interface
local pushToTalk = {}
pushToTalk.init = function(modifiers)
modifierKeys = modifiers or {"fn"}
loadSettings()
-- loadIcons()
-- initMenubarIcon()
updateInputVolumes()
installSystemAudioWatcher()
changeMicrophoneState(settings.pushToTalk)
modifiersChangedTap:start()
local oldShutdownCallback = hs.shutdownCallback
hs.shutdownCallback = function()
if oldShutdownCallback ~= nil then oldShutdownCallback() end
saveSettings()
changeMicrophoneState(false)
end
end
return pushToTalk