Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember selected input device / short tap for toggle #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions PushToTalk/HotKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
}
Expand Down
46 changes: 45 additions & 1 deletion PushToTalk/Microphone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,51 @@ class Microphone {
}
}

func getDefaultInputDevice() -> AudioDeviceID {
var propertySize = UInt32(MemoryLayout<AudioDeviceID>.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
Expand All @@ -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
}
}
Expand Down