name: run-java-test

on:
  push:
    branches:
      - master
    paths:
      - '.github/workflows/run-java-test.yaml'
      - 'CMakeLists.txt'
      - 'cmake/**'
      - 'java-api-examples/**'
      - 'sherpa-onnx/csrc/*'
      - 'sherpa-onnx/jni/*'
      - 'sherpa-onnx/java-api/**'
  pull_request:
    branches:
      - master
    paths:
      - '.github/workflows/run-java-test.yaml'
      - 'CMakeLists.txt'
      - 'cmake/**'
      - 'java-api-examples/**'
      - 'sherpa-onnx/csrc/*'
      - 'sherpa-onnx/jni/*'
      - 'sherpa-onnx/java-api/**'
  workflow_dispatch:

concurrency:
  group: run-java-test-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  run_java_test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, macos-14]

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: ccache
        uses: hendrikmuhs/ccache-action@v1.2
        with:
          key: ${{ matrix.os }}-java

      - name: OS info
        shell: bash
        run: |
          uname -a

      - uses: actions/setup-java@v4
        with:
          distribution: 'temurin' # See 'Supported distributions' for available options
          java-version: '21'

      - name: Display java version
        shell: bash
        run: |
          java -version
          java -help
          echo "----"
          javac -version
          javac -help
          echo "JAVA_HOME is: ${JAVA_HOME}"

          cmake --version

      - name:  Build sherpa-onnx (jar)
        shell: bash
        run: |
          cd sherpa-onnx/java-api/
          make
          ls -lh

      - uses: actions/upload-artifact@v4
        with:
          name: sherpa-onnx-jar-${{ matrix.os }}
          path: sherpa-onnx/java-api/build

      - name:  Build sherpa-onnx (C++)
        shell: bash
        run: |
          export CMAKE_CXX_COMPILER_LAUNCHER=ccache
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"

          mkdir build
          cd build

          cmake \
            -DSHERPA_ONNX_ENABLE_PYTHON=OFF \
            -DSHERPA_ONNX_ENABLE_TESTS=OFF \
            -DSHERPA_ONNX_ENABLE_CHECK=OFF \
            -DBUILD_SHARED_LIBS=ON \
            -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
            -DSHERPA_ONNX_ENABLE_BINARY=OFF \
            -DBUILD_ESPEAK_NG_EXE=OFF \
            -DSHERPA_ONNX_ENABLE_JNI=ON \
            ..

            make -j4
            ls -lh lib

      - name:  Run java test (kws)
        shell: bash
        run: |
          cd ./java-api-examples
          ./run-kws-from-file.sh
          rm -rf sherpa-onnx-*

      - name:  Run java test (VAD + Non-streaming SenseVoice)
        shell: bash
        run: |
          cd ./java-api-examples
          ./run-vad-non-streaming-sense-voice.sh
          rm *.onnx
          ls -lh *.wav
          rm *.wav
          rm -rf sherpa-onnx-*

      - name:  Run java test (VAD + Non-streaming Paraformer)
        shell: bash
        run: |
          cd ./java-api-examples
          ./run-vad-non-streaming-paraformer.sh
          rm *.onnx
          ls -lh *.wav
          rm *.wav
          rm -rf sherpa-onnx-*

      - name:  Run java test (VAD remove silence)
        shell: bash
        run: |
          cd ./java-api-examples
          ./run-vad-remove-slience.sh
          rm *.onnx
          ls -lh *.wav
          rm *.wav

      - name:  Run java test (speaker identification)
        shell: bash
        run: |
          cd ./java-api-examples
          ./run-speaker-identification.sh
          # Delete model files to save space
          rm -rf *.onnx
          rm -rf sr-data

      - name:  Run java test (audio tagging)
        shell: bash
        run: |
          cd ./java-api-examples
          ./run-audio-tagging-zipformer-from-file.sh
          # Delete model files to save space
          rm -rf sherpa-onnx-zipformer-*

          ./run-audio-tagging-ced-from-file.sh
          rm -rf sherpa-onnx-ced-*

      - name:  Run java test (add punctuations)
        shell: bash
        run: |
          cd ./java-api-examples
          ./run-add-punctuation-zh-en.sh
          # Delete model files to save space
          rm -rf sherpa-onnx-punct-*

      - name:  Run java test (Spoken language identification)
        shell: bash
        run: |
          cd ./java-api-examples
          ./run-spoken-language-identification-whisper.sh
          # Delete model files to save space
          rm -rf sherpa-onnx-whisper-*

      - name:  Run java test (Streaming ASR)
        shell: bash
        run: |
          cd ./java-api-examples
          ./run-inverse-text-normalization-transducer.sh
          rm -rf sherpa-onnx-streaming-*

          ./run-streaming-decode-file-ctc.sh
          # Delete model files to save space
          rm -rf sherpa-onnx-streaming-*

          ./run-streaming-decode-file-ctc-hlg.sh
          rm -rf sherpa-onnx-streaming-*

          ./run-streaming-decode-file-paraformer.sh
          rm -rf sherpa-onnx-streaming-*

          ./run-streaming-decode-file-transducer.sh
          rm -rf sherpa-onnx-streaming-*

      - name:  Run java test (Non-Streaming ASR)
        shell: bash
        run: |
          cd ./java-api-examples

          ./run-non-streaming-decode-file-sense-voice.sh
          rm -rf sherpa-onnx-sense-voice-*

          ./run-inverse-text-normalization-paraformer.sh

          ./run-non-streaming-decode-file-paraformer.sh
          rm -rf sherpa-onnx-paraformer-zh-*

          ./run-non-streaming-decode-file-transducer.sh
          rm -rf sherpa-onnx-zipformer-*

          ./run-non-streaming-decode-file-whisper.sh
          rm -rf sherpa-onnx-whisper-*

          ./run-non-streaming-decode-file-nemo.sh
          rm -rf sherpa-onnx-nemo-*

      - name:  Run java test (Non-Streaming TTS)
        shell: bash
        run: |
          cd ./java-api-examples
          ./run-non-streaming-tts-piper-en.sh
          rm -rf vits-piper-*

          ./run-non-streaming-tts-coqui-de.sh
          rm -rf vits-coqui-*

          ./run-non-streaming-tts-vits-zh.sh
          rm -rf vits-zh-*

      - uses: actions/upload-artifact@v4
        with:
          name: tts-wav-files-${{ matrix.os }}
          path: java-api-examples/*.wav
