Fangjun Kuang
Committed by GitHub

Add Java API for audio tagging (#820)

正在显示 39 个修改的文件 包含 476 行增加129 行删除
@@ -106,6 +106,17 @@ jobs: @@ -106,6 +106,17 @@ jobs:
106 make -j4 106 make -j4
107 ls -lh lib 107 ls -lh lib
108 108
  109 + - name: Run java test (audio tagging)
  110 + shell: bash
  111 + run: |
  112 + cd ./java-api-examples
  113 + ./run-audio-tagging-zipformer-from-file.sh
  114 + # Delete model files to save space
  115 + rm -rf sherpa-onnx-zipformer-*
  116 +
  117 + ./run-audio-tagging-ced-from-file.sh
  118 + rm -rf sherpa-onnx-ced-*
  119 +
109 - name: Run java test (add punctuations) 120 - name: Run java test (add punctuations)
110 shell: bash 121 shell: bash
111 run: | 122 run: |
@@ -101,3 +101,4 @@ sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01 @@ -101,3 +101,4 @@ sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01
101 *.tar.gz 101 *.tar.gz
102 *.tar.bz2 102 *.tar.bz2
103 *.zip 103 *.zip
  104 +sherpa-onnx-ced-*
  1 +// Copyright 2024 Xiaomi Corporation
  2 +
  3 +// This file shows how to use a CED audio tagging model to tag
  4 +// input audio files.
  5 +import com.k2fsa.sherpa.onnx.*;
  6 +
  7 +public class AudioTaggingCEDFromFile {
  8 + public static void main(String[] args) {
  9 + // please download the model from
  10 + // https://github.com/k2-fsa/sherpa-onnx/releases/tag/audio-tagging-models
  11 + String model = "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/model.int8.onnx";
  12 + String labels = "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/class_labels_indices.csv";
  13 + int topK = 5;
  14 +
  15 + String[] testWaves =
  16 + new String[] {
  17 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/1.wav",
  18 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/2.wav",
  19 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/3.wav",
  20 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/4.wav",
  21 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/5.wav",
  22 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/6.wav",
  23 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/7.wav",
  24 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/8.wav",
  25 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/9.wav",
  26 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/10.wav",
  27 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/11.wav",
  28 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/12.wav",
  29 + "./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/test_wavs/13.wav",
  30 + };
  31 +
  32 + AudioTaggingModelConfig modelConfig =
  33 + AudioTaggingModelConfig.builder().setCED(model).setNumThreads(1).setDebug(true).build();
  34 +
  35 + AudioTaggingConfig config =
  36 + AudioTaggingConfig.builder().setModel(modelConfig).setLabels(labels).setTopK(topK).build();
  37 +
  38 + AudioTagging tagger = new AudioTagging(config);
  39 + System.out.println("------");
  40 + for (String filename : testWaves) {
  41 + WaveReader reader = new WaveReader(filename);
  42 +
  43 + OfflineStream stream = tagger.createStream();
  44 + stream.acceptWaveform(reader.getSamples(), reader.getSampleRate());
  45 +
  46 + AudioEvent[] events = tagger.compute(stream);
  47 +
  48 + stream.release();
  49 +
  50 + System.out.printf("input file: %s\n", filename);
  51 + System.out.printf("Probability\t\tName\n");
  52 + for (AudioEvent e : events) {
  53 + System.out.printf("%.3f\t\t\t%s\n", e.getProb(), e.getName());
  54 + }
  55 + System.out.println("------");
  56 + }
  57 +
  58 + tagger.release();
  59 + }
  60 +}
  1 +// Copyright 2024 Xiaomi Corporation
  2 +
  3 +// This file shows how to use a zipformer audio tagging model to tag
  4 +// input audio files.
  5 +import com.k2fsa.sherpa.onnx.*;
  6 +
  7 +public class AudioTaggingZipformerFromFile {
  8 + public static void main(String[] args) {
  9 + // please download the model from
  10 + // https://github.com/k2-fsa/sherpa-onnx/releases/tag/audio-tagging-models
  11 + String model = "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/model.int8.onnx";
  12 + String labels =
  13 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/class_labels_indices.csv";
  14 + int topK = 5;
  15 +
  16 + String[] testWaves =
  17 + new String[] {
  18 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/1.wav",
  19 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/2.wav",
  20 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/3.wav",
  21 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/4.wav",
  22 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/5.wav",
  23 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/6.wav",
  24 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/7.wav",
  25 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/8.wav",
  26 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/9.wav",
  27 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/10.wav",
  28 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/11.wav",
  29 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/12.wav",
  30 + "./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/test_wavs/13.wav",
  31 + };
  32 +
  33 + OfflineZipformerAudioTaggingModelConfig zipformer =
  34 + OfflineZipformerAudioTaggingModelConfig.builder().setModel(model).build();
  35 +
  36 + AudioTaggingModelConfig modelConfig =
  37 + AudioTaggingModelConfig.builder()
  38 + .setZipformer(zipformer)
  39 + .setNumThreads(1)
  40 + .setDebug(true)
  41 + .build();
  42 +
  43 + AudioTaggingConfig config =
  44 + AudioTaggingConfig.builder().setModel(modelConfig).setLabels(labels).setTopK(topK).build();
  45 +
  46 + AudioTagging tagger = new AudioTagging(config);
  47 + System.out.println("------");
  48 + for (String filename : testWaves) {
  49 + WaveReader reader = new WaveReader(filename);
  50 +
  51 + OfflineStream stream = tagger.createStream();
  52 + stream.acceptWaveform(reader.getSamples(), reader.getSampleRate());
  53 +
  54 + AudioEvent[] events = tagger.compute(stream);
  55 +
  56 + stream.release();
  57 +
  58 + System.out.printf("input file: %s\n", filename);
  59 + System.out.printf("Probability\t\tName\n");
  60 + for (AudioEvent e : events) {
  61 + System.out.printf("%.3f\t\t\t%s\n", e.getProb(), e.getName());
  62 + }
  63 + System.out.println("------");
  64 + }
  65 +
  66 + tagger.release();
  67 + }
  68 +}
@@ -36,10 +36,17 @@ This directory contains examples for the JAVA API of sherpa-onnx. @@ -36,10 +36,17 @@ This directory contains examples for the JAVA API of sherpa-onnx.
36 ./run-spoken-language-identification-whisper.sh 36 ./run-spoken-language-identification-whisper.sh
37 ``` 37 ```
38 38
39 -## Add puncutations to text 39 +## Add punctuations to text
40 40
41 The punctuation model supports both English and Chinese. 41 The punctuation model supports both English and Chinese.
42 42
43 ```bash 43 ```bash
44 ./run-add-punctuation-zh-en.sh 44 ./run-add-punctuation-zh-en.sh
45 ``` 45 ```
  46 +
  47 +## Audio tagging
  48 +
  49 +```bash
  50 +./run-audio-tagging-zipformer-from-file.sh
  51 +./run-audio-tagging-ced-from-file.sh
  52 +```
@@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then @@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
25 popd 25 popd
26 fi 26 fi
27 27
28 -if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then  
29 - cmake \  
30 - -DSHERPA_ONNX_ENABLE_PYTHON=OFF \  
31 - -DSHERPA_ONNX_ENABLE_TESTS=OFF \  
32 - -DSHERPA_ONNX_ENABLE_CHECK=OFF \  
33 - -DBUILD_SHARED_LIBS=ON \  
34 - -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \  
35 - -DSHERPA_ONNX_ENABLE_JNI=ON \  
36 - ..  
37 -  
38 - make -j4  
39 - ls -lh lib  
40 -fi  
41 -  
42 if [ ! -f ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx ]; then 28 if [ ! -f ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx ]; then
43 curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 29 curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
44 tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 30 tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
  1 +#!/usr/bin/env bash
  2 +
  3 +set -ex
  4 +
  5 +if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
  6 + mkdir -p ../build
  7 + pushd ../build
  8 + cmake \
  9 + -DSHERPA_ONNX_ENABLE_PYTHON=OFF \
  10 + -DSHERPA_ONNX_ENABLE_TESTS=OFF \
  11 + -DSHERPA_ONNX_ENABLE_CHECK=OFF \
  12 + -DBUILD_SHARED_LIBS=ON \
  13 + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
  14 + -DSHERPA_ONNX_ENABLE_JNI=ON \
  15 + ..
  16 +
  17 + make -j4
  18 + ls -lh lib
  19 + popd
  20 +fi
  21 +
  22 +if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
  23 + pushd ../sherpa-onnx/java-api
  24 + make
  25 + popd
  26 +fi
  27 +
  28 +if [ ! -f ./sherpa-onnx-ced-mini-audio-tagging-2024-04-19/model.int8.onnx ]; then
  29 + curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/audio-tagging-models/sherpa-onnx-ced-mini-audio-tagging-2024-04-19.tar.bz2
  30 + tar xvf sherpa-onnx-ced-mini-audio-tagging-2024-04-19.tar.bz2
  31 + rm sherpa-onnx-ced-mini-audio-tagging-2024-04-19.tar.bz2
  32 +fi
  33 +
  34 +java \
  35 + -Djava.library.path=$PWD/../build/lib \
  36 + -cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
  37 + ./AudioTaggingCEDFromFile.java
  1 +#!/usr/bin/env bash
  2 +
  3 +set -ex
  4 +
  5 +if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
  6 + mkdir -p ../build
  7 + pushd ../build
  8 + cmake \
  9 + -DSHERPA_ONNX_ENABLE_PYTHON=OFF \
  10 + -DSHERPA_ONNX_ENABLE_TESTS=OFF \
  11 + -DSHERPA_ONNX_ENABLE_CHECK=OFF \
  12 + -DBUILD_SHARED_LIBS=ON \
  13 + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
  14 + -DSHERPA_ONNX_ENABLE_JNI=ON \
  15 + ..
  16 +
  17 + make -j4
  18 + ls -lh lib
  19 + popd
  20 +fi
  21 +
  22 +if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
  23 + pushd ../sherpa-onnx/java-api
  24 + make
  25 + popd
  26 +fi
  27 +
  28 +if [ ! -f ./sherpa-onnx-zipformer-small-audio-tagging-2024-04-15/model.int8.onnx ]; then
  29 + curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/audio-tagging-models/sherpa-onnx-zipformer-small-audio-tagging-2024-04-15.tar.bz2
  30 + tar xvf sherpa-onnx-zipformer-small-audio-tagging-2024-04-15.tar.bz2
  31 + rm sherpa-onnx-zipformer-small-audio-tagging-2024-04-15.tar.bz2
  32 +fi
  33 +
  34 +java \
  35 + -Djava.library.path=$PWD/../build/lib \
  36 + -cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
  37 + ./AudioTaggingZipformerFromFile.java
@@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then @@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
25 popd 25 popd
26 fi 26 fi
27 27
28 -if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then  
29 - cmake \  
30 - -DSHERPA_ONNX_ENABLE_PYTHON=OFF \  
31 - -DSHERPA_ONNX_ENABLE_TESTS=OFF \  
32 - -DSHERPA_ONNX_ENABLE_CHECK=OFF \  
33 - -DBUILD_SHARED_LIBS=ON \  
34 - -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \  
35 - -DSHERPA_ONNX_ENABLE_JNI=ON \  
36 - ..  
37 -  
38 - make -j4  
39 - ls -lh lib  
40 -fi  
41 -  
42 if [ ! -f ./sherpa-onnx-nemo-ctc-en-citrinet-512/tokens.txt ]; then 28 if [ ! -f ./sherpa-onnx-nemo-ctc-en-citrinet-512/tokens.txt ]; then
43 curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2 29 curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2
44 tar xvf sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2 30 tar xvf sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2
@@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then @@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
25 popd 25 popd
26 fi 26 fi
27 27
28 -if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then  
29 - cmake \  
30 - -DSHERPA_ONNX_ENABLE_PYTHON=OFF \  
31 - -DSHERPA_ONNX_ENABLE_TESTS=OFF \  
32 - -DSHERPA_ONNX_ENABLE_CHECK=OFF \  
33 - -DBUILD_SHARED_LIBS=ON \  
34 - -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \  
35 - -DSHERPA_ONNX_ENABLE_JNI=ON \  
36 - ..  
37 -  
38 - make -j4  
39 - ls -lh lib  
40 -fi  
41 -  
42 if [ ! -f ./sherpa-onnx-paraformer-zh-2023-03-28/tokens.txt ]; then 28 if [ ! -f ./sherpa-onnx-paraformer-zh-2023-03-28/tokens.txt ]; then
43 curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2 29 curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2
44 30
@@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then @@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
25 popd 25 popd
26 fi 26 fi
27 27
28 -if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then  
29 - cmake \  
30 - -DSHERPA_ONNX_ENABLE_PYTHON=OFF \  
31 - -DSHERPA_ONNX_ENABLE_TESTS=OFF \  
32 - -DSHERPA_ONNX_ENABLE_CHECK=OFF \  
33 - -DBUILD_SHARED_LIBS=ON \  
34 - -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \  
35 - -DSHERPA_ONNX_ENABLE_JNI=ON \  
36 - ..  
37 -  
38 - make -j4  
39 - ls -lh lib  
40 -fi  
41 -  
42 if [ ! -f ./sherpa-onnx-zipformer-gigaspeech-2023-12-12/tokens.txt ]; then 28 if [ ! -f ./sherpa-onnx-zipformer-gigaspeech-2023-12-12/tokens.txt ]; then
43 curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-zipformer-gigaspeech-2023-12-12.tar.bz2 29 curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-zipformer-gigaspeech-2023-12-12.tar.bz2
44 30
@@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then @@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
25 popd 25 popd
26 fi 26 fi
27 27
28 -if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then  
29 - cmake \  
30 - -DSHERPA_ONNX_ENABLE_PYTHON=OFF \  
31 - -DSHERPA_ONNX_ENABLE_TESTS=OFF \  
32 - -DSHERPA_ONNX_ENABLE_CHECK=OFF \  
33 - -DBUILD_SHARED_LIBS=ON \  
34 - -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \  
35 - -DSHERPA_ONNX_ENABLE_JNI=ON \  
36 - ..  
37 -  
38 - make -j4  
39 - ls -lh lib  
40 -fi  
41 -  
42 if [ ! -f ./sherpa-onnx-whisper-tiny.en/tiny.en-tokens.txt ]; then 28 if [ ! -f ./sherpa-onnx-whisper-tiny.en/tiny.en-tokens.txt ]; then
43 curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-whisper-tiny.en.tar.bz2 29 curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-whisper-tiny.en.tar.bz2
44 30
@@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then @@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
25 popd 25 popd
26 fi 26 fi
27 27
28 -if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then  
29 - cmake \  
30 - -DSHERPA_ONNX_ENABLE_PYTHON=OFF \  
31 - -DSHERPA_ONNX_ENABLE_TESTS=OFF \  
32 - -DSHERPA_ONNX_ENABLE_CHECK=OFF \  
33 - -DBUILD_SHARED_LIBS=ON \  
34 - -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \  
35 - -DSHERPA_ONNX_ENABLE_JNI=ON \  
36 - ..  
37 -  
38 - make -j4  
39 - ls -lh lib  
40 -fi  
41 -  
42 # please visit 28 # please visit
43 # https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models 29 # https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
44 # to download more models 30 # to download more models
@@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then @@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
25 popd 25 popd
26 fi 26 fi
27 27
28 -if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then  
29 - cmake \  
30 - -DSHERPA_ONNX_ENABLE_PYTHON=OFF \  
31 - -DSHERPA_ONNX_ENABLE_TESTS=OFF \  
32 - -DSHERPA_ONNX_ENABLE_CHECK=OFF \  
33 - -DBUILD_SHARED_LIBS=ON \  
34 - -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \  
35 - -DSHERPA_ONNX_ENABLE_JNI=ON \  
36 - ..  
37 -  
38 - make -j4  
39 - ls -lh lib  
40 -fi  
41 -  
42 # please visit 28 # please visit
43 # https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models 29 # https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
44 # to download more models 30 # to download more models
@@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then @@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
25 popd 25 popd
26 fi 26 fi
27 27
28 -if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then  
29 - cmake \  
30 - -DSHERPA_ONNX_ENABLE_PYTHON=OFF \  
31 - -DSHERPA_ONNX_ENABLE_TESTS=OFF \  
32 - -DSHERPA_ONNX_ENABLE_CHECK=OFF \  
33 - -DBUILD_SHARED_LIBS=ON \  
34 - -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \  
35 - -DSHERPA_ONNX_ENABLE_JNI=ON \  
36 - ..  
37 -  
38 - make -j4  
39 - ls -lh lib  
40 -fi  
41 -  
42 # please visit 28 # please visit
43 # https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models 29 # https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
44 # to download more models 30 # to download more models
@@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then @@ -25,20 +25,6 @@ if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
25 popd 25 popd
26 fi 26 fi
27 27
28 -if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then  
29 - cmake \  
30 - -DSHERPA_ONNX_ENABLE_PYTHON=OFF \  
31 - -DSHERPA_ONNX_ENABLE_TESTS=OFF \  
32 - -DSHERPA_ONNX_ENABLE_CHECK=OFF \  
33 - -DBUILD_SHARED_LIBS=ON \  
34 - -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \  
35 - -DSHERPA_ONNX_ENABLE_JNI=ON \  
36 - ..  
37 -  
38 - make -j4  
39 - ls -lh lib  
40 -fi  
41 -  
42 # Note that it needs a multilingual whisper model. so, for example, tiny works while tiny.en does not work 28 # Note that it needs a multilingual whisper model. so, for example, tiny works while tiny.en does not work
43 # https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-whisper-tiny.tar.bz2 29 # https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-whisper-tiny.tar.bz2
44 if [ ! -f ./sherpa-onnx-whisper-tiny/tiny-encoder.int8.onnx ]; then 30 if [ ! -f ./sherpa-onnx-whisper-tiny/tiny-encoder.int8.onnx ]; then
  1 +# Copyright 2024 Xiaomi Corporation
1 2
2 # all .class and .jar files are put inside out_dir 3 # all .class and .jar files are put inside out_dir
3 out_dir := build 4 out_dir := build
@@ -44,6 +45,12 @@ java_files += OfflinePunctuationModelConfig.java @@ -44,6 +45,12 @@ java_files += OfflinePunctuationModelConfig.java
44 java_files += OfflinePunctuationConfig.java 45 java_files += OfflinePunctuationConfig.java
45 java_files += OfflinePunctuation.java 46 java_files += OfflinePunctuation.java
46 47
  48 +java_files += OfflineZipformerAudioTaggingModelConfig.java
  49 +java_files += AudioTaggingModelConfig.java
  50 +java_files += AudioTaggingConfig.java
  51 +java_files += AudioEvent.java
  52 +java_files += AudioTagging.java
  53 +
47 class_files := $(java_files:%.java=%.class) 54 class_files := $(java_files:%.java=%.class)
48 55
49 java_files := $(addprefix src/$(package_dir)/,$(java_files)) 56 java_files := $(addprefix src/$(package_dir)/,$(java_files))
  1 +// Copyright 2024 Xiaomi Corporation
  2 +
  3 +package com.k2fsa.sherpa.onnx;
  4 +
  5 +public class AudioEvent {
  6 + private String name = "";
  7 + private int index = 0;
  8 + private float prob = 0;
  9 +
  10 + public AudioEvent(String name, int index, float prob) {
  11 + this.name = name;
  12 + this.index = index;
  13 + this.prob = prob;
  14 + }
  15 +
  16 + public String getName() {
  17 + return name;
  18 + }
  19 +
  20 + public int getIndex() {
  21 + return index;
  22 + }
  23 +
  24 + public float getProb() {
  25 + return prob;
  26 + }
  27 +
  28 + @Override
  29 + public String toString() {
  30 + return String.format("AudioEven(name=%s, index=%d, prob=%.3f)\n", name, index, prob);
  31 + }
  32 +}
  1 +// Copyright 2024 Xiaomi Corporation
  2 +
  3 +package com.k2fsa.sherpa.onnx;
  4 +
  5 +public class AudioTagging {
  6 + static {
  7 + System.loadLibrary("sherpa-onnx-jni");
  8 + }
  9 +
  10 + private long ptr = 0;
  11 +
  12 + public AudioTagging(AudioTaggingConfig config) {
  13 + ptr = newFromFile(config);
  14 + }
  15 +
  16 + public OfflineStream createStream() {
  17 + long p = createStream(ptr);
  18 + return new OfflineStream(p);
  19 + }
  20 +
  21 + public AudioEvent[] compute(OfflineStream stream) {
  22 + return compute(stream, -1);
  23 +
  24 + }
  25 +
  26 + public AudioEvent[] compute(OfflineStream stream, int topK) {
  27 + Object[] arr = compute(ptr, stream.getPtr(), topK);
  28 +
  29 + AudioEvent[] events = new AudioEvent[arr.length];
  30 + for (int i = 0; i < arr.length; ++i) {
  31 + Object[] obj = (Object[]) arr[i];
  32 + String name = (String) obj[0];
  33 + int index = (int) obj[1];
  34 + float prob = (float) obj[2];
  35 + events[i] = new AudioEvent(name, index, prob);
  36 + }
  37 + return events;
  38 + }
  39 +
  40 +
  41 + @Override
  42 + protected void finalize() throws Throwable {
  43 + release();
  44 + }
  45 +
  46 + // You'd better call it manually if it is not used anymore
  47 + public void release() {
  48 + if (this.ptr == 0) {
  49 + return;
  50 + }
  51 + delete(this.ptr);
  52 + this.ptr = 0;
  53 + }
  54 +
  55 + private native void delete(long ptr);
  56 +
  57 + private native long newFromFile(AudioTaggingConfig config);
  58 +
  59 + private native long createStream(long ptr);
  60 +
  61 + private native Object[] compute(long ptr, long streamPtr, int topK);
  62 +}
  1 +// Copyright 2024 Xiaomi Corporation
  2 +
  3 +package com.k2fsa.sherpa.onnx;
  4 +
  5 +public class AudioTaggingConfig {
  6 + private final AudioTaggingModelConfig model;
  7 + private final String labels;
  8 + private final int topK;
  9 +
  10 + private AudioTaggingConfig(Builder builder) {
  11 + this.model = builder.model;
  12 + this.labels = builder.labels;
  13 + this.topK = builder.topK;
  14 + }
  15 +
  16 + public static Builder builder() {
  17 + return new AudioTaggingConfig.Builder();
  18 + }
  19 +
  20 + public static class Builder {
  21 + private AudioTaggingModelConfig model = AudioTaggingModelConfig.builder().build();
  22 + private String labels = "";
  23 + private int topK = 5;
  24 +
  25 + public AudioTaggingConfig build() {
  26 + return new AudioTaggingConfig(this);
  27 + }
  28 +
  29 + public Builder setModel(AudioTaggingModelConfig model) {
  30 + this.model = model;
  31 + return this;
  32 + }
  33 +
  34 + public Builder setLabels(String labels) {
  35 + this.labels = labels;
  36 + return this;
  37 + }
  38 +
  39 + public Builder setTopK(int topK) {
  40 + this.topK = topK;
  41 + return this;
  42 + }
  43 + }
  44 +}
  1 +// Copyright 2024 Xiaomi Corporation
  2 +
  3 +package com.k2fsa.sherpa.onnx;
  4 +
  5 +public class AudioTaggingModelConfig {
  6 + private final OfflineZipformerAudioTaggingModelConfig zipformer;
  7 + private final String ced;
  8 + private final int numThreads;
  9 + private final boolean debug;
  10 + private final String provider;
  11 +
  12 + private AudioTaggingModelConfig(Builder builder) {
  13 + this.zipformer = builder.zipformer;
  14 + this.ced = builder.ced;
  15 + this.numThreads = builder.numThreads;
  16 + this.debug = builder.debug;
  17 + this.provider = builder.provider;
  18 + }
  19 +
  20 + public static Builder builder() {
  21 + return new Builder();
  22 + }
  23 +
  24 + public static class Builder {
  25 + private OfflineZipformerAudioTaggingModelConfig zipformer = OfflineZipformerAudioTaggingModelConfig.builder().build();
  26 + private String ced = "";
  27 + private int numThreads = 1;
  28 + private boolean debug = true;
  29 + private String provider = "cpu";
  30 +
  31 + public AudioTaggingModelConfig build() {
  32 + return new AudioTaggingModelConfig(this);
  33 + }
  34 +
  35 + public Builder setZipformer(OfflineZipformerAudioTaggingModelConfig zipformer) {
  36 + this.zipformer = zipformer;
  37 + return this;
  38 + }
  39 +
  40 + public Builder setCED(String ced) {
  41 + this.ced = ced;
  42 + return this;
  43 + }
  44 +
  45 + public Builder setNumThreads(int numThreads) {
  46 + this.numThreads = numThreads;
  47 + return this;
  48 + }
  49 +
  50 + public Builder setDebug(boolean debug) {
  51 + this.debug = debug;
  52 + return this;
  53 + }
  54 +
  55 + public Builder setProvider(String provider) {
  56 + this.provider = provider;
  57 + return this;
  58 + }
  59 + }
  60 +}
1 // Copyright 2022-2023 by zhaoming 1 // Copyright 2022-2023 by zhaoming
2 // Copyright 2024 Xiaomi Corporation 2 // Copyright 2024 Xiaomi Corporation
  3 +
3 package com.k2fsa.sherpa.onnx; 4 package com.k2fsa.sherpa.onnx;
4 5
5 public class EndpointRule { 6 public class EndpointRule {
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OfflineModelConfig { 5 public class OfflineModelConfig {
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OfflineNemoEncDecCtcModelConfig { 5 public class OfflineNemoEncDecCtcModelConfig {
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OfflineParaformerModelConfig { 5 public class OfflineParaformerModelConfig {
@@ -7,7 +7,7 @@ public class OfflinePunctuation { @@ -7,7 +7,7 @@ public class OfflinePunctuation {
7 System.loadLibrary("sherpa-onnx-jni"); 7 System.loadLibrary("sherpa-onnx-jni");
8 } 8 }
9 9
10 - private long ptr = 0; // this is the asr engine ptrss 10 + private long ptr = 0;
11 11
12 public OfflinePunctuation(OfflinePunctuationConfig config) { 12 public OfflinePunctuation(OfflinePunctuationConfig config) {
13 ptr = newFromFile(config); 13 ptr = newFromFile(config);
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OfflineRecognizer { 5 public class OfflineRecognizer {
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OfflineRecognizerConfig { 5 public class OfflineRecognizerConfig {
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OfflineRecognizerResult { 5 public class OfflineRecognizerResult {
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OfflineStream { 5 public class OfflineStream {
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OfflineTransducerModelConfig { 5 public class OfflineTransducerModelConfig {
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OfflineWhisperModelConfig { 5 public class OfflineWhisperModelConfig {
  1 +// Copyright 2024 Xiaomi Corporation
  2 +
  3 +package com.k2fsa.sherpa.onnx;
  4 +
  5 +public class OfflineZipformerAudioTaggingModelConfig {
  6 + private final String model;
  7 +
  8 + private OfflineZipformerAudioTaggingModelConfig(Builder builder) {
  9 + this.model = builder.model;
  10 + }
  11 +
  12 + public static Builder builder() {
  13 + return new Builder();
  14 + }
  15 +
  16 + public String getModel() {
  17 + return model;
  18 + }
  19 +
  20 + public static class Builder {
  21 + private String model = "";
  22 +
  23 + public OfflineZipformerAudioTaggingModelConfig build() {
  24 + return new OfflineZipformerAudioTaggingModelConfig(this);
  25 + }
  26 +
  27 + public Builder setModel(String model) {
  28 + this.model = model;
  29 + return this;
  30 + }
  31 + }
  32 +}
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OnlineCtcFstDecoderConfig { 5 public class OnlineCtcFstDecoderConfig {
1 // Copyright 2022-2023 by zhaoming 1 // Copyright 2022-2023 by zhaoming
2 // Copyright 2024 Xiaomi Corporation 2 // Copyright 2024 Xiaomi Corporation
3 -package com.k2fsa.sherpa.onnx;  
4 3
  4 +package com.k2fsa.sherpa.onnx;
5 5
6 public class OnlineRecognizer { 6 public class OnlineRecognizer {
7 static { 7 static {
1 // Copyright 2022-2023 by zhaoming 1 // Copyright 2022-2023 by zhaoming
2 // Copyright 2024 Xiaomi Corporation 2 // Copyright 2024 Xiaomi Corporation
  3 +
3 package com.k2fsa.sherpa.onnx; 4 package com.k2fsa.sherpa.onnx;
4 5
5 public class OnlineRecognizerConfig { 6 public class OnlineRecognizerConfig {
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OnlineRecognizerResult { 5 public class OnlineRecognizerResult {
1 // Copyright 2022-2023 by zhaoming 1 // Copyright 2022-2023 by zhaoming
2 // Copyright 2024 Xiaomi Corporation 2 // Copyright 2024 Xiaomi Corporation
  3 +
3 package com.k2fsa.sherpa.onnx; 4 package com.k2fsa.sherpa.onnx;
4 5
5 public class OnlineStream { 6 public class OnlineStream {
1 // Copyright 2024 Xiaomi Corporation 1 // Copyright 2024 Xiaomi Corporation
  2 +
2 package com.k2fsa.sherpa.onnx; 3 package com.k2fsa.sherpa.onnx;
3 4
4 public class OnlineZipformer2CtcModelConfig { 5 public class OnlineZipformer2CtcModelConfig {