-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java API for speaker diarization (#1416)
- Loading branch information
1 parent
2d412b1
commit 1851ff6
Showing
14 changed files
with
471 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2024 Xiaomi Corporation | ||
|
||
// This file shows how to use sherpa-onnx Java API for speaker diarization, | ||
import com.k2fsa.sherpa.onnx.*; | ||
|
||
public class OfflineSpeakerDiarizationDemo { | ||
public static void main(String[] args) { | ||
/* Please use the following commands to download files used in this file | ||
Step 1: Download a speaker segmentation model | ||
Please visit https://github.com/k2-fsa/sherpa-onnx/releases/tag/speaker-segmentation-models | ||
for a list of available models. The following is an example | ||
wget 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 | ||
Step 2: Download a speaker embedding extractor model | ||
Please visit https://github.com/k2-fsa/sherpa-onnx/releases/tag/speaker-recongition-models | ||
for a list of available models. The following is an example | ||
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-recongition-models/3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx | ||
Step 3. Download test wave files | ||
Please visit https://github.com/k2-fsa/sherpa-onnx/releases/tag/speaker-segmentation-models | ||
for a list of available test wave files. The following is an example | ||
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-segmentation-models/0-four-speakers-zh.wav | ||
Step 4. Run it | ||
*/ | ||
|
||
String segmentationModel = "./sherpa-onnx-pyannote-segmentation-3-0/model.onnx"; | ||
String embeddingModel = "./3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx"; | ||
String waveFilename = "./0-four-speakers-zh.wav"; | ||
|
||
WaveReader reader = new WaveReader(waveFilename); | ||
|
||
OfflineSpeakerSegmentationPyannoteModelConfig pyannote = | ||
OfflineSpeakerSegmentationPyannoteModelConfig.builder().setModel(segmentationModel).build(); | ||
|
||
OfflineSpeakerSegmentationModelConfig segmentation = | ||
OfflineSpeakerSegmentationModelConfig.builder() | ||
.setPyannote(pyannote) | ||
.setDebug(true) | ||
.build(); | ||
|
||
SpeakerEmbeddingExtractorConfig embedding = | ||
SpeakerEmbeddingExtractorConfig.builder().setModel(embeddingModel).setDebug(true).build(); | ||
|
||
// The test wave file ./0-four-speakers-zh.wav contains four speakers, so | ||
// we use numClusters=4 here. If you don't know the number of speakers | ||
// in the test wave file, please set the numClusters to -1 and provide | ||
// threshold for clustering | ||
FastClusteringConfig clustering = | ||
FastClusteringConfig.builder() | ||
.setNumClusters(4) // set it to -1 if you don't know the actual number | ||
.setThreshold(0.5f) | ||
.build(); | ||
|
||
OfflineSpeakerDiarizationConfig config = | ||
OfflineSpeakerDiarizationConfig.builder() | ||
.setSegmentation(segmentation) | ||
.setEmbedding(embedding) | ||
.setClustering(clustering) | ||
.setMinDurationOn(0.2f) | ||
.setMinDurationOff(0.5f) | ||
.build(); | ||
|
||
OfflineSpeakerDiarization sd = new OfflineSpeakerDiarization(config); | ||
if (sd.getSampleRate() != reader.getSampleRate()) { | ||
System.out.printf( | ||
"Expected sample rate: %d, given: %d\n", sd.getSampleRate(), reader.getSampleRate()); | ||
return; | ||
} | ||
|
||
// OfflineSpeakerDiarizationSegment[] segments = sd.process(reader.getSamples()); | ||
// without callback is also ok | ||
|
||
// or you can use a callback to show the progress | ||
OfflineSpeakerDiarizationSegment[] segments = | ||
sd.processWithCallback( | ||
reader.getSamples(), | ||
(int numProcessedChunks, int numTotalChunks, long arg) -> { | ||
float progress = 100.0f * numProcessedChunks / numTotalChunks; | ||
System.out.printf("Progress: %.2f%%\n", progress); | ||
|
||
return 0; | ||
}); | ||
|
||
for (OfflineSpeakerDiarizationSegment s : segments) { | ||
System.out.printf("%.3f -- %.3f speaker_%02d\n", s.getStart(), s.getEnd(), s.getSpeaker()); | ||
} | ||
|
||
sd.release(); | ||
} | ||
} |
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,45 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -ex | ||
|
||
if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then | ||
mkdir -p ../build | ||
pushd ../build | ||
cmake \ | ||
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \ | ||
-DSHERPA_ONNX_ENABLE_TESTS=OFF \ | ||
-DSHERPA_ONNX_ENABLE_CHECK=OFF \ | ||
-DBUILD_SHARED_LIBS=ON \ | ||
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | ||
-DSHERPA_ONNX_ENABLE_JNI=ON \ | ||
.. | ||
|
||
make -j4 | ||
ls -lh lib | ||
popd | ||
fi | ||
|
||
if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then | ||
pushd ../sherpa-onnx/java-api | ||
make | ||
popd | ||
fi | ||
|
||
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 | ||
|
||
java \ | ||
-Djava.library.path=$PWD/../build/lib \ | ||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \ | ||
./OfflineSpeakerDiarizationDemo.java |
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
44 changes: 44 additions & 0 deletions
44
sherpa-onnx/java-api/src/com/k2fsa/sherpa/onnx/FastClusteringConfig.java
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,44 @@ | ||
// Copyright 2024 Xiaomi Corporation | ||
|
||
package com.k2fsa.sherpa.onnx; | ||
|
||
public class FastClusteringConfig { | ||
private final int numClusters; | ||
private final float threshold; | ||
|
||
private FastClusteringConfig(Builder builder) { | ||
this.numClusters = builder.numClusters; | ||
this.threshold = builder.threshold; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public int getNumClusters() { | ||
return numClusters; | ||
} | ||
|
||
public float getThreshold() { | ||
return threshold; | ||
} | ||
|
||
public static class Builder { | ||
private int numClusters = -1; | ||
private float threshold = 0.5f; | ||
|
||
public FastClusteringConfig build() { | ||
return new FastClusteringConfig(this); | ||
} | ||
|
||
public Builder setNumClusters(int numClusters) { | ||
this.numClusters = numClusters; | ||
return this; | ||
} | ||
|
||
public Builder setThreshold(float threshold) { | ||
this.threshold = threshold; | ||
return this; | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
sherpa-onnx/java-api/src/com/k2fsa/sherpa/onnx/OfflineSpeakerDiarization.java
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,61 @@ | ||
// Copyright 2024 Xiaomi Corporation | ||
|
||
package com.k2fsa.sherpa.onnx; | ||
|
||
public class OfflineSpeakerDiarization { | ||
static { | ||
System.loadLibrary("sherpa-onnx-jni"); | ||
} | ||
|
||
private long ptr = 0; | ||
|
||
public OfflineSpeakerDiarization(OfflineSpeakerDiarizationConfig config) { | ||
ptr = newFromFile(config); | ||
} | ||
|
||
public int getSampleRate() { | ||
return getSampleRate(ptr); | ||
} | ||
|
||
// Only config.clustering is used. All other fields are ignored | ||
public void setConfig(OfflineSpeakerDiarizationConfig config) { | ||
setConfig(ptr, config); | ||
} | ||
|
||
public OfflineSpeakerDiarizationSegment[] process(float[] samples) { | ||
return process(ptr, samples); | ||
} | ||
|
||
public OfflineSpeakerDiarizationSegment[] processWithCallback(float[] samples, OfflineSpeakerDiarizationCallback callback) { | ||
return processWithCallback(ptr, samples, callback, 0); | ||
} | ||
|
||
public OfflineSpeakerDiarizationSegment[] processWithCallback(float[] samples, OfflineSpeakerDiarizationCallback callback, long arg) { | ||
return processWithCallback(ptr, samples, callback, arg); | ||
} | ||
|
||
protected void finalize() throws Throwable { | ||
release(); | ||
} | ||
|
||
// You'd better call it manually if it is not used anymore | ||
public void release() { | ||
if (this.ptr == 0) { | ||
return; | ||
} | ||
delete(this.ptr); | ||
this.ptr = 0; | ||
} | ||
|
||
private native int getSampleRate(long ptr); | ||
|
||
private native void delete(long ptr); | ||
|
||
private native long newFromFile(OfflineSpeakerDiarizationConfig config); | ||
|
||
private native void setConfig(long ptr, OfflineSpeakerDiarizationConfig config); | ||
|
||
private native OfflineSpeakerDiarizationSegment[] process(long ptr, float[] samples); | ||
|
||
private native OfflineSpeakerDiarizationSegment[] processWithCallback(long ptr, float[] samples, OfflineSpeakerDiarizationCallback callback, long arg); | ||
} |
8 changes: 8 additions & 0 deletions
8
sherpa-onnx/java-api/src/com/k2fsa/sherpa/onnx/OfflineSpeakerDiarizationCallback.java
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,8 @@ | ||
// Copyright 2024 Xiaomi Corporation | ||
|
||
package com.k2fsa.sherpa.onnx; | ||
|
||
@FunctionalInterface | ||
public interface OfflineSpeakerDiarizationCallback { | ||
Integer invoke(int numProcessedChunks, int numTotalCunks, long arg); | ||
} |
79 changes: 79 additions & 0 deletions
79
sherpa-onnx/java-api/src/com/k2fsa/sherpa/onnx/OfflineSpeakerDiarizationConfig.java
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,79 @@ | ||
package com.k2fsa.sherpa.onnx; | ||
|
||
public class OfflineSpeakerDiarizationConfig { | ||
private final OfflineSpeakerSegmentationModelConfig segmentation; | ||
private final SpeakerEmbeddingExtractorConfig embedding; | ||
private final FastClusteringConfig clustering; | ||
private final float minDurationOn; | ||
private final float minDurationOff; | ||
|
||
private OfflineSpeakerDiarizationConfig(Builder builder) { | ||
this.segmentation = builder.segmentation; | ||
this.embedding = builder.embedding; | ||
this.clustering = builder.clustering; | ||
this.minDurationOff = builder.minDurationOff; | ||
this.minDurationOn = builder.minDurationOn; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public OfflineSpeakerSegmentationModelConfig getSegmentation() { | ||
return segmentation; | ||
} | ||
|
||
public SpeakerEmbeddingExtractorConfig getEmbedding() { | ||
return embedding; | ||
} | ||
|
||
public FastClusteringConfig getClustering() { | ||
return clustering; | ||
} | ||
|
||
public float getMinDurationOff() { | ||
return minDurationOff; | ||
} | ||
|
||
public float getMinDurationOn() { | ||
return minDurationOn; | ||
} | ||
|
||
public static class Builder { | ||
private OfflineSpeakerSegmentationModelConfig segmentation = OfflineSpeakerSegmentationModelConfig.builder().build(); | ||
private SpeakerEmbeddingExtractorConfig embedding = SpeakerEmbeddingExtractorConfig.builder().build(); | ||
private FastClusteringConfig clustering = FastClusteringConfig.builder().build(); | ||
private float minDurationOn = 0.2f; | ||
private float minDurationOff = 0.5f; | ||
|
||
public OfflineSpeakerDiarizationConfig build() { | ||
return new OfflineSpeakerDiarizationConfig(this); | ||
} | ||
|
||
public Builder setSegmentation(OfflineSpeakerSegmentationModelConfig segmentation) { | ||
this.segmentation = segmentation; | ||
return this; | ||
} | ||
|
||
public Builder setEmbedding(SpeakerEmbeddingExtractorConfig embedding) { | ||
this.embedding = embedding; | ||
return this; | ||
} | ||
|
||
public Builder setClustering(FastClusteringConfig clustering) { | ||
this.clustering = clustering; | ||
return this; | ||
} | ||
|
||
public Builder setMinDurationOff(float minDurationOff) { | ||
this.minDurationOff = minDurationOff; | ||
return this; | ||
} | ||
|
||
public Builder setMinDurationOn(float minDurationOn) { | ||
this.minDurationOn = minDurationOn; | ||
return this; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.