Skip to content

Commit

Permalink
use resource_bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
albho committed Dec 2, 2024
1 parent f30da2a commit 2398055
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
6 changes: 5 additions & 1 deletion binding/ios/Eagle-iOS.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ Pod::Spec.new do |s|
s.ios.deployment_target = '13.0'
s.swift_version = '5.0'
s.vendored_frameworks = 'lib/ios/PvEagle.xcframework'
s.resources = 'lib/common/eagle_params.pv'
s.resource_bundles = {
'EagleResources' => [
'lib/common/eagle_params.pv'
]
}
s.source_files = 'binding/ios/*.{swift}'
s.exclude_files = 'binding/ios/EagleTestApp/**'
end
22 changes: 3 additions & 19 deletions binding/ios/Eagle.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2023 Picovoice Inc.
// Copyright 2023-2024 Picovoice Inc.
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
Expand Down Expand Up @@ -37,26 +37,10 @@ public class Eagle: EagleBase {
var modelPathArg = modelPath

if modelPath == nil {

#if SWIFT_PACKAGE

if let bundleURL = Bundle.module.url(forResource: "eagle_params", withExtension: "pv") {
modelPathArg = bundleURL.path
} else {
throw EagleIOError("Could not retrieve default model from the package bundle")
}

#else

let bundle = Bundle(for: type(of: self))

modelPathArg = bundle.path(forResource: "eagle_params", ofType: "pv")
modelPathArg = Eagle.resourceBundle.path(forResource: "eagle_params", ofType: "pv")
if modelPathArg == nil {
throw EagleIOError("Could not retrieve default model from app bundle")
throw EagleIOError("Could not find default model file in app bundle.")
}

#endif

}

if !FileManager().fileExists(atPath: modelPathArg!) {
Expand Down
26 changes: 26 additions & 0 deletions binding/ios/EagleBase.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import Foundation
import PvEagle

/// Base class providing shared utilities and functions for Eagle and EagleProfiler
public class EagleBase {

#if SWIFT_PACKAGE

static let resourceBundle = Bundle.module

#else

static let resourceBundle: Bundle = {
let myBundle = Bundle(for: Eagle.self)

guard let resourceBundleURL = myBundle.url(
forResource: "EagleResources", withExtension: "bundle")
else {
fatalError("EagleResources.bundle not found")
}

guard let resourceBundle = Bundle(url: resourceBundleURL)
else {
fatalError("Could not open EagleResources.bundle")
}

return resourceBundle
}()

#endif

/// Required audio sample rate for PCM data
public static let sampleRate = Int(pv_sample_rate())

Expand Down
22 changes: 3 additions & 19 deletions binding/ios/EagleProfiler.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2023 Picovoice Inc.
// Copyright 2023-2024 Picovoice Inc.
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
Expand Down Expand Up @@ -39,26 +39,10 @@ public class EagleProfiler: EagleBase {
var modelPathArg = modelPath

if modelPath == nil {

#if SWIFT_PACKAGE

if let bundleURL = Bundle.module.url(forResource: "eagle_params", withExtension: "pv") {
modelPathArg = bundleURL.path
} else {
throw EagleIOError("Could not retrieve default model from the package bundle")
}

#else

let bundle = Bundle(for: type(of: self))

modelPathArg = bundle.path(forResource: "eagle_params", ofType: "pv")
modelPathArg = EagleProfiler.resourceBundle.path(forResource: "eagle_params", ofType: "pv")
if modelPathArg == nil {
throw EagleIOError("Could not retrieve default model from app bundle")
throw EagleIOError("Could not find default model file in app bundle.")
}

#endif

}

if !FileManager().fileExists(atPath: modelPathArg!) {
Expand Down

0 comments on commit 2398055

Please sign in to comment.