|
| 1 | +import Foundation |
| 2 | +import os |
| 3 | + |
| 4 | +private let logger = Logger(subsystem: "com.typeless.app", category: "ASREngineFactory") |
| 5 | + |
| 6 | +/// Creates and initializes ASR engine instances based on the selected model type. |
| 7 | +/// |
| 8 | +/// Encapsulates all model-loading logic (Streaming Paraformer, QwenASR, Dual Engine) |
| 9 | +/// and their supporting models (punctuation, CSC, ITN). |
| 10 | +/// |
| 11 | +/// Thread safety: |
| 12 | +/// - `createEngine(for:pipeline:recognitionQueue:)` is async and called from a Task |
| 13 | +/// spawned on stateQueue. It writes to `pipeline` properties which are safe because |
| 14 | +/// no other code touches them during initialization (FSM is in `.initializing` state). |
| 15 | +enum ASREngineFactory { |
| 16 | + |
| 17 | + /// Result of engine creation, containing the engine and a TermNormalizer. |
| 18 | + struct Result { |
| 19 | + let engine: (any ASREngine)? |
| 20 | + let termNormalizer: TermNormalizer? |
| 21 | + } |
| 22 | + |
| 23 | + /// Creates an ASR engine for the given model type, also loading supporting models |
| 24 | + /// (punctuation, CSC, ITN, TermNormalizer) into the provided pipeline. |
| 25 | + /// |
| 26 | + /// - Parameters: |
| 27 | + /// - modelType: Which ASR model to load. |
| 28 | + /// - pipeline: The post-processing pipeline to configure with supporting models. |
| 29 | + /// - recognitionQueue: The serial queue used by the ASR engine for decoding. |
| 30 | + /// - Returns: The created ASR engine (nil on failure) and TermNormalizer. |
| 31 | + static func createEngine( |
| 32 | + for modelType: ASRModelType, |
| 33 | + pipeline: PostProcessingPipeline, |
| 34 | + recognitionQueue: DispatchQueue |
| 35 | + ) async -> Result { |
| 36 | + logger.info("开始加载语音识别模型 (\(modelType.displayName, privacy: .public))") |
| 37 | + |
| 38 | + // Clear previous pipeline state |
| 39 | + pipeline.reset() |
| 40 | + |
| 41 | + var engine: (any ASREngine)? |
| 42 | + |
| 43 | + switch modelType { |
| 44 | + case .streamingParaformer: |
| 45 | + engine = await createStreamingParaformer(pipeline: pipeline, recognitionQueue: recognitionQueue) |
| 46 | + case .qwenASR: |
| 47 | + engine = await createQwenASR(pipeline: pipeline, recognitionQueue: recognitionQueue) |
| 48 | + case .dualEngine: |
| 49 | + engine = await createDualEngine(pipeline: pipeline, recognitionQueue: recognitionQueue) |
| 50 | + } |
| 51 | + |
| 52 | + // Load TermNormalizer (shared by all engines) |
| 53 | + let termNormalizer = TermNormalizer() |
| 54 | + if termNormalizer != nil { |
| 55 | + logger.info("TermNormalizer 词典加载成功") |
| 56 | + } else { |
| 57 | + logger.warning("TermNormalizer 词典加载失败,跳过专有名词标准化") |
| 58 | + } |
| 59 | + pipeline.termNormalizer = termNormalizer |
| 60 | + |
| 61 | + return Result(engine: engine, termNormalizer: termNormalizer) |
| 62 | + } |
| 63 | + |
| 64 | + // MARK: - Streaming Paraformer |
| 65 | + |
| 66 | + private static func createStreamingParaformer( |
| 67 | + pipeline: PostProcessingPipeline, |
| 68 | + recognitionQueue: DispatchQueue |
| 69 | + ) async -> (any ASREngine)? { |
| 70 | + guard SherpaOnnxManager.shared.isStreamingParaformerDownloaded(), |
| 71 | + let paths = SherpaOnnxManager.shared.getStreamingParaformerPath() else { |
| 72 | + logger.warning("Streaming Paraformer 模型未下载") |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + let itnFstPath = await loadITNFst() |
| 77 | + |
| 78 | + guard let recognizer = SherpaOnnxOnlineRecognizer( |
| 79 | + encoderPath: paths.encoderPath, |
| 80 | + decoderPath: paths.decoderPath, |
| 81 | + tokensPath: paths.tokensPath, |
| 82 | + ruleFstsPath: itnFstPath |
| 83 | + ) else { |
| 84 | + logger.error("Streaming Paraformer 模型加载失败") |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + let engine = StreamingParaformerEngine( |
| 89 | + recognizer: recognizer, recognitionQueue: recognitionQueue |
| 90 | + ) |
| 91 | + logger.info("Streaming Paraformer 模型加载成功") |
| 92 | + |
| 93 | + // Load CSC + punctuation post-processing modules |
| 94 | + await loadPunctuation(into: pipeline) |
| 95 | + loadCSC(into: pipeline) |
| 96 | + |
| 97 | + return engine |
| 98 | + } |
| 99 | + |
| 100 | + // MARK: - QwenASR |
| 101 | + |
| 102 | + private static func createQwenASR( |
| 103 | + pipeline: PostProcessingPipeline, |
| 104 | + recognitionQueue: DispatchQueue |
| 105 | + ) async -> (any ASREngine)? { |
| 106 | + guard SherpaOnnxManager.shared.isQwenASRModelDownloaded(), |
| 107 | + let modelDir = SherpaOnnxManager.shared.getQwenASRModelDir() else { |
| 108 | + logger.warning("QwenASR 模型未下载") |
| 109 | + return nil |
| 110 | + } |
| 111 | + |
| 112 | + guard let recognizer = QwenASRStreamRecognizer(modelDir: modelDir) else { |
| 113 | + logger.error("QwenASR 流式模型加载失败") |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + let engine = QwenASREngine( |
| 118 | + recognizer: recognizer, recognitionQueue: recognitionQueue |
| 119 | + ) |
| 120 | + logger.info("QwenASR 流式模型加载成功") |
| 121 | + |
| 122 | + // Load standalone ITN for post-processing |
| 123 | + if let itnFstPath = await loadITNFst() { |
| 124 | + pipeline.itn = SherpaOnnxITN(ruleFsts: itnFstPath) |
| 125 | + if pipeline.itn != nil { |
| 126 | + logger.info("ITN 模型加载成功") |
| 127 | + } else { |
| 128 | + logger.warning("ITN 模型初始化失败") |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return engine |
| 133 | + } |
| 134 | + |
| 135 | + // MARK: - Dual Engine |
| 136 | + |
| 137 | + private static func createDualEngine( |
| 138 | + pipeline: PostProcessingPipeline, |
| 139 | + recognitionQueue: DispatchQueue |
| 140 | + ) async -> (any ASREngine)? { |
| 141 | + // 1. Load Streaming Paraformer |
| 142 | + guard SherpaOnnxManager.shared.isStreamingParaformerDownloaded(), |
| 143 | + let paths = SherpaOnnxManager.shared.getStreamingParaformerPath() else { |
| 144 | + logger.warning("Dual Engine: Streaming Paraformer 模型未下载") |
| 145 | + return nil |
| 146 | + } |
| 147 | + |
| 148 | + let itnFstPath = await loadITNFst() |
| 149 | + |
| 150 | + guard let parafoRecognizer = SherpaOnnxOnlineRecognizer( |
| 151 | + encoderPath: paths.encoderPath, |
| 152 | + decoderPath: paths.decoderPath, |
| 153 | + tokensPath: paths.tokensPath, |
| 154 | + ruleFstsPath: itnFstPath |
| 155 | + ) else { |
| 156 | + logger.error("Dual Engine: Streaming Paraformer 模型加载失败") |
| 157 | + return nil |
| 158 | + } |
| 159 | + |
| 160 | + let paraformerEngine = StreamingParaformerEngine( |
| 161 | + recognizer: parafoRecognizer, recognitionQueue: recognitionQueue |
| 162 | + ) |
| 163 | + logger.info("Dual Engine: Streaming Paraformer 加载成功") |
| 164 | + |
| 165 | + // 2. Load QwenASR (for offline precise transcription) |
| 166 | + guard SherpaOnnxManager.shared.isQwenASRModelDownloaded(), |
| 167 | + let modelDir = SherpaOnnxManager.shared.getQwenASRModelDir() else { |
| 168 | + logger.warning("Dual Engine: QwenASR 模型未下载") |
| 169 | + return nil |
| 170 | + } |
| 171 | + |
| 172 | + guard let qwenRecognizer = QwenASRStreamRecognizer(modelDir: modelDir) else { |
| 173 | + logger.error("Dual Engine: QwenASR 模型加载失败") |
| 174 | + return nil |
| 175 | + } |
| 176 | + logger.info("Dual Engine: QwenASR 加载成功") |
| 177 | + |
| 178 | + // 3. Create dual engine |
| 179 | + let engine = DualEngineASR( |
| 180 | + paraformerEngine: paraformerEngine, |
| 181 | + qwenRecognizer: qwenRecognizer |
| 182 | + ) |
| 183 | + logger.info("Dual Engine: 初始化完成") |
| 184 | + |
| 185 | + // 4. Load standalone ITN for post-processing |
| 186 | + if let itnFstPathForPostProcess = await loadITNFst() { |
| 187 | + pipeline.itn = SherpaOnnxITN(ruleFsts: itnFstPathForPostProcess) |
| 188 | + if pipeline.itn != nil { |
| 189 | + logger.info("Dual Engine: ITN 模型加载成功") |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return engine |
| 194 | + } |
| 195 | + |
| 196 | + // MARK: - Supporting Model Loaders |
| 197 | + |
| 198 | + private static func loadPunctuation(into pipeline: PostProcessingPipeline) async { |
| 199 | + if let punctPath = SherpaOnnxManager.shared.getPunctuationModelPath() { |
| 200 | + pipeline.punctuator = SherpaOnnxPunctuation(modelPath: punctPath) |
| 201 | + if pipeline.punctuator != nil { |
| 202 | + logger.info("标点模型加载成功") |
| 203 | + } |
| 204 | + } else { |
| 205 | + logger.info("标点模型未下载,正在下载...") |
| 206 | + await withCheckedContinuation { continuation in |
| 207 | + SherpaOnnxManager.shared.downloadPunctuationModel(progress: { progress in |
| 208 | + logger.debug("标点模型下载: \(progress, privacy: .public)") |
| 209 | + }, completion: { success, error in |
| 210 | + if success, let punctPath = SherpaOnnxManager.shared.getPunctuationModelPath() { |
| 211 | + pipeline.punctuator = SherpaOnnxPunctuation(modelPath: punctPath) |
| 212 | + logger.info("标点模型下载并加载成功") |
| 213 | + } else { |
| 214 | + logger.error("标点模型下载失败: \(error ?? "未知错误", privacy: .public)") |
| 215 | + } |
| 216 | + continuation.resume() |
| 217 | + }) |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + private static func loadCSC(into pipeline: PostProcessingPipeline) { |
| 223 | + if let paths = SherpaOnnxManager.shared.getCSCModelPath() { |
| 224 | + pipeline.corrector = ChineseSpellingCorrector(modelPath: paths.modelPath, vocabPath: paths.vocabPath) |
| 225 | + if pipeline.corrector != nil { |
| 226 | + logger.info("CSC 纠错模型加载成功") |
| 227 | + } else { |
| 228 | + logger.warning("CSC 纠错模型加载失败") |
| 229 | + } |
| 230 | + } else { |
| 231 | + logger.info("CSC 纠错模型未下载,跳过") |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + private static func loadITNFst() async -> String? { |
| 236 | + if let fstPath = SherpaOnnxManager.shared.getITNFstPath() { |
| 237 | + return fstPath |
| 238 | + } |
| 239 | + |
| 240 | + logger.info("ITN FST 模型未下载,正在下载...") |
| 241 | + return await withCheckedContinuation { continuation in |
| 242 | + SherpaOnnxManager.shared.downloadITNFst(progress: { progress in |
| 243 | + logger.debug("ITN FST 下载: \(progress, privacy: .public)") |
| 244 | + }, completion: { success, error in |
| 245 | + if success, let fstPath = SherpaOnnxManager.shared.getITNFstPath() { |
| 246 | + continuation.resume(returning: fstPath) |
| 247 | + } else { |
| 248 | + logger.error("ITN FST 下载失败: \(error ?? "未知错误", privacy: .public)") |
| 249 | + continuation.resume(returning: nil) |
| 250 | + } |
| 251 | + }) |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments