Skip to content

Commit

Permalink
Issue - 42 WhisperKit support simulator fixed (#52)
Browse files Browse the repository at this point in the history
* Issue - 42 WhisperKit support simulator fixed

* Issue - 42 enhancement done

* Setup AVAudioSession, which fixes crash in simulators

* Add availability checks for model compute options

---------

Co-authored-by: ZachNagengast <[email protected]>
  • Loading branch information
bharat9806 and ZachNagengast authored Mar 8, 2024
1 parent ccdd77d commit bfa357e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"originHash" : "cd17206b47bb810af9459722192530e3838d8e6629a970988e32a432aaa05f6e",
"pins" : [
{
"identity" : "networkimage",
Expand Down Expand Up @@ -37,5 +38,5 @@
}
}
],
"version" : 2
"version" : 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct WhisperAXWatchView: View {

var body: some View {
NavigationSplitView {
if WhisperKit.deviceName().hasPrefix("Watch7") {
if WhisperKit.deviceName().hasPrefix("Watch7") || WhisperKit.isRunningOnSimulator {
modelSelectorView
.navigationTitle("WhisperAX")
.navigationBarTitleDisplayMode(.automatic)
Expand Down
24 changes: 23 additions & 1 deletion Sources/WhisperKit/Core/AudioProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,27 @@ public extension AudioProcessor {
}
}
#endif


/// Attempts to setup the shared audio session if available on the device's OS
func setupAudioSessionForDevice() throws {
#if !os(macOS) // AVAudioSession is not available on macOS

#if !os(watchOS) // watchOS does not support .defaultToSpeaker
let options: AVAudioSession.CategoryOptions = [.defaultToSpeaker, .allowBluetooth]
#else
let options: AVAudioSession.CategoryOptions = .mixWithOthers
#endif

let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playAndRecord, options: options)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
} catch let error as NSError {
throw WhisperError.audioProcessingFailed("Failed to set up audio session: \(error)")
}
#endif
}

func setupEngine(inputDeviceID: DeviceID? = nil) throws -> AVAudioEngine {
let audioEngine = AVAudioEngine()
let inputNode = audioEngine.inputNode
Expand Down Expand Up @@ -546,6 +566,8 @@ public extension AudioProcessor {
func startRecordingLive(inputDeviceID: DeviceID? = nil, callback: (([Float]) -> Void)? = nil) throws {
audioSamples = []
audioEnergy = []

try? setupAudioSessionForDevice()

audioEngine = try setupEngine(inputDeviceID: inputDeviceID)

Expand Down
8 changes: 8 additions & 0 deletions Sources/WhisperKit/Core/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public enum ModelState: CustomStringConvertible {
}
}

@available(macOS 13, iOS 16, watchOS 10, visionOS 1, *)
public struct ModelComputeOptions {
public var melCompute: MLComputeUnits
public var audioEncoderCompute: MLComputeUnits
Expand All @@ -138,6 +139,13 @@ public struct ModelComputeOptions {
textDecoderCompute: MLComputeUnits = .cpuAndNeuralEngine,
prefillCompute: MLComputeUnits = .cpuOnly
) {
if WhisperKit.isRunningOnSimulator {
self.melCompute = .cpuOnly
self.audioEncoderCompute = .cpuOnly
self.textDecoderCompute = .cpuOnly
self.prefillCompute = .cpuOnly
return
}
self.melCompute = melCompute
self.audioEncoderCompute = audioEncoderCompute
self.textDecoderCompute = textDecoderCompute
Expand Down
11 changes: 11 additions & 0 deletions Sources/WhisperKit/Core/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ extension Process {
}
#endif

@available(macOS 13, iOS 16, watchOS 10, visionOS 1, *)
public extension WhisperKit {
static var isRunningOnSimulator: Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}

public func resolveAbsolutePath(_ inputPath: String) -> String {
let fileManager = FileManager.default

Expand Down

0 comments on commit bfa357e

Please sign in to comment.