From 9d5890c69ea0f0bdbc00ccd02d11bdeee1bd9e99 Mon Sep 17 00:00:00 2001 From: Albert Ho Date: Mon, 2 Dec 2024 12:15:27 -0800 Subject: [PATCH] refactor resources bundle --- binding/ios/Falcon-iOS.podspec | 6 ++++- binding/ios/Falcon.swift | 49 ++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/binding/ios/Falcon-iOS.podspec b/binding/ios/Falcon-iOS.podspec index d3dd228..e99548e 100644 --- a/binding/ios/Falcon-iOS.podspec +++ b/binding/ios/Falcon-iOS.podspec @@ -16,7 +16,11 @@ Pod::Spec.new do |s| s.ios.deployment_target = '13.0' s.swift_version = '5.0' s.vendored_frameworks = 'lib/ios/PvFalcon.xcframework' - s.resources = 'lib/common/falcon_params.pv' + s.resource_bundles = { + 'FalconResources' => [ + 'lib/common/falcon_params.pv' + ] + } s.source_files = 'binding/ios/*.{swift}' s.exclude_files = 'binding/ios/FalconTestApp/**' end diff --git a/binding/ios/Falcon.swift b/binding/ios/Falcon.swift index 8fe4084..ac8907b 100644 --- a/binding/ios/Falcon.swift +++ b/binding/ios/Falcon.swift @@ -39,6 +39,32 @@ public struct FalconSegment { /// iOS binding for Falcon speaker diarization engine. Provides a Swift interface to the Falcon library. public class Falcon { + +#if SWIFT_PACKAGE + + static let resourceBundle = Bundle.module + +#else + + static let resourceBundle: Bundle = { + let myBundle = Bundle(for: Falcon.self) + + guard let resourceBundleURL = myBundle.url( + forResource: "FalconResources", withExtension: "bundle") + else { + fatalError("FalconResources.bundle not found") + } + + guard let resourceBundle = Bundle(url: resourceBundleURL) + else { + fatalError("Could not open FalconResources.bundle") + } + + return resourceBundle + }() + +#endif + private static let supportedAudioTypes: Set = [ "3gp", "flac", @@ -75,28 +101,11 @@ public class Falcon { } var modelPathArg = modelPath - - if modelPath == nil { - -#if SWIFT_PACKAGE - - if let bundleURL = Bundle.module.url(forResource: "falcon_params", withExtension: "pv") { - modelPathArg = bundleURL.path - } else { - throw FalconIOError("Could not retrieve default model from the package bundle") - } - -#else - - let bundle = Bundle(for: type(of: self)) - - modelPathArg = bundle.path(forResource: "falcon_params", ofType: "pv") + if modelPathArg == nil { + modelPathArg = Falcon.resourceBundle.path(forResource: "falcon_params", ofType: "pv") if modelPathArg == nil { - throw FalconIOError("Could not retrieve default model from app bundle") + throw FalconIOError("Could not find default model file in app bundle.") } - -#endif - } if !FileManager().fileExists(atPath: modelPathArg!) {