diff --git a/AUTHORS b/AUTHORS index cb04c967..fd7435bb 100644 --- a/AUTHORS +++ b/AUTHORS @@ -2,7 +2,7 @@ $ Main contributors and volunteers of this repository (vChewing for macOS): - Shiki Suen // Main developer of vChewing for macOS, Megrez language engine, and Tekkon syllable composer engine. - Hiraku Wang // Technical reinforcement in Cocoa during the Object-Cpp dev period of this project. -- Isaac Xen // Technical reinforcement in Swift: SFX Module and StringView Ranges Extension. +- Isaac Xen // Technical reinforcement in Swift: SFX Module (NSSound ver.) and StringView Ranges Extension. $ Contributors and volunteeres of the upstream repo, having no responsibility in discussing anything in the current repo: diff --git a/Source/Modules/SFX/clsSFX.swift b/Source/Modules/SFX/clsSFX.swift index 3fc43f7b..6a6b4e04 100644 --- a/Source/Modules/SFX/clsSFX.swift +++ b/Source/Modules/SFX/clsSFX.swift @@ -1,6 +1,4 @@ -// Copyright (c) 2022 and onwards Isaac Xen (MIT License). -// All possible vChewing-specific modifications are of: -// (c) 2021 and onwards The vChewing Project (MIT-NTL License). +// Copyright (c) 2021 and onwards The vChewing Project (MIT-NTL License). /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in @@ -24,52 +22,15 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import Cocoa - -public class clsSFX: NSObject, NSSoundDelegate { - private static let shared = clsSFX() - override private init() { - super.init() - } - - private var currentBeep: NSSound? - private func beep() { - let defaultVolume: Float = 0.4 - // Stop existing beep - if let beep = currentBeep { - if beep.isPlaying { - for i in 1..<30 { - beep.volume = (defaultVolume / Float(i)) - usleep(1000) - } - beep.stop() - beep.volume = defaultVolume - } - } - // Create a new beep sound if possible - var sndBeep: String - if mgrPrefs.shouldNotFartInLieuOfBeep == false { - sndBeep = "Fart" - } else { - sndBeep = "Beep" - } - guard - let beep = NSSound(named: sndBeep) - else { - NSSound.beep() - return - } - beep.delegate = self - beep.volume = defaultVolume - beep.play() - currentBeep = beep - } - - public func sound(_: NSSound, didFinishPlaying _: Bool) { - currentBeep = nil - } +import AVFoundation +import Foundation +public class clsSFX { static func beep() { - shared.beep() + let filePath = Bundle.main.path(forResource: mgrPrefs.shouldNotFartInLieuOfBeep ? "Beep" : "Fart", ofType: "m4a")! + let fileURL = URL(fileURLWithPath: filePath) + var soundID: SystemSoundID = 0 + AudioServicesCreateSystemSoundID(fileURL as CFURL, &soundID) + AudioServicesPlaySystemSound(soundID) } }