Skip to content

Commit

Permalink
Add Go API for speaker identification (#718)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Mar 29, 2024
1 parent 2e0bcca commit 6da4a1c
Show file tree
Hide file tree
Showing 25 changed files with 641 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test-go-package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ jobs:
run: |
gcc --version
- name: Test speaker identification
shell: bash
run: |
cd go-api-examples/speaker-identification
./run.sh
- name: Test non-streaming TTS (Linux/macOS)
if: matrix.os != 'windows-latest'
shell: bash
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/test-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ jobs:
go mod tidy
go build
- name: Test speaker identification
shell: bash
run: |
cd scripts/go/_internal/speaker-identification/
./run.sh
- name: Test non-streaming TTS (macOS)
shell: bash
run: |
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ vits-mms-*
*.tar.bz2
sherpa-onnx-paraformer-trilingual-zh-cantonese-en
sr-data
*xcworkspace/xcuserdata/*

4 changes: 4 additions & 0 deletions go-api-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ for details.
- [./vad-spoken-language-identification](./vad-spoken-language-identification) It shows how to use silero VAD + Whisper
for spoken language identification.

- [./speaker-identification](./speaker-identification) It shows how to use Go API for speaker identification.

- [./vad-speaker-identification](./vad-speaker-identification) It shows how to use Go API for VAD + speaker identification.

[sherpa-onnx]: https://github.com/k2-fsa/sherpa-onnx
3 changes: 3 additions & 0 deletions go-api-examples/speaker-identification/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module speaker-identification

go 1.12
146 changes: 146 additions & 0 deletions go-api-examples/speaker-identification/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package main

import (
sherpa "github.com/k2-fsa/sherpa-onnx-go/sherpa_onnx"
"log"
)

func createSpeakerEmbeddingExtractor() *sherpa.SpeakerEmbeddingExtractor {
config := sherpa.SpeakerEmbeddingExtractorConfig{}

// Please download the model from
// https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-recongition-models/3dspeaker_speech_campplus_sv_zh-cn_16k-common.onnx
//
// You can find more models at
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/speaker-recongition-models

config.Model = "./3dspeaker_speech_campplus_sv_zh-cn_16k-common.onnx"
config.NumThreads = 1
config.Debug = 1
config.Provider = "cpu"

ex := sherpa.NewSpeakerEmbeddingExtractor(&config)
return ex
}

func computeEmbeddings(ex *sherpa.SpeakerEmbeddingExtractor, files []string) [][]float32 {
embeddings := make([][]float32, len(files))

for i, f := range files {
wave := sherpa.ReadWave(f)

stream := ex.CreateStream()
defer sherpa.DeleteOnlineStream(stream)
stream.AcceptWaveform(wave.SampleRate, wave.Samples)
stream.InputFinished()
embeddings[i] = ex.Compute(stream)
}

return embeddings

}

func registerSpeakers(ex *sherpa.SpeakerEmbeddingExtractor, manager *sherpa.SpeakerEmbeddingManager) {
// Please download the test data from
// https://github.com/csukuangfj/sr-data
spk1_files := []string{
"./sr-data/enroll/fangjun-sr-1.wav",
"./sr-data/enroll/fangjun-sr-2.wav",
"./sr-data/enroll/fangjun-sr-3.wav",
}

spk2_files := []string{
"./sr-data/enroll/leijun-sr-1.wav",
"./sr-data/enroll/leijun-sr-2.wav",
}

spk1_embeddings := computeEmbeddings(ex, spk1_files)
spk2_embeddings := computeEmbeddings(ex, spk2_files)

ok := manager.RegisterV("fangjun", spk1_embeddings)
if !ok {
panic("Failed to register fangjun")
}

ok = manager.RegisterV("leijun", spk2_embeddings)
if !ok {
panic("Failed to register leijun")
}

if !manager.Contains("fangjun") {
panic("Failed to find fangjun")
}

if !manager.Contains("leijun") {
panic("Failed to find leijun")
}

if manager.NumSpeakers() != 2 {
panic("There should be only 2 speakers")
}

all_speakers := manager.AllSpeakers()
log.Printf("All speakers: %v\n", all_speakers)
}

func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)

ex := createSpeakerEmbeddingExtractor()
defer sherpa.DeleteSpeakerEmbeddingExtractor(ex)

manager := sherpa.NewSpeakerEmbeddingManager(ex.Dim())
defer sherpa.DeleteSpeakerEmbeddingManager(manager)
registerSpeakers(ex, manager)

// Please download the test data from
// https://github.com/csukuangfj/sr-data
test1 := "./sr-data/test/fangjun-test-sr-1.wav"
embeddings := computeEmbeddings(ex, []string{test1})[0]
threshold := float32(0.6)
name := manager.Search(embeddings, threshold)
if len(name) > 0 {
log.Printf("%v matches %v", test1, name)
} else {
log.Printf("No matches found for %v", test1)
}

test2 := "./sr-data/test/leijun-test-sr-1.wav"
embeddings = computeEmbeddings(ex, []string{test2})[0]
name = manager.Search(embeddings, threshold)
if len(name) > 0 {
log.Printf("%v matches %v", test2, name)
} else {
log.Printf("No matches found for %v", test2)
}

test3 := "./sr-data/test/liudehua-test-sr-1.wav"
embeddings = computeEmbeddings(ex, []string{test3})[0]
name = manager.Search(embeddings, threshold)
if len(name) > 0 {
log.Printf("%v matches %v", test3, name)
} else {
log.Printf("No matches found for %v", test3)
}

if !manager.Remove("fangjun") {
panic("Failed to deregister fangjun")
} else {
log.Print("fangjun deregistered\n")
}

test1 = "./sr-data/test/fangjun-test-sr-1.wav"
embeddings = computeEmbeddings(ex, []string{test1})[0]
name = manager.Search(embeddings, threshold)
if len(name) > 0 {
log.Printf("%v matches %v", test1, name)
} else {
log.Printf("No matches found for %v", test1)
}
}

func chk(err error) {
if err != nil {
panic(err)
}
}
13 changes: 13 additions & 0 deletions go-api-examples/speaker-identification/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

if [ ! -f ./3dspeaker_speech_campplus_sv_zh-cn_16k-common.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-recongition-models/3dspeaker_speech_campplus_sv_zh-cn_16k-common.onnx
fi

if [ ! -f ./sr-data/enroll/fangjun-sr-1.wav ]; then
git clone https://github.com/csukuangfj/sr-data
fi

go mod tidy
go build
./speaker-identification
4 changes: 2 additions & 2 deletions go-api-examples/vad-asr-paraformer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func main() {

duration := float32(len(speechSegment.Samples)) / float32(config.SampleRate)

audio := &sherpa.GeneratedAudio{}
audio := &sherpa.Wave{}
audio.Samples = speechSegment.Samples
audio.SampleRate = config.SampleRate

Expand All @@ -120,7 +120,7 @@ func main() {
chk(s.Stop())
}

func decode(recognizer *sherpa.OfflineRecognizer, audio *sherpa.GeneratedAudio, id int) {
func decode(recognizer *sherpa.OfflineRecognizer, audio *sherpa.Wave, id int) {
stream := sherpa.NewOfflineStream(recognizer)
defer sherpa.DeleteOfflineStream(stream)
stream.AcceptWaveform(audio.SampleRate, audio.Samples)
Expand Down
4 changes: 2 additions & 2 deletions go-api-examples/vad-asr-whisper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func main() {

duration := float32(len(speechSegment.Samples)) / float32(config.SampleRate)

audio := &sherpa.GeneratedAudio{}
audio := &sherpa.Wave{}
audio.Samples = speechSegment.Samples
audio.SampleRate = config.SampleRate

Expand All @@ -118,7 +118,7 @@ func main() {
chk(s.Stop())
}

func decode(recognizer *sherpa.OfflineRecognizer, audio *sherpa.GeneratedAudio, id int) {
func decode(recognizer *sherpa.OfflineRecognizer, audio *sherpa.Wave, id int) {
stream := sherpa.NewOfflineStream(recognizer)
defer sherpa.DeleteOfflineStream(stream)
stream.AcceptWaveform(audio.SampleRate, audio.Samples)
Expand Down
3 changes: 3 additions & 0 deletions go-api-examples/vad-speaker-identification/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vad-speaker-identification

go 1.12
Loading

0 comments on commit 6da4a1c

Please sign in to comment.