Skip to content

Commit

Permalink
feat: add auto finish of speech recognition to ios
Browse files Browse the repository at this point in the history
  • Loading branch information
daheeahn committed Jun 21, 2024
1 parent 796a341 commit a0efcf6
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions ios/ExpoSttModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,36 @@ enum AuthorizationStatus {
case notDetermined, denied, restricted, authorized
}
public class ExpoSttModule: Module {
private var averagePowerForChannel0: Float?
private var averagePowerForChannel1: Float?
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
private var recognitionTask: SFSpeechRecognitionTask?
private var recognitionTask: SFSpeechRecognitionTask? // A task object for monitoring the speech recognition progress.
private var audioEngine: AVAudioEngine?
private var audioSession: AVAudioSession?
private var speechRecognizer: SFSpeechRecognizer?
private var speechRecognizer: SFSpeechRecognizer? // An object you use to check for the availability of the speech recognition service, and to initiate the speech recognition process.
private var sessionId: String?
private var priorAudioCategory: AVAudioSession.Category?
private var priorAudioCategoryOptions: AVAudioSession.CategoryOptions?
private var isTearingDown: Bool = false
private var continuous: Bool = false

private var silenceTimer: Timer?
private let silenceTimeout: TimeInterval = 5.0 // 5 seconds of silence before stopping

private func startSilenceTimer() {
silenceTimer = Timer.scheduledTimer(withTimeInterval: silenceTimeout, repeats: false) { [weak self] _ in
self?.handleSilenceTimeout()
}
}

private func resetSilenceTimer() {
silenceTimer?.invalidate()
startSilenceTimer()
}

private func handleSilenceTimeout() {
recognitionTask?.finish()
sendEvent(ReactEvents.onSpeechEnd.rawValue)
teardown()
}

private func teardown() {
isTearingDown = true
Expand Down Expand Up @@ -177,6 +195,9 @@ public class ExpoSttModule: Module {
if !self.continuous {
self.teardown()
}
} else {
// Reset the silence timer if partial results are returned
self.resetSilenceTimer()
}
})

Expand All @@ -191,6 +212,7 @@ public class ExpoSttModule: Module {
audioEngine?.connect(inputNode!, to: mixer, format: recordingFormat)
audioEngine?.prepare()
try audioEngine?.start()
startSilenceTimer()
}

public func definition() -> ModuleDefinition {
Expand Down

0 comments on commit a0efcf6

Please sign in to comment.