-
Notifications
You must be signed in to change notification settings - Fork 537
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 speaker diarization (#1404)
- Loading branch information
1 parent
df681e9
commit 1571344
Showing
4 changed files
with
209 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
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,35 @@ | ||
#!/usr/bin/env bash | ||
|
||
if [ ! -f ./sherpa-onnx-pyannote-segmentation-3-0/model.onnx ]; then | ||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-segmentation-models/sherpa-onnx-pyannote-segmentation-3-0.tar.bz2 | ||
tar xvf sherpa-onnx-pyannote-segmentation-3-0.tar.bz2 | ||
rm sherpa-onnx-pyannote-segmentation-3-0.tar.bz2 | ||
fi | ||
|
||
if [ ! -f ./3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx ]; then | ||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-recongition-models/3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx | ||
fi | ||
|
||
if [ ! -f ./0-four-speakers-zh.wav ]; then | ||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-segmentation-models/0-four-speakers-zh.wav | ||
fi | ||
|
||
if [ ! -e ./speaker-diarization ]; 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 \ | ||
./speaker-diarization.swift ./SherpaOnnx.swift \ | ||
-L ../build-swift-macos/install/lib/ \ | ||
-l sherpa-onnx \ | ||
-l onnxruntime \ | ||
-o speaker-diarization | ||
|
||
strip speaker-diarization | ||
else | ||
echo "./speaker-diarization exists - skip building" | ||
fi | ||
|
||
export DYLD_LIBRARY_PATH=$PWD/../build-swift-macos/install/lib:$DYLD_LIBRARY_PATH | ||
./speaker-diarization |
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,56 @@ | ||
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 segmentationModel = "./sherpa-onnx-pyannote-segmentation-3-0/model.onnx" | ||
let embeddingExtractorModel = "./3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx" | ||
let waveFilename = "./0-four-speakers-zh.wav" | ||
|
||
// There are 4 speakers in ./0-four-speakers-zh.wav, so we use 4 here | ||
let numSpeakers = 4 | ||
var config = sherpaOnnxOfflineSpeakerDiarizationConfig( | ||
segmentation: sherpaOnnxOfflineSpeakerSegmentationModelConfig( | ||
pyannote: sherpaOnnxOfflineSpeakerSegmentationPyannoteModelConfig(model: segmentationModel)), | ||
embedding: sherpaOnnxSpeakerEmbeddingExtractorConfig(model: embeddingExtractorModel), | ||
clustering: sherpaOnnxFastClusteringConfig(numClusters: numSpeakers) | ||
) | ||
|
||
let sd = SherpaOnnxOfflineSpeakerDiarizationWrapper(config: &config) | ||
|
||
let fileURL: NSURL = NSURL(fileURLWithPath: waveFilename) | ||
let audioFile = try! AVAudioFile(forReading: fileURL as URL) | ||
|
||
let audioFormat = audioFile.processingFormat | ||
assert(Int(audioFormat.sampleRate) == sd.sampleRate) | ||
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() | ||
print("Started!") | ||
let segments = sd.process(samples: array) | ||
for i in 0..<segments.count { | ||
print("\(segments[i].start) -- \(segments[i].end) speaker_\(segments[i].speaker)") | ||
} | ||
} | ||
|
||
@main | ||
struct App { | ||
static func main() { | ||
run() | ||
} | ||
} |