Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active January 24, 2021 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/ac104ddc9e189eb7e4d805e4ba2fc286 to your computer and use it in GitHub Desktop.
Save jasdev/ac104ddc9e189eb7e4d805e4ba2fc286 to your computer and use it in GitHub Desktop.
A sketch of a transcription effect in TCA.
import ComposableArchitecture
let transcriptionEffect: (URL) -> Effect<String, NSError> = { audioURL in
.future { callback in
guard
let speechRecognizer = SFSpeechRecognizer(),
speechRecognizer.isAvailable // (1)
else {
callback(
.failure(
NSError(
domain: "com.jasdev.CaptionsGeneration",
code: 1,
userInfo: nil
)
)
)
return
}
let request = SFSpeechURLRecognitionRequest(url: audioURL)
request.shouldReportPartialResults = false // (2)
speechRecognizer.recognitionTask(with: request) { [weak request] result, error in
if let error = error {
callback(.failure(error as NSError))
} else {
callback(
.success(
transcriptionSegmentsToSubRipText(
result!
.bestTranscription
.segments
)
)
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment