2026-07-03 22:13:16 +02:00
|
|
|
import AVFoundation
|
|
|
|
|
import Speech
|
|
|
|
|
|
2026-07-03 23:57:49 +02:00
|
|
|
/// The transcribing service that transcribes recorded audio into text on device.
|
2026-07-03 22:13:16 +02:00
|
|
|
///
|
|
|
|
|
/// The service writes the recorded audio into a temporary `.m4a` file and runs it through a `SpeechAnalyzer` with a `SpeechTranscriber`
|
2026-07-04 10:15:18 +02:00
|
|
|
/// 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.
|
2026-07-03 23:57:49 +02:00
|
|
|
public struct AudioTranscribingService: TranscribingService {
|
2026-07-03 22:13:16 +02:00
|
|
|
|
2026-07-04 10:15:18 +02:00
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
|
|
/// The locale of the spoken language to transcribe.
|
|
|
|
|
private let locale: Locale
|
|
|
|
|
|
2026-07-03 22:13:16 +02:00
|
|
|
// MARK: Initializers
|
|
|
|
|
|
2026-07-04 10:15:18 +02:00
|
|
|
/// 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
|
|
|
|
|
}
|
2026-07-03 22:13:16 +02:00
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
/// - Returns: The transcription of the recorded audio.
|
2026-07-04 10:15:18 +02:00
|
|
|
/// - 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 {
|
2026-07-03 22:13:16 +02:00
|
|
|
try audio.write(to: Constant.File.url)
|
|
|
|
|
|
|
|
|
|
defer {
|
|
|
|
|
try? FileManager.default.removeItem(at: Constant.File.url)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-04 10:15:18 +02:00
|
|
|
guard let locale = await SpeechTranscriber.supportedLocale(
|
|
|
|
|
equivalentTo: locale
|
|
|
|
|
) else {
|
2026-07-03 22:13:16 +02:00
|
|
|
throw AudioTranscribingError.localeNotSupported
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let transcriber = SpeechTranscriber(
|
|
|
|
|
locale: locale,
|
|
|
|
|
preset: .transcription
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if let request = try await AssetInventory.assetInstallationRequest(supporting: [transcriber]) {
|
|
|
|
|
try await request.downloadAndInstall()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let analyzer = SpeechAnalyzer(modules: [transcriber])
|
|
|
|
|
|
|
|
|
|
async let text = transcriber.results.reduce(into: "") { text, result in
|
|
|
|
|
text += String(result.text.characters)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let file = try AVAudioFile(forReading: Constant.File.url)
|
|
|
|
|
|
|
|
|
|
if let lastSampleTime = try await analyzer.analyzeSequence(from: file) {
|
|
|
|
|
try await analyzer.finalizeAndFinish(through: lastSampleTime)
|
|
|
|
|
} else {
|
|
|
|
|
await analyzer.cancelAndFinishNow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return try await text
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Errors
|
|
|
|
|
|
|
|
|
|
/// The errors thrown by ``AudioTranscribingService``.
|
2026-07-03 23:57:49 +02:00
|
|
|
public enum AudioTranscribingError: Error {
|
2026-07-04 10:15:18 +02:00
|
|
|
/// The transcriber supports no equivalent of the locale the service was created with.
|
2026-07-03 22:13:16 +02:00
|
|
|
case localeNotSupported
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Constants
|
|
|
|
|
|
2026-07-03 23:57:49 +02:00
|
|
|
/// The constant values used across the audio transcribing service.
|
|
|
|
|
private enum Constant {
|
2026-07-03 22:13:16 +02:00
|
|
|
/// The file constants.
|
|
|
|
|
enum File {
|
|
|
|
|
/// The location of the temporary file the recorded audio is written into for the analysis.
|
|
|
|
|
static let url = FileManager.default.temporaryDirectory.appending(path: "transcription.m4a")
|
|
|
|
|
}
|
|
|
|
|
}
|