-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add Rust API for speaker diarization #3370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
| use sherpa_onnx::{ | ||
| FastClusteringConfig, OfflineSpeakerDiarization, OfflineSpeakerDiarizationConfig, | ||
| OfflineSpeakerSegmentationModelConfig, OfflineSpeakerSegmentationPyannoteModelConfig, | ||
| SpeakerEmbeddingExtractorConfig, Wave, | ||
| }; | ||
|
|
||
| fn main() { | ||
| let config = OfflineSpeakerDiarizationConfig { | ||
| segmentation: OfflineSpeakerSegmentationModelConfig { | ||
| pyannote: OfflineSpeakerSegmentationPyannoteModelConfig { | ||
| model: Some("./sherpa-onnx-pyannote-segmentation-3-0/model.onnx".into()), | ||
| }, | ||
| ..Default::default() | ||
| }, | ||
| embedding: SpeakerEmbeddingExtractorConfig { | ||
| model: Some("./3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx".into()), | ||
| ..Default::default() | ||
| }, | ||
| clustering: FastClusteringConfig { | ||
| num_clusters: 4, | ||
| ..Default::default() | ||
| }, | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let sd = OfflineSpeakerDiarization::create(&config) | ||
| .expect("Failed to initialize offline speaker diarization"); | ||
|
|
||
| let wave = Wave::read("./0-four-speakers-zh.wav").expect("Failed to read wave"); | ||
|
|
||
| assert_eq!( | ||
| sd.sample_rate(), | ||
| wave.sample_rate(), | ||
| "Unexpected sample rate" | ||
| ); | ||
|
|
||
| let result = sd | ||
| .process(wave.samples()) | ||
| .expect("Failed to do speaker diarization"); | ||
| println!("Number of speakers: {}", result.num_speakers()); | ||
| println!("Number of segments: {}", result.num_segments()); | ||
|
|
||
| for s in result.sort_by_start_time() { | ||
| println!("{:.3} -- {:.3} speaker_{:02}", s.start, s.end, s.speaker); | ||
| } | ||
| } |
This file contains hidden or 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,18 @@ | ||||||
| #!/usr/bin/env bash | ||||||
| set -ex | ||||||
|
|
||||||
| 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 | ||||||
|
||||||
| 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 | |
| curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-recognition-models/3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx |
This file contains hidden or 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
98 changes: 98 additions & 0 deletions
98
sherpa-onnx/rust/sherpa-onnx-sys/src/offline_speaker_diarization.rs
This file contains hidden or 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,98 @@ | ||
| #![allow(non_camel_case_types)] | ||
| #![allow(non_snake_case)] | ||
| #![allow(non_upper_case_globals)] | ||
|
|
||
| use std::os::raw::{c_char, c_float}; | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct OfflineSpeakerSegmentationPyannoteModelConfig { | ||
| pub model: *const c_char, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct OfflineSpeakerSegmentationModelConfig { | ||
| pub pyannote: OfflineSpeakerSegmentationPyannoteModelConfig, | ||
| pub num_threads: i32, | ||
| pub debug: i32, | ||
| pub provider: *const c_char, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct FastClusteringConfig { | ||
| pub num_clusters: i32, | ||
| pub threshold: c_float, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct OfflineSpeakerDiarizationConfig { | ||
| pub segmentation: OfflineSpeakerSegmentationModelConfig, | ||
| pub embedding: crate::speaker_embedding::SpeakerEmbeddingExtractorConfig, | ||
| pub clustering: FastClusteringConfig, | ||
| pub min_duration_on: c_float, | ||
| pub min_duration_off: c_float, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| pub struct OfflineSpeakerDiarization { | ||
| _private: [u8; 0], | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| pub struct OfflineSpeakerDiarizationResult { | ||
| _private: [u8; 0], | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct OfflineSpeakerDiarizationSegment { | ||
| pub start: c_float, | ||
| pub end: c_float, | ||
| pub speaker: i32, | ||
| } | ||
|
|
||
| extern "C" { | ||
| pub fn SherpaOnnxCreateOfflineSpeakerDiarization( | ||
| config: *const OfflineSpeakerDiarizationConfig, | ||
| ) -> *const OfflineSpeakerDiarization; | ||
|
|
||
| pub fn SherpaOnnxDestroyOfflineSpeakerDiarization(sd: *const OfflineSpeakerDiarization); | ||
|
|
||
| pub fn SherpaOnnxOfflineSpeakerDiarizationGetSampleRate( | ||
| sd: *const OfflineSpeakerDiarization, | ||
| ) -> i32; | ||
|
|
||
| pub fn SherpaOnnxOfflineSpeakerDiarizationSetConfig( | ||
| sd: *const OfflineSpeakerDiarization, | ||
| config: *const OfflineSpeakerDiarizationConfig, | ||
| ); | ||
|
|
||
| pub fn SherpaOnnxOfflineSpeakerDiarizationResultGetNumSpeakers( | ||
| r: *const OfflineSpeakerDiarizationResult, | ||
| ) -> i32; | ||
|
|
||
| pub fn SherpaOnnxOfflineSpeakerDiarizationResultGetNumSegments( | ||
| r: *const OfflineSpeakerDiarizationResult, | ||
| ) -> i32; | ||
|
|
||
| pub fn SherpaOnnxOfflineSpeakerDiarizationResultSortByStartTime( | ||
| r: *const OfflineSpeakerDiarizationResult, | ||
| ) -> *const OfflineSpeakerDiarizationSegment; | ||
|
|
||
| pub fn SherpaOnnxOfflineSpeakerDiarizationDestroySegment( | ||
| s: *const OfflineSpeakerDiarizationSegment, | ||
| ); | ||
|
|
||
| pub fn SherpaOnnxOfflineSpeakerDiarizationProcess( | ||
| sd: *const OfflineSpeakerDiarization, | ||
| samples: *const c_float, | ||
| n: i32, | ||
| ) -> *const OfflineSpeakerDiarizationResult; | ||
|
|
||
| pub fn SherpaOnnxOfflineSpeakerDiarizationDestroyResult( | ||
| r: *const OfflineSpeakerDiarizationResult, | ||
| ); | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a typo in the URL:
recongitionshould berecognition. While the current URL with the typo works because a release with the typo exists, it's better to use the corrected URL for future-proofing and clarity, as a corrected release tag also exists.