2026-07-03 22:13:16 +02:00
import AVFoundation
2026-07-04 15:58:32 +02:00
import OSLog
2026-07-03 22:13:16 +02:00
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
///
2026-07-04 15:06:55 +02:00
/// The service runs the given recorded audio file through a `SpeechAnalyzer` with a `SpeechTranscriber` 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
2026-07-05 13:34:16 +02:00
/// the transcriber supports. Any speech model assets still missing for that locale are installed first through ``AssetPreinstalling``;
/// every transcription after that happens entirely offline.
2026-07-04 12:03:19 +02:00
public struct AudioTranscribing : Transcribing {
2026-07-04 10:15:18 +02:00
2026-07-05 13:34:16 +02:00
// MARK: Properties
/// The preinstalling service that installs any speech model assets still missing when a transcription starts.
private let preinstaller = AssetPreinstalling ()
2026-07-03 22:13:16 +02:00
// MARK: Initializers
2026-07-04 12:03:19 +02:00
/// Creates an audio transcribing service.
public init () {}
2026-07-03 22:13:16 +02:00
// MARK: Methods
2026-07-04 15:06:55 +02:00
/// Transcribes the given recorded audio file into text, deleting the file when the transcription finishes.
2026-07-03 22:13:16 +02:00
///
2026-07-04 12:03:19 +02:00
/// - Parameters:
2026-07-04 15:06:55 +02:00
/// - audio: The location of the recorded audio file to transcribe.
2026-07-04 12:03:19 +02:00
/// - locale: The locale of the spoken language to transcribe.
2026-07-03 22:13:16 +02:00
/// - Returns: The transcription of the recorded audio.
2026-07-04 12:03:19 +02:00
/// - Throws: ``AudioTranscribingError/localeNotSupported`` when the transcriber supports no equivalent of the given locale,
2026-07-04 15:58:32 +02:00
/// ``AudioTranscribingError/assetsNotInstalled`` when the speech model assets fail to reserve, download, or install,
/// or any error thrown while reading the audio file or analyzing its contents.
2026-07-04 12:03:19 +02:00
public func callAsFunction (
2026-07-04 15:06:55 +02:00
_ audio : URL ,
2026-07-04 12:03:19 +02:00
locale : Locale
) async throws -> Transcription {
2026-07-03 22:13:16 +02:00
defer {
2026-07-04 15:06:55 +02:00
try ? FileManager . default . removeItem ( at : audio )
2026-07-03 22:13:16 +02:00
}
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
)
2026-07-04 15:58:32 +02:00
do {
2026-07-05 13:34:16 +02:00
try await preinstaller . install ( for : locale )
2026-07-04 15:58:32 +02:00
} catch {
logger . error ( "The speech model assets for the \" \( locale . identifier , privacy : . public ) \" locale failed to install: \( String ( describing : error ) , privacy : . public ) " )
throw AudioTranscribingError . assetsNotInstalled
2026-07-03 22:13:16 +02:00
}
let analyzer = SpeechAnalyzer ( modules : [ transcriber ])
2026-07-04 19:17:18 +02:00
async let text = transcriber . results
. reduce ( into : "" ) { text , result in
text += String ( result . text . characters )
}
2026-07-03 22:13:16 +02:00
2026-07-04 15:06:55 +02:00
let file = try AVAudioFile ( forReading : audio )
2026-07-03 22:13:16 +02:00
if let lastSampleTime = try await analyzer . analyzeSequence ( from : file ) {
try await analyzer . finalizeAndFinish ( through : lastSampleTime )
} else {
await analyzer . cancelAndFinishNow ()
}
2026-07-04 12:03:19 +02:00
return try await . init ( text : text )
2026-07-03 22:13:16 +02:00
}
}
2026-07-04 15:58:32 +02:00
// MARK: - Constants
/// The logger that records the failures of the audio transcribing service.
private let logger = Logger (
subsystem : "Features.Recording" ,
category : "AudioTranscribing"
)