-
Notifications
You must be signed in to change notification settings - Fork 502
/
Porcupine.swift
308 lines (266 loc) · 11.8 KB
/
Porcupine.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
// Copyright 2021-2023 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
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//
import Foundation
import PvPorcupine
/// Low-level iOS binding for Porcupine wake word engine. Provides a Swift interface to the Porcupine library.
public class Porcupine {
#if SWIFT_PACKAGE
static let resourceBundle = Bundle.module
#else
static let resourceBundle: Bundle = {
let myBundle = Bundle(for: Porcupine.self)
guard let resourceBundleURL = myBundle.url(
forResource: "PorcupineResources", withExtension: "bundle")
else { fatalError("PorcupineResources.bundle not found") }
guard let resourceBundle = Bundle(url: resourceBundleURL)
else { fatalError("Could not open PorcupineResources.bundle") }
return resourceBundle
}()
#endif
public enum BuiltInKeyword: String, CaseIterable {
case alexa = "Alexa"
case americano = "Americano"
case blueberry = "Blueberry"
case bumblebee = "Bumblebee"
case computer = "Computer"
case grapefruit = "Grapefruit"
case grasshopper = "Grasshopper"
case heyGoogle = "Hey Google"
case heySiri = "Hey Siri"
case jarvis = "Jarvis"
case okGoogle = "Ok Google"
case picovoice = "Picovoice"
case porcupine = "Porcupine"
case terminator = "Terminator"
}
private var handle: OpaquePointer?
public static let frameLength = UInt32(pv_porcupine_frame_length())
public static let sampleRate = UInt32(pv_sample_rate())
public static let version = String(cString: pv_porcupine_version())
private static var sdk = "ios"
public static func setSdk(sdk: String) {
self.sdk = sdk
}
/// Constructor.
///
/// - Parameters:
/// - accessKey: The AccessKey obtained from Picovoice Console (https://console.picovoice.ai).
/// - keywordPaths: Absolute paths to keyword model files.
/// - modelPath: Absolute path to file containing model parameters.
/// - sensitivities: Sensitivities for detecting keywords. Each value should be a number within [0, 1].
/// A higher sensitivity results in fewer misses at the cost of increasing the false alarm rate.
/// - Throws: PorcupineError
public init(
accessKey: String,
keywordPaths: [String],
modelPath: String? = nil,
sensitivities: [Float32]? = nil
) throws {
var modelPathArg = modelPath
if modelPath == nil {
modelPathArg = Porcupine.resourceBundle.path(forResource: "porcupine_params", ofType: "pv")
if modelPathArg == nil {
throw PorcupineIOError("Unable to find the default model path")
}
}
if accessKey.count == 0 {
throw PorcupineInvalidArgumentError("AccessKey is required for Porcupine initialization")
}
let sensitivitiesArg = sensitivities ?? Array(repeating: 0.5, count: keywordPaths.count)
if sensitivitiesArg.count != keywordPaths.count {
throw PorcupineInvalidArgumentError("Number of sensitivity values (\(sensitivitiesArg.count)) " +
"does not match number of keywords (\(keywordPaths.count))")
}
if !sensitivitiesArg.allSatisfy({$0 >= 0 && $0 <= 1}) {
throw PorcupineInvalidArgumentError(
"One or more sensitivities provided were not floating-point values between [0,1]")
}
if !FileManager().fileExists(atPath: modelPathArg!) {
modelPathArg = try getResourcePath(modelPathArg!)
}
var keywordPathsArgs = keywordPaths
for i in 0..<keywordPathsArgs.count where !FileManager().fileExists(atPath: keywordPathsArgs[i]) {
keywordPathsArgs[i] = try getResourcePath(keywordPathsArgs[i])
}
pv_set_sdk(Porcupine.sdk)
let status = pv_porcupine_init(
accessKey,
modelPathArg,
Int32(keywordPathsArgs.count),
keywordPathsArgs.map { UnsafePointer(strdup($0)) },
sensitivitiesArg,
&handle)
if status != PV_STATUS_SUCCESS {
let messageStack = try getMessageStack()
throw pvStatusToPorcupineError(status, "Porcupine init failed", messageStack)
}
}
/// Constructor.
///
/// - Parameters:
/// - accessKey: The AccessKey obtained from Picovoice Console (https://console.picovoice.ai).
/// - keywordPath: Absolute paths to a keyword model file.
/// - modelPath: Absolute path to file containing model parameters.
/// - sensitivity: Sensitivity for detecting keywords. Each value should be a number within [0, 1].
/// A higher sensitivity results in fewer misses at the cost of increasing the false alarm rate.
/// - Throws: PorcupineError
public convenience init(
accessKey: String,
keywordPath: String,
modelPath: String? = nil,
sensitivity: Float32 = 0.5
) throws {
try self.init(
accessKey: accessKey,
keywordPaths: [keywordPath],
modelPath: modelPath,
sensitivities: [sensitivity])
}
/// Constructor.
///
/// - Parameters:
/// - accessKey: The AccessKey obtained from Picovoice Console (https://console.picovoice.ai).
/// - keywords: An array of built-in keywords from the Porcupine.BuiltInKeyword enum.
/// - modelPath: Absolute path to file containing model parameters.
/// - sensitivities: Sensitivities for detecting keywords. Each value should be a number within [0, 1].
/// A higher sensitivity results in fewer misses at the cost of increasing the false alarm rate.
/// - Throws: PorcupineError
public convenience init(
accessKey: String,
keywords: [Porcupine.BuiltInKeyword],
modelPath: String? = nil,
sensitivities: [Float32]? = nil
) throws {
var keywordPaths = [String]()
for k in keywords {
let keywordPath = Porcupine.resourceBundle.path(
forResource: k.rawValue.lowercased() + "_ios",
ofType: "ppn")
if keywordPath == nil {
throw PorcupineIOError("Unable to open the default keyword file for keyword '\(k)'")
}
keywordPaths.append(keywordPath!)
}
try self.init(
accessKey: accessKey,
keywordPaths: keywordPaths,
modelPath: modelPath,
sensitivities: sensitivities)
}
/// Constructor.
///
/// - Parameters:
/// - accessKey: The AccessKey obtained from Picovoice Console (https://console.picovoice.ai).
/// - keyword: A built-in keyword from the Porcupine.BuiltInKeyword enum.
/// - modelPath: Absolute path to file containing model parameters.
/// - sensitivity: Sensitivity for detecting keywords. Each value should be a number within [0, 1].
/// A higher sensitivity results in fewer misses at the cost of increasing the false alarm rate.
/// - Throws: PorcupineError
public convenience init(
accessKey: String,
keyword: Porcupine.BuiltInKeyword,
modelPath: String? = nil,
sensitivity: Float32 = 0.5
) throws {
try self.init(accessKey: accessKey, keywords: [keyword], modelPath: modelPath, sensitivities: [sensitivity])
}
deinit {
self.delete()
}
/// Releases native resources that were allocated to Porcupine
public func delete() {
if handle != nil {
pv_porcupine_delete(handle)
handle = nil
}
}
/// Process a frame of audio with the wake word engine
///
/// - Parameters:
/// - pcm: An array of 16-bit pcm samples
/// - Throws: PorcupineError
/// - Returns:Index of keyword detected or -1 if no keyword was detected
public func process(pcm: [Int16]) throws -> Int32 {
if handle == nil {
throw PorcupineInvalidStateError("Porcupine must be initialized before processing")
}
if pcm.count != Porcupine.frameLength {
throw PorcupineInvalidArgumentError("Frame of audio data must contain \(Porcupine.frameLength) " +
"samples - given frame contained \(pcm.count)")
}
var result: Int32 = -1
let status = pv_porcupine_process(self.handle, pcm, &result)
if status != PV_STATUS_SUCCESS {
let messageStack = try getMessageStack()
throw pvStatusToPorcupineError(status, "Porcupine process failed", messageStack)
}
return result
}
/// Given a path, return the full path to the resource.
///
/// - Parameters:
/// - filePath: relative path of a file in the bundle.
/// - Throws: PorcupineIOError
/// - Returns: The full path of the resource.
private func getResourcePath(_ filePath: String) throws -> String {
if let resourcePath = Bundle(for: type(of: self)).resourceURL?.appendingPathComponent(filePath).path {
if FileManager.default.fileExists(atPath: resourcePath) {
return resourcePath
}
}
throw PorcupineIOError("Could not find file at path '\(filePath)'. " +
"If this is a packaged asset, ensure you have added it to your xcode project.")
}
private func pvStatusToPorcupineError(
_ status: pv_status_t,
_ message: String,
_ messageStack: [String] = []) -> PorcupineError {
switch status {
case PV_STATUS_OUT_OF_MEMORY:
return PorcupineMemoryError(message, messageStack)
case PV_STATUS_IO_ERROR:
return PorcupineIOError(message, messageStack)
case PV_STATUS_INVALID_ARGUMENT:
return PorcupineInvalidArgumentError(message, messageStack)
case PV_STATUS_STOP_ITERATION:
return PorcupineStopIterationError(message, messageStack)
case PV_STATUS_KEY_ERROR:
return PorcupineKeyError(message, messageStack)
case PV_STATUS_INVALID_STATE:
return PorcupineInvalidStateError(message, messageStack)
case PV_STATUS_RUNTIME_ERROR:
return PorcupineRuntimeError(message, messageStack)
case PV_STATUS_ACTIVATION_ERROR:
return PorcupineActivationError(message, messageStack)
case PV_STATUS_ACTIVATION_LIMIT_REACHED:
return PorcupineActivationLimitError(message, messageStack)
case PV_STATUS_ACTIVATION_THROTTLED:
return PorcupineActivationThrottledError(message, messageStack)
case PV_STATUS_ACTIVATION_REFUSED:
return PorcupineActivationRefusedError(message, messageStack)
default:
let pvStatusString = String(cString: pv_status_to_string(status))
return PorcupineError("\(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 pvStatusToPorcupineError(status, "Unable to get Porcupine 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
}
}