test_offline_speaker_diarization.kt
1.8 KB
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
package com.k2fsa.sherpa.onnx
fun main() {
testOfflineSpeakerDiarization()
}
fun callback(numProcessedChunks: Int, numTotalChunks: Int, arg: Long): Int {
val progress = numProcessedChunks.toFloat() / numTotalChunks * 100
val s = "%.2f".format(progress)
println("Progress: ${s}%");
return 0
}
fun testOfflineSpeakerDiarization() {
var config = OfflineSpeakerDiarizationConfig(
segmentation=OfflineSpeakerSegmentationModelConfig(
pyannote=OfflineSpeakerSegmentationPyannoteModelConfig("./sherpa-onnx-pyannote-segmentation-3-0/model.onnx"),
),
embedding=SpeakerEmbeddingExtractorConfig(
model="./3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx",
),
// 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 threshold like below.
//
// clustering=FastClusteringConfig(threshold=0.5),
//
// WARNING: You need to tune threshold by yourself.
// A larger threshold leads to fewer clusters, i.e., few speakers.
// A smaller threshold leads to more clusters, i.e., more speakers.
//
clustering=FastClusteringConfig(numClusters=4),
)
val sd = OfflineSpeakerDiarization(config=config)
val waveData = WaveReader.readWave(
filename = "./0-four-speakers-zh.wav",
)
if (sd.sampleRate() != waveData.sampleRate) {
println("Expected sample rate: ${sd.sampleRate()}, given: ${waveData.sampleRate}")
return
}
// val segments = sd.process(waveData.samples) // this one is also ok
val segments = sd.processWithCallback(waveData.samples, callback=::callback)
for (segment in segments) {
println("${segment.start} -- ${segment.end} speaker_${segment.speaker}")
}
}