Added the "locale" parameter to the initializer for the AudioTranscribingService service in the Recording package target.
This commit is contained in:
@@ -4,14 +4,26 @@ import Speech
|
|||||||
/// The transcribing service that transcribes recorded audio into text on device.
|
/// 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`
|
/// 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
|
/// module, joining the finalized results into the returned text. The transcription happens in the locale given at initialization — or rather in the
|
||||||
/// on first use; every transcription after that happens entirely offline.
|
/// 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 {
|
public struct AudioTranscribingService: TranscribingService {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
/// The locale of the spoken language to transcribe.
|
||||||
|
private let locale: Locale
|
||||||
|
|
||||||
// MARK: Initializers
|
// MARK: Initializers
|
||||||
|
|
||||||
/// Creates an audio transcribing service.
|
/// Creates an audio transcribing service for a locale.
|
||||||
public init() {}
|
///
|
||||||
|
/// - 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
|
// MARK: Methods
|
||||||
|
|
||||||
@@ -19,16 +31,20 @@ public struct AudioTranscribingService: TranscribingService {
|
|||||||
///
|
///
|
||||||
/// - Parameter audio: The recorded audio to transcribe.
|
/// - Parameter audio: The recorded audio to transcribe.
|
||||||
/// - Returns: The transcription of the recorded audio.
|
/// - Returns: The transcription of the recorded audio.
|
||||||
/// - Throws: ``AudioTranscribingError/localeNotSupported`` when the transcriber does not support the current locale, or any
|
/// - Throws: ``AudioTranscribingError/localeNotSupported`` when the transcriber supports no equivalent of the service's locale,
|
||||||
/// error thrown while installing the speech model assets, reading the audio file, or analyzing its contents.
|
/// 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 {
|
public func transcribe(
|
||||||
|
_ audio: Data
|
||||||
|
) async throws -> String {
|
||||||
try audio.write(to: Constant.File.url)
|
try audio.write(to: Constant.File.url)
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
try? FileManager.default.removeItem(at: Constant.File.url)
|
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
|
throw AudioTranscribingError.localeNotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +80,7 @@ public struct AudioTranscribingService: TranscribingService {
|
|||||||
|
|
||||||
/// The errors thrown by ``AudioTranscribingService``.
|
/// The errors thrown by ``AudioTranscribingService``.
|
||||||
public enum AudioTranscribingError: Error {
|
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
|
case localeNotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,9 @@ struct DummyTranscribingService: TranscribingService {
|
|||||||
///
|
///
|
||||||
/// - Parameter audio: The recorded audio to transcribe.
|
/// - Parameter audio: The recorded audio to transcribe.
|
||||||
/// - Returns: A dummy transcription.
|
/// - 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)
|
try await Task.sleep(for: Constant.Delay.transcribing)
|
||||||
|
|
||||||
return Constant.Text.transcription
|
return Constant.Text.transcription
|
||||||
|
|||||||
Reference in New Issue
Block a user