Skip to content

Commit

Permalink
Add resumeRecordingLive function (#138)
Browse files Browse the repository at this point in the history
* add reStartRecordingLive function

* rewrite function comment

* add function call

* correct the code format

* correct for code format

* Update Sources/WhisperKit/Core/AudioProcessor.swift

Co-authored-by: Zach Nagengast <[email protected]>

* Update Sources/WhisperKit/Core/AudioProcessor.swift

Co-authored-by: Zach Nagengast <[email protected]>

* Update Sources/WhisperKit/Core/AudioProcessor.swift

Co-authored-by: Zach Nagengast <[email protected]>

* Update Sources/WhisperKit/Core/AudioProcessor.swift

Co-authored-by: Zach Nagengast <[email protected]>

* change method name for better understanding

* Update Sources/WhisperKit/Core/AudioProcessor.swift

Co-authored-by: Zach Nagengast <[email protected]>

---------

Co-authored-by: cunhanfeng <[email protected]>
Co-authored-by: Zach Nagengast <[email protected]>
  • Loading branch information
3 people committed May 15, 2024
1 parent d41566a commit 984de54
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Sources/WhisperKit/Core/AudioProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,20 @@ public protocol AudioProcessing {

/// Stops recording and cleans up resources
func stopRecording()

/// Resume recording audio from the specified input device, appending to continuous `audioArray` after pause
func resumeRecordingLive(inputDeviceID: DeviceID?, callback: (([Float]) -> Void)?) throws
}

/// Overrideable default methods for AudioProcessing
public extension AudioProcessing {
func startRecordingLive(inputDeviceID: DeviceID? = nil, callback: (([Float]) -> Void)?) throws {
try startRecordingLive(inputDeviceID: inputDeviceID, callback: callback)
}

func resumeRecordingLive(inputDeviceID: DeviceID? = nil, callback: (([Float]) -> Void)?) throws {
try resumeRecordingLive(inputDeviceID: inputDeviceID, callback: callback)
}

static func padOrTrimAudio(fromArray audioArray: [Float], startAt startIndex: Int = 0, toLength frameLength: Int = 480_000, saveSegment: Bool = false) -> MLMultiArray? {
let currentFrameLength = audioArray.count
Expand Down Expand Up @@ -150,6 +157,7 @@ public extension AudioProcessing {

@available(macOS 13, iOS 16, watchOS 10, visionOS 1, *)
public class AudioProcessor: NSObject, AudioProcessing {
private var lastInputDevice:DeviceID?
public var audioEngine: AVAudioEngine?
public var audioSamples: ContiguousArray<Float> = []
public var audioEnergy: [(rel: Float, avg: Float, max: Float, min: Float)] = []
Expand Down Expand Up @@ -587,6 +595,24 @@ public extension AudioProcessor {

// Set the callback
audioBufferCallback = callback

lastInputDevice = inputDeviceID
}

func resumeRecordingLive(inputDeviceID: DeviceID? = nil, callback: (([Float]) -> Void)? = nil) throws {
try? setupAudioSessionForDevice()

if inputDeviceID == lastInputDevice{
try audioEngine?.start()
} else {
audioEngine = try setupEngine(inputDeviceID: inputDeviceID)
}


// Set the callback only if the provided callback is not nil
if let callback = callback {
audioBufferCallback = callback
}
}

func pauseRecording() {
Expand Down

0 comments on commit 984de54

Please sign in to comment.