وویس تایپینگ سانی اوکس مک
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

472 lines
16 KiB

import Foundation
import Cocoa
public enum RecognitionLanguage: String, CaseIterable {
case multi = "multi"
case all = "all"
case persian = "fa"
case english = "en"
case arabic = "ar"
case persianEnglish = "fa_en"
case persianArabic = "fa_ar"
case persianEnglishArabic = "fa_en_ar"
public var displayName: String {
switch self {
case .multi:
return "Multilingual (Persian, English, Arabic)"
case .all:
return "🌍 All Languages (Automatic Full Detection)"
case .persian:
return "Persian Only (فارسی)"
case .english:
return "English Only"
case .arabic:
return "Arabic Only (عربی)"
case .persianEnglish:
return "Persian + English"
case .persianArabic:
return "Persian + Arabic"
case .persianEnglishArabic:
return "Persian + English + Arabic"
}
}
public var hints: [String] {
switch self {
case .multi, .persianEnglishArabic:
return ["fa", "en", "ar"]
case .all:
return [] // When empty, Soniox auto-identifies all 60+ global languages!
case .persian:
return ["fa"]
case .english:
return ["en"]
case .arabic:
return ["ar"]
case .persianEnglish:
return ["fa", "en"]
case .persianArabic:
return ["fa", "ar"]
}
}
}
public enum TranslationTargetLanguage: String, CaseIterable {
case off = "off"
case english = "en"
case arabic = "ar"
case german = "de"
case french = "fr"
case spanish = "es"
case turkish = "tr"
case russian = "ru"
case chinese = "zh"
case japanese = "ja"
case italian = "it"
case persian = "fa"
public var displayName: String {
switch self {
case .off:
return "Off (Original Spoken Language)"
case .english:
return "🇺🇸 English (انگلیسی)"
case .arabic:
return "🇸🇦 Arabic (عربی)"
case .german:
return "🇩🇪 German (آلمانی)"
case .french:
return "🇫🇷 French (فرانسوی)"
case .spanish:
return "🇪🇸 Spanish (اسپانیایی)"
case .turkish:
return "🇹🇷 Turkish (ترکی استانبولی)"
case .russian:
return "🇷🇺 Russian (روسی)"
case .chinese:
return "🇨🇳 Chinese (چینی)"
case .japanese:
return "🇯🇵 Japanese (ژاپنی)"
case .italian:
return "🇮🇹 Italian (ایتالیایی)"
case .persian:
return "🇮🇷 Persian (فارسی)"
}
}
public var isEnabled: Bool {
return self != .off
}
}
public enum NumberFormatOption: String, CaseIterable {
case digits = "digits"
case persianDigits = "persianDigits"
case words = "words"
case raw = "raw"
public var displayName: String {
switch self {
case .digits:
return "English Digits (1, 2, 3...)"
case .persianDigits:
return "Persian Digits (۱، ۲، ۳...)"
case .words:
return "Persian Words (یک، دو، سه...)"
case .raw:
return "As Recognized (Raw)"
}
}
}
public enum TypingDeliveryMode: String, CaseIterable {
case liveWordByWord = "liveWordByWord"
case batchOnRelease = "batchOnRelease"
public var displayName: String {
switch self {
case .liveWordByWord:
return "⚡️ Live Word-by-Word (Simultaneous Typing)"
case .batchOnRelease:
return "📦 Full Sentence on Release (After Speaking)"
}
}
}
public enum LiveStreamEngineMode: String, CaseIterable {
case provisionalRewrite = "provisionalRewrite"
case confirmedFinalsOnly = "confirmedFinalsOnly"
public var displayName: String {
switch self {
case .provisionalRewrite:
return "✨ Instant Live Words + Auto-Polish on Finish (Recommended)"
case .confirmedFinalsOnly:
return "🔒 Confirmed Final Words Only (No Rewrite)"
}
}
}
public enum ClipboardBehavior: String, CaseIterable {
case copyFinalOnce = "copyFinalOnce"
case preserveOriginal = "preserveOriginal"
case copyInBatchOnly = "copyInBatchOnly"
public var displayName: String {
switch self {
case .copyFinalOnce:
return "📋 Copy Full Text Once at End (Default)"
case .preserveOriginal:
return "🛡️ Preserve Previous Clipboard (Zero History)"
case .copyInBatchOnly:
return "📦 Copy Only in Full-Sentence Mode"
}
}
}
public enum MicStandbyMode: String, CaseIterable {
case immediateOff = "immediateOff"
case warmCooldown15s = "warmCooldown15s"
public var displayName: String {
switch self {
case .immediateOff:
return "🔇 Turn Mic Off Immediately When Idle (Default)"
case .warmCooldown15s:
return "⏱️ Auto-Off After 15s Idle (Fast Back-to-Back)"
}
}
}
public enum GatewayRouteMode: String, CaseIterable {
case autoSmart = "autoSmart"
case iranDirect = "iranDirect"
case iranArvan = "iranArvan"
case germany = "germany"
public var displayName: String {
switch self {
case .autoSmart:
return "🤖 Auto (Iran 2.180.3.35 on IR IP / Germany Outside)"
case .iranDirect:
return "🇮🇷 Iran Direct (2.180.3.35:8097)"
case .iranArvan:
return "🇮🇷 Iran Edge TLS (stt2.nwhco.ir)"
case .germany:
return "🇩🇪 Germany Edge (stt.newhorizonco.uk)"
}
}
}
public final class SonioxSettings {
public static let shared = SonioxSettings()
private let defaults = UserDefaults.standard
private init() {
if defaults.object(forKey: "SonioxHotkeyPreset") == nil {
defaults.set(HotkeyPreset.option.rawValue, forKey: "SonioxHotkeyPreset")
}
}
public var language: RecognitionLanguage {
get {
let val = defaults.string(forKey: "SonioxLanguage") ?? RecognitionLanguage.multi.rawValue
return RecognitionLanguage(rawValue: val) ?? .multi
}
set {
defaults.set(newValue.rawValue, forKey: "SonioxLanguage")
SonioxSessionPool.shared.reconnectAll()
}
}
public var targetLanguage: TranslationTargetLanguage {
get {
let val = defaults.string(forKey: "SonioxTargetLanguage") ?? TranslationTargetLanguage.off.rawValue
return TranslationTargetLanguage(rawValue: val) ?? .off
}
set {
defaults.set(newValue.rawValue, forKey: "SonioxTargetLanguage")
SonioxSessionPool.shared.reconnectAll()
}
}
public var numberFormat: NumberFormatOption {
get {
let val = defaults.string(forKey: "SonioxNumberFormat") ?? NumberFormatOption.digits.rawValue
return NumberFormatOption(rawValue: val) ?? .digits
}
set {
defaults.set(newValue.rawValue, forKey: "SonioxNumberFormat")
}
}
public var voicePunctuation: Bool {
get {
if defaults.object(forKey: "SonioxVoicePunctuation") == nil { return true }
return defaults.bool(forKey: "SonioxVoicePunctuation")
}
set {
defaults.set(newValue, forKey: "SonioxVoicePunctuation")
}
}
public var voiceCommands: Bool {
get {
if defaults.object(forKey: "SonioxVoiceCommands") == nil { return true }
return defaults.bool(forKey: "SonioxVoiceCommands")
}
set {
defaults.set(newValue, forKey: "SonioxVoiceCommands")
}
}
public var domainVocabulary: Bool {
get {
if defaults.object(forKey: "SonioxDomainVocabulary") == nil { return true }
return defaults.bool(forKey: "SonioxDomainVocabulary")
}
set {
defaults.set(newValue, forKey: "SonioxDomainVocabulary")
}
}
public var addTrailingSpace: Bool {
get {
if defaults.object(forKey: "SonioxAddTrailingSpace") == nil { return true }
return defaults.bool(forKey: "SonioxAddTrailingSpace")
}
set {
defaults.set(newValue, forKey: "SonioxAddTrailingSpace")
}
}
public var normalizeText: Bool {
get {
if defaults.object(forKey: "SonioxNormalizeText") == nil { return true }
return defaults.bool(forKey: "SonioxNormalizeText")
}
set {
defaults.set(newValue, forKey: "SonioxNormalizeText")
}
}
public var liveStreamingInput: Bool {
get {
return typingDeliveryMode == .liveWordByWord
}
set {
typingDeliveryMode = newValue ? .liveWordByWord : .batchOnRelease
}
}
public var typingDeliveryMode: TypingDeliveryMode {
get {
let val = defaults.string(forKey: "SonioxTypingDeliveryMode") ?? TypingDeliveryMode.liveWordByWord.rawValue
return TypingDeliveryMode(rawValue: val) ?? .liveWordByWord
}
set {
defaults.set(newValue.rawValue, forKey: "SonioxTypingDeliveryMode")
}
}
public var liveStreamEngineMode: LiveStreamEngineMode {
get {
let val = defaults.string(forKey: "SonioxLiveStreamEngineMode") ?? LiveStreamEngineMode.provisionalRewrite.rawValue
return LiveStreamEngineMode(rawValue: val) ?? .provisionalRewrite
}
set {
defaults.set(newValue.rawValue, forKey: "SonioxLiveStreamEngineMode")
}
}
public var clipboardBehavior: ClipboardBehavior {
get {
let val = defaults.string(forKey: "SonioxClipboardBehavior") ?? ClipboardBehavior.copyFinalOnce.rawValue
return ClipboardBehavior(rawValue: val) ?? .copyFinalOnce
}
set {
defaults.set(newValue.rawValue, forKey: "SonioxClipboardBehavior")
}
}
public var micStandbyMode: MicStandbyMode {
get {
let val = defaults.string(forKey: "SonioxMicStandbyMode") ?? MicStandbyMode.immediateOff.rawValue
return MicStandbyMode(rawValue: val) ?? .immediateOff
}
set {
defaults.set(newValue.rawValue, forKey: "SonioxMicStandbyMode")
}
}
public var gatewayRouteMode: GatewayRouteMode {
get {
let val = defaults.string(forKey: "SonioxGatewayRouteMode") ?? GatewayRouteMode.autoSmart.rawValue
return GatewayRouteMode(rawValue: val) ?? .autoSmart
}
set {
defaults.set(newValue.rawValue, forKey: "SonioxGatewayRouteMode")
PingMonitor.shared.refreshGatewayRoute()
}
}
public var playSounds: Bool {
get {
if defaults.object(forKey: "SonioxPlaySounds") == nil { return true }
return defaults.bool(forKey: "SonioxPlaySounds")
}
set {
defaults.set(newValue, forKey: "SonioxPlaySounds")
}
}
public var launchAtLogin: Bool {
get {
return defaults.bool(forKey: "SonioxLaunchAtLogin")
}
set {
defaults.set(newValue, forKey: "SonioxLaunchAtLogin")
updateLaunchAtLogin(enabled: newValue)
}
}
public var preferredAudioDeviceID: String? {
get {
return defaults.string(forKey: "SonioxPreferredAudioDevice")
}
set {
if let val = newValue {
defaults.set(val, forKey: "SonioxPreferredAudioDevice")
} else {
defaults.removeObject(forKey: "SonioxPreferredAudioDevice")
}
}
}
public func resetToDefaults() {
defaults.removeObject(forKey: "SonioxHotkeyPreset")
defaults.removeObject(forKey: "SonioxDictationMode")
defaults.removeObject(forKey: "SonioxLanguage")
defaults.removeObject(forKey: "SonioxTargetLanguage")
defaults.removeObject(forKey: "SonioxNumberFormat")
defaults.removeObject(forKey: "SonioxVoicePunctuation")
defaults.removeObject(forKey: "SonioxDomainVocabulary")
defaults.removeObject(forKey: "SonioxAddTrailingSpace")
defaults.removeObject(forKey: "SonioxNormalizeText")
defaults.removeObject(forKey: "SonioxTypingDeliveryMode")
defaults.removeObject(forKey: "SonioxLiveStreamEngineMode")
defaults.removeObject(forKey: "SonioxClipboardBehavior")
defaults.removeObject(forKey: "SonioxMicStandbyMode")
defaults.removeObject(forKey: "SonioxGatewayRouteMode")
defaults.removeObject(forKey: "SonioxPlaySounds")
defaults.removeObject(forKey: "SonioxLaunchAtLogin")
defaults.removeObject(forKey: "SonioxPreferredAudioDevice")
updateLaunchAtLogin(enabled: false)
HotkeyManager.shared.registerHotkeys()
SonioxSessionPool.shared.reconnectAll()
}
public func ensureHardwareKeyRemapping() {
// Disabled: do not mess with system-wide hardware Option key mappings
}
private func installRemappingLaunchAgent() {
let launchAgentDir = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Library/LaunchAgents")
let plistURL = launchAgentDir.appendingPathComponent("com.soniox.keymapping.plist")
try? FileManager.default.createDirectory(at: launchAgentDir, withIntermediateDirectories: true)
let plistContent = """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.soniox.keymapping</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/hidutil</string>
<string>property</string>
<string>--set</string>
<string>{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":3221225679,"HIDKeyboardModifierMappingDst":1879048254},{"HIDKeyboardModifierMappingSrc":4294967451,"HIDKeyboardModifierMappingDst":1879048254}]}</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
"""
try? plistContent.write(to: plistURL, atomically: true, encoding: .utf8)
}
private func updateLaunchAtLogin(enabled: Bool) {
let launchAgentDir = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Library/LaunchAgents")
let plistURL = launchAgentDir.appendingPathComponent("com.soniox.voice.plist")
if enabled {
try? FileManager.default.createDirectory(at: launchAgentDir, withIntermediateDirectories: true)
let plistContent = """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.soniox.voice</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/Soniox Voice.app/Contents/MacOS/SonioxVoice</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
"""
try? plistContent.write(to: plistURL, atomically: true, encoding: .utf8)
} else {
try? FileManager.default.removeItem(at: plistURL)
}
}
}