|
| 1 | +// Copyright (c) 2024 Xiaomi Corporation |
| 2 | +import 'dart:io'; |
| 3 | +import 'dart:typed_data'; |
| 4 | +import 'dart:ffi'; |
| 5 | + |
| 6 | +import 'package:sherpa_onnx/sherpa_onnx.dart' as sherpa_onnx; |
| 7 | +import './init.dart'; |
| 8 | + |
| 9 | +void main(List<String> arguments) async { |
| 10 | + await initSherpaOnnx(); |
| 11 | + |
| 12 | + /* Please use the following commands to download files used in this file |
| 13 | + Step 1: Download a speaker segmentation model |
| 14 | +
|
| 15 | + Please visit https://github.com/k2-fsa/sherpa-onnx/releases/tag/speaker-segmentation-models |
| 16 | + for a list of available models. The following is an example |
| 17 | +
|
| 18 | + wget https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-segmentation-models/sherpa-onnx-pyannote-segmentation-3-0.tar.bz2 |
| 19 | + tar xvf sherpa-onnx-pyannote-segmentation-3-0.tar.bz2 |
| 20 | + rm sherpa-onnx-pyannote-segmentation-3-0.tar.bz2 |
| 21 | +
|
| 22 | + Step 2: Download a speaker embedding extractor model |
| 23 | +
|
| 24 | + Please visit https://github.com/k2-fsa/sherpa-onnx/releases/tag/speaker-recongition-models |
| 25 | + for a list of available models. The following is an example |
| 26 | +
|
| 27 | + wget https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-recongition-models/3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx |
| 28 | +
|
| 29 | + Step 3. Download test wave files |
| 30 | +
|
| 31 | + Please visit https://github.com/k2-fsa/sherpa-onnx/releases/tag/speaker-segmentation-models |
| 32 | + for a list of available test wave files. The following is an example |
| 33 | +
|
| 34 | + wget https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-segmentation-models/0-four-speakers-zh.wav |
| 35 | +
|
| 36 | + Step 4. Run it |
| 37 | + */ |
| 38 | + |
| 39 | + final segmentationModel = |
| 40 | + "./sherpa-onnx-pyannote-segmentation-3-0/model.onnx"; |
| 41 | + |
| 42 | + final embeddingModel = |
| 43 | + "./3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx"; |
| 44 | + |
| 45 | + final waveFilename = "./0-four-speakers-zh.wav"; |
| 46 | + |
| 47 | + final segmentationConfig = sherpa_onnx.OfflineSpeakerSegmentationModelConfig( |
| 48 | + pyannote: sherpa_onnx.OfflineSpeakerSegmentationPyannoteModelConfig( |
| 49 | + model: segmentationModel), |
| 50 | + ); |
| 51 | + |
| 52 | + final embeddingConfig = |
| 53 | + sherpa_onnx.SpeakerEmbeddingExtractorConfig(model: embeddingModel); |
| 54 | + |
| 55 | + // since we know there are 4 speakers in ./0-four-speakers-zh.wav, we set |
| 56 | + // numClusters to 4. If you don't know the exact number, please set it to -1. |
| 57 | + // in that case, you have to set threshold. A larger threshold leads to |
| 58 | + // fewer clusters, i.e., fewer speakers. |
| 59 | + final clusteringConfig = |
| 60 | + sherpa_onnx.FastClusteringConfig(numClusters: 4, threshold: 0.5); |
| 61 | + |
| 62 | + var config = sherpa_onnx.OfflineSpeakerDiarizationConfig( |
| 63 | + segmentation: segmentationConfig, |
| 64 | + embedding: embeddingConfig, |
| 65 | + clustering: clusteringConfig, |
| 66 | + minDurationOn: 0.2, |
| 67 | + minDurationOff: 0.5); |
| 68 | + |
| 69 | + final sd = sherpa_onnx.OfflineSpeakerDiarization(config); |
| 70 | + if (sd.ptr == nullptr) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + final waveData = sherpa_onnx.readWave(waveFilename); |
| 75 | + if (sd.sampleRate != waveData.sampleRate) { |
| 76 | + print( |
| 77 | + 'Expected sample rate: ${sd.sampleRate}, given: ${waveData.sampleRate}'); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + print('started'); |
| 82 | + |
| 83 | + // Use the following statement if you don't want to use a callback |
| 84 | + // final segments = sd.process(samples: waveData.samples); |
| 85 | + |
| 86 | + final segments = sd.processWithCallback( |
| 87 | + samples: waveData.samples, |
| 88 | + callback: (int numProcessedChunk, int numTotalChunks) { |
| 89 | + final progress = 100.0 * numProcessedChunk / numTotalChunks; |
| 90 | + |
| 91 | + print('Progress ${progress.toStringAsFixed(2)}%'); |
| 92 | + |
| 93 | + return 0; |
| 94 | + }); |
| 95 | + |
| 96 | + for (int i = 0; i < segments.length; ++i) { |
| 97 | + print( |
| 98 | + '${segments[i].start.toStringAsFixed(3)} -- ${segments[i].end.toStringAsFixed(3)} speaker_${segments[i].speaker}'); |
| 99 | + } |
| 100 | +} |
0 commit comments