Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.0 ios #37

Merged
merged 9 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions binding/ios/Koala-iOS.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = 'Koala-iOS'
s.module_name = 'Koala'
s.version = '1.0.0'
s.version = '2.0.0'
s.license = {:type => 'Apache 2.0'}
s.summary = 'iOS SDK for Picovoice\'s Koala Noise Suppression Engine'
s.description =
Expand All @@ -18,7 +18,7 @@ Pod::Spec.new do |s|
DESC
s.homepage = 'https://github.com/Picovoice/koala/tree/main/binding/ios'
s.author = { 'Picovoice' => '[email protected]' }
s.source = { :git => "https://github.com/Picovoice/koala.git", :tag => "Koala-iOS-v1.0.0" }
s.source = { :git => "https://github.com/Picovoice/koala.git", :tag => "Koala-iOS-v2.0.0" }
s.ios.deployment_target = '11.0'
s.swift_version = '5.0'
s.vendored_frameworks = 'lib/ios/PvKoala.xcframework'
Expand Down
67 changes: 50 additions & 17 deletions binding/ios/Koala.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class Koala {
/// streams of audio data, this delay specifies the time shift between the input and output stream.
public var delaySample: UInt32 = 0

private static var sdk = "ios"
ksyeo1010 marked this conversation as resolved.
Show resolved Hide resolved

public static func setSdk(sdk: String) {
self.sdk = sdk
}

/// Constructor.
///
/// - Parameters:
Expand All @@ -70,18 +76,22 @@ public class Koala {
modelPathArg = try getResourcePath(modelPathArg!)
}

pv_set_sdk(Koala.sdk)

var status = pv_koala_init(
accessKey,
modelPathArg,
&self.handle)
if status != PV_STATUS_SUCCESS {
throw pvStatusToKoalaError(status, "Koala init failed")
let messageStack = try getMessageStack()
throw pvStatusToKoalaError(status, "Koala init failed", messageStack)
}

var cDelaySample: Int32 = 0
status = pv_koala_delay_sample(self.handle, &cDelaySample)
if status != PV_STATUS_SUCCESS {
throw pvStatusToKoalaError(status, "Failed to get Koala delay sample")
let messageStack = try getMessageStack()
throw pvStatusToKoalaError(status, "Failed to get Koala delay sample", messageStack)
}
self.delaySample = UInt32(cDelaySample)
}
Expand Down Expand Up @@ -141,7 +151,8 @@ public class Koala {
var enhancedPcm = [Int16](repeating: 0, count: Int(Koala.frameLength))
let status = pv_koala_process(self.handle, pcm, &enhancedPcm[0])
if status != PV_STATUS_SUCCESS {
throw pvStatusToKoalaError(status, "Koala process failed")
let messageStack = try getMessageStack()
throw pvStatusToKoalaError(status, "Koala process failed", messageStack)
}

return enhancedPcm
Expand All @@ -158,7 +169,8 @@ public class Koala {

let status = pv_koala_reset(self.handle)
if status != PV_STATUS_SUCCESS {
throw pvStatusToKoalaError(status, "Koala process failed")
let messageStack = try getMessageStack()
throw pvStatusToKoalaError(status, "Koala process failed", messageStack)
}
}

Expand All @@ -181,33 +193,54 @@ public class Koala {
""")
}

private func pvStatusToKoalaError(_ status: pv_status_t, _ message: String) -> KoalaError {
private func pvStatusToKoalaError(
_ status: pv_status_t,
_ message: String,
_ messageStack: [String] = []) -> KoalaError {
switch status {
case PV_STATUS_OUT_OF_MEMORY:
return KoalaMemoryError(message)
return KoalaMemoryError(message, messageStack)
case PV_STATUS_IO_ERROR:
return KoalaIOError(message)
return KoalaIOError(message, messageStack)
case PV_STATUS_INVALID_ARGUMENT:
return KoalaInvalidArgumentError(message)
return KoalaInvalidArgumentError(message, messageStack)
case PV_STATUS_STOP_ITERATION:
return KoalaStopIterationError(message)
return KoalaStopIterationError(message, messageStack)
case PV_STATUS_KEY_ERROR:
return KoalaKeyError(message)
return KoalaKeyError(message, messageStack)
case PV_STATUS_INVALID_STATE:
return KoalaInvalidStateError(message)
return KoalaInvalidStateError(message, messageStack)
case PV_STATUS_RUNTIME_ERROR:
return KoalaRuntimeError(message)
return KoalaRuntimeError(message, messageStack)
case PV_STATUS_ACTIVATION_ERROR:
return KoalaActivationError(message)
return KoalaActivationError(message, messageStack)
case PV_STATUS_ACTIVATION_LIMIT_REACHED:
return KoalaActivationLimitError(message)
return KoalaActivationLimitError(message, messageStack)
case PV_STATUS_ACTIVATION_THROTTLED:
return KoalaActivationThrottledError(message)
return KoalaActivationThrottledError(message, messageStack)
case PV_STATUS_ACTIVATION_REFUSED:
return KoalaActivationRefusedError(message)
return KoalaActivationRefusedError(message, messageStack)
default:
let pvStatusString = String(cString: pv_status_to_string(status))
return KoalaError("\(pvStatusString): \(message)")
return KoalaError("\(pvStatusString): \(message)", messageStack)
}
}

private func getMessageStack() throws -> [String] {
var messageStackRef: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>?
var messageStackDepth: Int32 = 0
let status = pv_get_error_stack(&messageStackRef, &messageStackDepth)
if status != PV_STATUS_SUCCESS {
throw pvStatusToKoalaError(status, "Unable to get Koala error state")
}

var messageStack: [String] = []
for i in 0..<messageStackDepth {
messageStack.append(String(cString: messageStackRef!.advanced(by: Int(i)).pointee!))
}

pv_free_error_stack(messageStackRef)

return messageStack
}
}
8 changes: 1 addition & 7 deletions binding/ios/KoalaAppTest/KoalaAppTest/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,4 @@

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
}

}
class ViewController: UIViewController { }
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ class KoalaDemoUITests: XCTestCase {

var koala: Koala?

override func setUp() {
super.setUp()
}

override func tearDown() {
if koala != nil {
koala!.delete()
Expand Down Expand Up @@ -141,4 +137,22 @@ class KoalaDemoUITests: XCTestCase {
func testVersion() throws {
XCTAssertGreaterThan(Koala.version, "")
}

func testMessageStack() throws {
var first_error: String = ""
do {
let koala = try Koala(accessKey: "invalid")
XCTAssertNil(koala)
} catch {
first_error = "\(error.localizedDescription)"
XCTAssert(first_error.count < 1024)
}

do {
let koala = try Koala(accessKey: "invalid")
XCTAssertNil(koala)
} catch {
XCTAssert("\(error.localizedDescription)".count == first_error.count)
}
}
}
6 changes: 3 additions & 3 deletions binding/ios/KoalaAppTest/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'

target 'KoalaAppTest' do
pod 'Koala-iOS', '~> 1.0.0'
pod 'Koala-iOS', :podspec => 'https://raw.githubusercontent.com/Picovoice/koala/v2.0-ios/binding/ios/Koala-iOS.podspec'
end

target 'KoalaAppTestUITests' do
pod 'Koala-iOS', '~> 1.0.0'
pod 'Koala-iOS', :podspec => 'https://raw.githubusercontent.com/Picovoice/koala/v2.0-ios/binding/ios/Koala-iOS.podspec'
end

target 'PerformanceTest' do
pod 'Koala-iOS', '~> 1.0.0'
pod 'Koala-iOS', :podspec => 'https://raw.githubusercontent.com/Picovoice/koala/v2.0-ios/binding/ios/Koala-iOS.podspec'
end
21 changes: 13 additions & 8 deletions binding/ios/KoalaAppTest/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
PODS:
- Koala-iOS (1.0.0)
- Koala-iOS (2.0.0)

DEPENDENCIES:
- Koala-iOS (~> 1.0.0)
- Koala-iOS (from `https://raw.githubusercontent.com/Picovoice/koala/v2.0-ios/binding/ios/Koala-iOS.podspec`)

SPEC REPOS:
trunk:
- Koala-iOS
EXTERNAL SOURCES:
Koala-iOS:
:podspec: https://raw.githubusercontent.com/Picovoice/koala/v2.0-ios/binding/ios/Koala-iOS.podspec

CHECKOUT OPTIONS:
Koala-iOS:
:commit: 82b588b19cb7225222853f7709f9e5de20415b47
:git: https://github.com/Picovoice/koala.git

SPEC CHECKSUMS:
Koala-iOS: 98ccd64b2531c57a0380dae47ea4105c4c5ae641
Koala-iOS: 865994d8e18b03854f2b933ec9645915ba95f189

PODFILE CHECKSUM: 1973a21901d505bae8a4a3f3777bd9df4b434bda
PODFILE CHECKSUM: aa02120478c1d3dd9e8085db3379479143edbc02

COCOAPODS: 1.11.2
COCOAPODS: 1.11.3
13 changes: 11 additions & 2 deletions binding/ios/KoalaErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@

public class KoalaError: LocalizedError {
private let message: String
private let messageStack: [String]

public init (_ message: String) {
public init (_ message: String, _ messageStack: [String] = []) {
self.message = message
self.messageStack = messageStack
}

public var errorDescription: String? {
return message
var messageString = message
if messageStack.count > 0 {
messageString += ":"
for i in 0..<messageStack.count {
messageString += "\n [\(i)] \(messageStack[i])"
}
}
return messageString
}

public var name: String {
Expand Down
2 changes: 1 addition & 1 deletion demo/ios/KoalaDemo/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'

target 'KoalaDemo' do
pod 'Koala-iOS', '~> 1.0.0'
pod 'Koala-iOS', :podspec => 'https://raw.githubusercontent.com/Picovoice/koala/v2.0-ios/binding/ios/Koala-iOS.podspec'
pod 'ios-voice-processor', '~> 1.1.0'
end
18 changes: 13 additions & 5 deletions demo/ios/KoalaDemo/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
PODS:
- ios-voice-processor (1.1.0)
- Koala-iOS (1.0.0)
- Koala-iOS (2.0.0)

DEPENDENCIES:
- ios-voice-processor (~> 1.1.0)
- Koala-iOS (~> 1.0.0)
- Koala-iOS (from `https://raw.githubusercontent.com/Picovoice/koala/v2.0-ios/binding/ios/Koala-iOS.podspec`)

SPEC REPOS:
trunk:
- ios-voice-processor
- Koala-iOS

EXTERNAL SOURCES:
Koala-iOS:
:podspec: https://raw.githubusercontent.com/Picovoice/koala/v2.0-ios/binding/ios/Koala-iOS.podspec

CHECKOUT OPTIONS:
Koala-iOS:
:commit: 9ea3024bbf5a91ad3ec558d23c64c936bcebf623
:git: https://github.com/Picovoice/koala.git

SPEC CHECKSUMS:
ios-voice-processor: 8e32d7f980a06d392d128ef1cd19cf6ddcaca3c1
Koala-iOS: 98ccd64b2531c57a0380dae47ea4105c4c5ae641
Koala-iOS: 865994d8e18b03854f2b933ec9645915ba95f189

PODFILE CHECKSUM: 0e642f89b04c02a356a72938e8737b9f102378c0
PODFILE CHECKSUM: 0f89a64b9647d42bf16b4915aa88de8d933489b8

COCOAPODS: 1.11.3
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ PV_API pv_status_t pv_get_error_stack(
*/
PV_API void pv_free_error_stack(char **message_stack);

PV_API void pv_set_sdk(const char *sdk);

#ifdef __cplusplus
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ PV_API pv_status_t pv_get_error_stack(
*/
PV_API void pv_free_error_stack(char **message_stack);

PV_API void pv_set_sdk(const char *sdk);

#ifdef __cplusplus
}

Expand Down