Made the transcribing callable and per-call for the transcribing protocol in the Recording package target.
This commit is contained in:
+15
-23
@@ -4,38 +4,30 @@ 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 transcription happens in the locale given at initialization — or rather in the
|
||||
/// module, joining the finalized results into the returned transcription. The transcription happens in the locale given at each call — 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
|
||||
public struct AudioTranscribing: Transcribing {
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// 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
|
||||
}
|
||||
/// Creates an audio transcribing service.
|
||||
public init() {}
|
||||
|
||||
// MARK: Methods
|
||||
|
||||
/// Transcribes the given recorded audio into text, deleting the temporary audio file when the transcription finishes.
|
||||
///
|
||||
/// - Parameter audio: The recorded audio to transcribe.
|
||||
/// - Parameters:
|
||||
/// - audio: The recorded audio to transcribe.
|
||||
/// - locale: The locale of the spoken language to transcribe.
|
||||
/// - Returns: The transcription of the recorded audio.
|
||||
/// - Throws: ``AudioTranscribingError/localeNotSupported`` when the transcriber supports no equivalent of the service's locale,
|
||||
/// - Throws: ``AudioTranscribingError/localeNotSupported`` when the transcriber supports no equivalent of the given 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 {
|
||||
public func callAsFunction(
|
||||
_ audio: Data,
|
||||
locale: Locale
|
||||
) async throws -> Transcription {
|
||||
try audio.write(to: Constant.File.url)
|
||||
|
||||
defer {
|
||||
@@ -71,16 +63,16 @@ public struct AudioTranscribingService: TranscribingService {
|
||||
await analyzer.cancelAndFinishNow()
|
||||
}
|
||||
|
||||
return try await text
|
||||
return try await .init(text: text)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Errors
|
||||
|
||||
/// The errors thrown by ``AudioTranscribingService``.
|
||||
/// The errors thrown by ``AudioTranscribing``.
|
||||
public enum AudioTranscribingError: Error {
|
||||
/// The transcriber supports no equivalent of the locale the service was created with.
|
||||
/// The transcriber supports no equivalent of the locale the transcription was requested with.
|
||||
case localeNotSupported
|
||||
}
|
||||
|
||||
+9
-6
@@ -4,20 +4,23 @@ import Foundation
|
||||
///
|
||||
/// The service is internal on purpose: it only backs the feature's previews and the default values of its model, and is not part of the
|
||||
/// package's public interface.
|
||||
struct DummyTranscribingService: TranscribingService {
|
||||
struct DummyTranscribing: Transcribing {
|
||||
|
||||
// MARK: Methods
|
||||
|
||||
/// Simulates the transcription of the given recorded audio with a two-second delay.
|
||||
///
|
||||
/// - Parameter audio: The recorded audio to transcribe.
|
||||
/// - Parameters:
|
||||
/// - audio: The recorded audio to transcribe.
|
||||
/// - locale: The locale of the spoken language to transcribe.
|
||||
/// - Returns: A dummy transcription.
|
||||
func transcribe(
|
||||
_ audio: Data
|
||||
) async throws -> String {
|
||||
func callAsFunction(
|
||||
_ audio: Data,
|
||||
locale: Locale
|
||||
) async throws -> Transcription {
|
||||
try await Task.sleep(for: Constant.Delay.transcribing)
|
||||
|
||||
return Constant.Text.transcription
|
||||
return .init(text: Constant.Text.transcription)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user