Added the "locale" parameter to the initializer for the AudioTranscribingService service in the Recording package target.

This commit is contained in:
2026-07-04 10:15:18 +02:00
parent 9c366b98cb
commit 2b62de5154
2 changed files with 28 additions and 10 deletions
@@ -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
}
@@ -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