-
-
Save jasdev/ac104ddc9e189eb7e4d805e4ba2fc286 to your computer and use it in GitHub Desktop.
A sketch of a transcription effect in TCA.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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