diff --git a/PushToTalk/HotKey.swift b/PushToTalk/HotKey.swift index c92f97d..7c30f48 100644 --- a/PushToTalk/HotKey.swift +++ b/PushToTalk/HotKey.swift @@ -71,13 +71,6 @@ class HotKey { microphone.status = MicrophoneStatus.HotKeySet } - func checkForDoubleTap() { - let timeInterval = NSDate().timeIntervalSince1970 - let timediff = timeInterval - self.previousEpoc - self.previousEpoc = timeInterval - self.doubletap = timediff < 0.2 - } - internal func handleFlagChangedEvent(_ theEvent: NSEvent!) { if self.recordingHotKey { self.recordingHotKey = false @@ -94,12 +87,18 @@ class HotKey { guard self.enabled else { return } if theEvent.modifierFlags.contains(self.modifierFlags) { - checkForDoubleTap() + if microphone.status != .Speaking { + let timeInterval = NSDate().timeIntervalSince1970 + self.previousEpoc = timeInterval + } + microphone.status = .Speaking } else { - if (!self.doubletap) { + let timeInterval = NSDate().timeIntervalSince1970 + let timediff = timeInterval - self.previousEpoc + // Only mute on keyup when we pressed long enough so short tap is toggling the state + if timediff > 0.2 { microphone.status = .Muted - doubletap = false } } } diff --git a/PushToTalk/Microphone.swift b/PushToTalk/Microphone.swift index a4e108c..a846370 100644 --- a/PushToTalk/Microphone.swift +++ b/PushToTalk/Microphone.swift @@ -33,10 +33,51 @@ class Microphone { } } + func getDefaultInputDevice() -> AudioDeviceID { + var propertySize = UInt32(MemoryLayout.size) + var deviceID = kAudioDeviceUnknown + + var propertyAddress = AudioObjectPropertyAddress( + mSelector: AudioObjectPropertySelector(kAudioHardwarePropertyDefaultInputDevice), + mScope: AudioObjectPropertyScope(kAudioObjectPropertyScopeGlobal), + mElement: AudioObjectPropertyElement(kAudioObjectPropertyElementMaster)) + + AudioObjectGetPropertyData(AudioObjectID(kAudioObjectSystemObject), &propertyAddress, 0, nil, &propertySize, &deviceID) + + return deviceID + } + func setupDeviceMenu(menu: NSMenu) throws { menu.removeAllItems() let devices = try? getInputDevices() - self.selectedInput = devices![0] + + let selectedInputId = getDefaultInputDevice() + + Swift.print("Looking for device ID '\(selectedInputId)'") + for device in devices! { + if device.id == selectedInputId { + self.selectedInput = device + Swift.print("Selecting device with ID '\(selectedInputId)'") + } + } + + /* + if UserDefaults.standard.integer(forKey: "prefSelectedInputId") != nil { + let selectedInputId = UInt32(UserDefaults.standard.integer(forKey: "prefSelectedInputId")) + + Swift.print("Looking for device ID '\(selectedInputId)'") + for device in devices! { + if device.id == selectedInputId { + self.selectedInput = device + Swift.print("Selecting device with ID '\(selectedInputId)'") + } + } + } + if self.selectedInput == nil { + self.selectedInput = devices![0] + } + */ + for device in devices! { let item = DeviceMenuItem() item.inputDevice = device @@ -59,6 +100,9 @@ class Microphone { } item.state = NSControl.StateValue.on self.selectedInput = item.inputDevice; + let inputId = self.selectedInput!; + Swift.print("Selecting '\(inputId.id)'") + UserDefaults.standard.set(inputId.id, forKey: "prefSelectedInputId") self.status = status } }