-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swift API for keyword spotting. (#1027)
- Loading branch information
1 parent
0be7f28
commit ab21131
Showing
5 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ sherpa-onnx-paraformer-zh-2023-09-14 | |
!*.sh | ||
*.bak | ||
streaming-hlg-decode-file | ||
keyword-spotting-from-file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import AVFoundation | ||
|
||
extension AudioBuffer { | ||
func array() -> [Float] { | ||
return Array(UnsafeBufferPointer(self)) | ||
} | ||
} | ||
|
||
extension AVAudioPCMBuffer { | ||
func array() -> [Float] { | ||
return self.audioBufferList.pointee.mBuffers.array() | ||
} | ||
} | ||
|
||
func run() { | ||
let filePath = "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/3.wav" | ||
let encoder = | ||
"./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/encoder-epoch-12-avg-2-chunk-16-left-64.onnx" | ||
let decoder = | ||
"./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/decoder-epoch-12-avg-2-chunk-16-left-64.onnx" | ||
let joiner = | ||
"./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/joiner-epoch-12-avg-2-chunk-16-left-64.onnx" | ||
let tokens = | ||
"./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/tokens.txt" | ||
let keywordsFile = | ||
"./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/test_keywords.txt" | ||
let transducerConfig = sherpaOnnxOnlineTransducerModelConfig( | ||
encoder: encoder, | ||
decoder: decoder, | ||
joiner: joiner | ||
) | ||
|
||
let modelConfig = sherpaOnnxOnlineModelConfig( | ||
tokens: tokens, | ||
transducer: transducerConfig | ||
) | ||
|
||
let featConfig = sherpaOnnxFeatureConfig( | ||
sampleRate: 16000, | ||
featureDim: 80 | ||
) | ||
var config = sherpaOnnxKeywordSpotterConfig( | ||
featConfig: featConfig, | ||
modelConfig: modelConfig, | ||
keywordsFile: keywordsFile | ||
) | ||
|
||
let spotter = SherpaOnnxKeywordSpotterWrapper(config: &config) | ||
|
||
let fileURL: NSURL = NSURL(fileURLWithPath: filePath) | ||
let audioFile = try! AVAudioFile(forReading: fileURL as URL) | ||
|
||
let audioFormat = audioFile.processingFormat | ||
assert(audioFormat.sampleRate == 16000) | ||
assert(audioFormat.channelCount == 1) | ||
assert(audioFormat.commonFormat == AVAudioCommonFormat.pcmFormatFloat32) | ||
|
||
let audioFrameCount = UInt32(audioFile.length) | ||
let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount) | ||
|
||
try! audioFile.read(into: audioFileBuffer!) | ||
let array: [Float]! = audioFileBuffer?.array() | ||
spotter.acceptWaveform(samples: array) | ||
|
||
let tailPadding = [Float](repeating: 0.0, count: 3200) | ||
spotter.acceptWaveform(samples: tailPadding) | ||
|
||
spotter.inputFinished() | ||
while spotter.isReady() { | ||
spotter.decode() | ||
let keyword = spotter.getResult().keyword | ||
if keyword != "" { | ||
print("Detected: \(keyword)") | ||
} | ||
} | ||
} | ||
|
||
@main | ||
struct App { | ||
static func main() { | ||
run() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -ex | ||
|
||
if [ ! -d ../build-swift-macos ]; then | ||
echo "Please run ../build-swift-macos.sh first!" | ||
exit 1 | ||
fi | ||
|
||
if [ ! -d ./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01 ]; then | ||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/kws-models/sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01.tar.bz2 | ||
tar xf sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01.tar.bz2 | ||
rm sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01.tar.bz2 | ||
fi | ||
|
||
if [ ! -e ./keyword-spotting-from-file ]; then | ||
# Note: We use -lc++ to link against libc++ instead of libstdc++ | ||
swiftc \ | ||
-lc++ \ | ||
-I ../build-swift-macos/install/include \ | ||
-import-objc-header ./SherpaOnnx-Bridging-Header.h \ | ||
./keyword-spotting-from-file.swift ./SherpaOnnx.swift \ | ||
-L ../build-swift-macos/install/lib/ \ | ||
-l sherpa-onnx \ | ||
-l onnxruntime \ | ||
-o keyword-spotting-from-file | ||
|
||
strip keyword-spotting-from-file | ||
else | ||
echo "./keyword-spotting-from-file exists - skip building" | ||
fi | ||
|
||
export DYLD_LIBRARY_PATH=$PWD/../build-swift-macos/install/lib:$DYLD_LIBRARY_PATH | ||
./keyword-spotting-from-file |