diff --git a/Packages/Features/Sources/Recording/Services/AudioTranscribingService.swift b/Packages/Features/Sources/Recording/Services/AudioTranscribingService.swift index f24f62b..64f672e 100644 --- a/Packages/Features/Sources/Recording/Services/AudioTranscribingService.swift +++ b/Packages/Features/Sources/Recording/Services/AudioTranscribingService.swift @@ -4,14 +4,26 @@ import Speech /// The transcribing service that transcribes recorded audio into text on device. /// /// The service writes the recorded audio into a temporary `.m4a` file and runs it through a `SpeechAnalyzer` with a `SpeechTranscriber` -/// module, joining the finalized results into the returned text. The speech model assets for the current locale are downloaded and installed -/// on first use; every transcription after that happens entirely offline. +/// module, joining the finalized results into the returned text. The transcription happens in the locale given at initialization — or rather in the +/// closest equivalent the transcriber supports. The speech model assets for that locale are downloaded and installed on first use; every +/// transcription after that happens entirely offline. public struct AudioTranscribingService: TranscribingService { + // MARK: Properties + + /// The locale of the spoken language to transcribe. + private let locale: Locale + // MARK: Initializers - /// Creates an audio transcribing service. - public init() {} + /// Creates an audio transcribing service for a locale. + /// + /// - Parameter locale: The locale of the spoken language to transcribe. Defaults to the user's current locale. + public init( + locale: Locale = .current + ) { + self.locale = locale + } // MARK: Methods @@ -19,16 +31,20 @@ public struct AudioTranscribingService: TranscribingService { /// /// - Parameter audio: The recorded audio to transcribe. /// - Returns: The transcription of the recorded audio. - /// - Throws: ``AudioTranscribingError/localeNotSupported`` when the transcriber does not support the current locale, or any - /// error thrown while installing the speech model assets, reading the audio file, or analyzing its contents. - public func transcribe(_ audio: Data) async throws -> String { + /// - Throws: ``AudioTranscribingError/localeNotSupported`` when the transcriber supports no equivalent of the service's locale, + /// or any error thrown while installing the speech model assets, reading the audio file, or analyzing its contents. + public func transcribe( + _ audio: Data + ) async throws -> String { try audio.write(to: Constant.File.url) defer { try? FileManager.default.removeItem(at: Constant.File.url) } - guard let locale = await SpeechTranscriber.supportedLocale(equivalentTo: .current) else { + guard let locale = await SpeechTranscriber.supportedLocale( + equivalentTo: locale + ) else { throw AudioTranscribingError.localeNotSupported } @@ -64,7 +80,7 @@ public struct AudioTranscribingService: TranscribingService { /// The errors thrown by ``AudioTranscribingService``. public enum AudioTranscribingError: Error { - /// The transcriber does not support the current locale. + /// The transcriber supports no equivalent of the locale the service was created with. case localeNotSupported } diff --git a/Packages/Features/Sources/Recording/Services/DummyTranscribingService.swift b/Packages/Features/Sources/Recording/Services/DummyTranscribingService.swift index bdff9a9..a869f6e 100644 --- a/Packages/Features/Sources/Recording/Services/DummyTranscribingService.swift +++ b/Packages/Features/Sources/Recording/Services/DummyTranscribingService.swift @@ -12,7 +12,9 @@ struct DummyTranscribingService: TranscribingService { /// /// - Parameter audio: The recorded audio to transcribe. /// - Returns: A dummy transcription. - func transcribe(_ audio: Data) async throws -> String { + func transcribe( + _ audio: Data + ) async throws -> String { try await Task.sleep(for: Constant.Delay.transcribing) return Constant.Text.transcription