Swift API for keyword spotting. (#1027)
This commit is contained in:
@@ -832,3 +832,111 @@ class SherpaOnnxSpokenLanguageIdentificationWrapper {
|
||||
return SherpaOnnxSpokenLanguageIdentificationResultWrapper(result: result)
|
||||
}
|
||||
}
|
||||
|
||||
// keyword spotting
|
||||
|
||||
class SherpaOnnxKeywordResultWrapper {
|
||||
/// A pointer to the underlying counterpart in C
|
||||
let result: UnsafePointer<SherpaOnnxKeywordResult>!
|
||||
|
||||
var keyword: String {
|
||||
return String(cString: result.pointee.keyword)
|
||||
}
|
||||
|
||||
var count: Int32 {
|
||||
return result.pointee.count
|
||||
}
|
||||
|
||||
var tokens: [String] {
|
||||
if let tokensPointer = result.pointee.tokens_arr {
|
||||
var tokens: [String] = []
|
||||
for index in 0..<count {
|
||||
if let tokenPointer = tokensPointer[Int(index)] {
|
||||
let token = String(cString: tokenPointer)
|
||||
tokens.append(token)
|
||||
}
|
||||
}
|
||||
return tokens
|
||||
} else {
|
||||
let tokens: [String] = []
|
||||
return tokens
|
||||
}
|
||||
}
|
||||
|
||||
init(result: UnsafePointer<SherpaOnnxKeywordResult>!) {
|
||||
self.result = result
|
||||
}
|
||||
|
||||
deinit {
|
||||
if let result {
|
||||
DestroyKeywordResult(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sherpaOnnxKeywordSpotterConfig(
|
||||
featConfig: SherpaOnnxFeatureConfig,
|
||||
modelConfig: SherpaOnnxOnlineModelConfig,
|
||||
keywordsFile: String,
|
||||
maxActivePaths: Int = 4,
|
||||
numTrailingBlanks: Int = 1,
|
||||
keywordsScore: Float = 1.0,
|
||||
keywordsThreshold: Float = 0.25
|
||||
) -> SherpaOnnxKeywordSpotterConfig {
|
||||
return SherpaOnnxKeywordSpotterConfig(
|
||||
feat_config: featConfig,
|
||||
model_config: modelConfig,
|
||||
max_active_paths: Int32(maxActivePaths),
|
||||
num_trailing_blanks: Int32(numTrailingBlanks),
|
||||
keywords_score: keywordsScore,
|
||||
keywords_threshold: keywordsThreshold,
|
||||
keywords_file: toCPointer(keywordsFile)
|
||||
)
|
||||
}
|
||||
|
||||
class SherpaOnnxKeywordSpotterWrapper {
|
||||
/// A pointer to the underlying counterpart in C
|
||||
let spotter: OpaquePointer!
|
||||
var stream: OpaquePointer!
|
||||
|
||||
init(
|
||||
config: UnsafePointer<SherpaOnnxKeywordSpotterConfig>!
|
||||
) {
|
||||
spotter = CreateKeywordSpotter(config)
|
||||
stream = CreateKeywordStream(spotter)
|
||||
}
|
||||
|
||||
deinit {
|
||||
if let stream {
|
||||
DestroyOnlineStream(stream)
|
||||
}
|
||||
|
||||
if let spotter {
|
||||
DestroyKeywordSpotter(spotter)
|
||||
}
|
||||
}
|
||||
|
||||
func acceptWaveform(samples: [Float], sampleRate: Int = 16000) {
|
||||
AcceptWaveform(stream, Int32(sampleRate), samples, Int32(samples.count))
|
||||
}
|
||||
|
||||
func isReady() -> Bool {
|
||||
return IsKeywordStreamReady(spotter, stream) == 1 ? true : false
|
||||
}
|
||||
|
||||
func decode() {
|
||||
DecodeKeywordStream(spotter, stream)
|
||||
}
|
||||
|
||||
func getResult() -> SherpaOnnxKeywordResultWrapper {
|
||||
let result: UnsafePointer<SherpaOnnxKeywordResult>? = GetKeywordResult(
|
||||
spotter, stream)
|
||||
return SherpaOnnxKeywordResultWrapper(result: result)
|
||||
}
|
||||
|
||||
/// Signal that no more audio samples would be available.
|
||||
/// After this call, you cannot call acceptWaveform() any more.
|
||||
func inputFinished() {
|
||||
InputFinished(stream)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user