name: pascal

on:
  push:
    branches:
      - master
    paths:
      - '.github/workflows/pascal.yaml'
      - 'CMakeLists.txt'
      - 'cmake/**'
      - 'pascal-api-examples/**'
      - 'sherpa-onnx/csrc/*'
      - 'sherpa-onnx/c-api/*'
      - 'sherpa-onnx/pascal-api/*'
  pull_request:
    branches:
      - master
    paths:
      - '.github/workflows/pascal.yaml'
      - 'CMakeLists.txt'
      - 'cmake/**'
      - 'pascal-api-examples/**'
      - 'sherpa-onnx/csrc/*'
      - 'sherpa-onnx/c-api/*'
      - 'sherpa-onnx/pascal-api/*'

  workflow_dispatch:

concurrency:
  group: pascal-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

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

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

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

      - name: Install Free pascal compiler (ubuntu)
        if: matrix.os == 'ubuntu-latest'
        shell: bash
        run: |
          sudo apt-get update
          sudo apt-get install -q -y fpc

      - name: Install Free pascal compiler (macos)
        if: matrix.os == 'macos-latest' || matrix.os == 'macos-13'
        shell: bash
        run: |
          brew install fpc
          # brew install --cask lazarus
          #
      - name: Install Free pascal compiler (windows)
        if: matrix.os == 'windows-latest'
        shell: bash
        run: |
          choco install lazarus

          ls -lh /c/lazarus/fpc/3.2.2/bin/x86_64-win64/

      - name: FPC info
        shell: bash
        run: |
          export PATH=/c/lazarus/fpc/3.2.2/bin/x86_64-win64:$PATH
          which fpc
          fpc -i

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

      - name: Configure CMake
        shell: bash
        run: |
          export CMAKE_CXX_COMPILER_LAUNCHER=ccache
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          cmake --version

          mkdir build
          cd build

          cmake \
            -DCMAKE_INSTALL_PREFIX=./install \
            -D BUILD_SHARED_LIBS=ON \
            -D SHERPA_ONNX_ENABLE_BINARY=OFF \
            -D CMAKE_BUILD_TYPE=Release \
            ..

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

          cd build
          cmake --build . --target install --config Release

          ls -lh install/lib/

          if [[ ${{ matrix.os }} == 'windows-latest' ]]; then
            cp -v install/lib/*.dll ../pascal-api-examples/read-wav
            cp -v install/lib/*.dll ../pascal-api-examples/streaming-asr
            cp -v install/lib/*.dll ../pascal-api-examples/non-streaming-asr
            cp -v install/lib/*.dll ../pascal-api-examples/vad
            cp -v install/lib/*.dll ../pascal-api-examples/vad-with-non-streaming-asr

            cp -v ../sherpa-onnx/pascal-api/*.pas ../pascal-api-examples/read-wav
            cp -v ../sherpa-onnx/pascal-api/*.pas ../pascal-api-examples/streaming-asr
            cp -v ../sherpa-onnx/pascal-api/*.pas ../pascal-api-examples/non-streaming-asr
            cp -v ../sherpa-onnx/pascal-api/*.pas ../pascal-api-examples/vad
            cp -v ../sherpa-onnx/pascal-api/*.pas ../pascal-api-examples/vad-with-non-streaming-asr
            cp -v ../sherpa-onnx/pascal-api/*.pas ../pascal-api-examples/tts
          fi

      - name:  Run Pascal test (Speaker diarization)
        shell: bash
        run: |
          export PATH=/c/lazarus/fpc/3.2.2/bin/x86_64-win64:$PATH

          cd ./pascal-api-examples
          pushd speaker-diarization

          ./run.sh
          rm -rfv *.onnx *.wav sherpa-onnx-*
          ls -lh
          echo "---"

          popd

      - name:  Run Pascal test (TTS)
        shell: bash
        run: |
          export PATH=/c/lazarus/fpc/3.2.2/bin/x86_64-win64:$PATH

          cd ./pascal-api-examples
          pushd tts

          ./run-piper.sh
          rm -rf vits-piper-*
          ls -lh
          echo "---"

          popd

      - name:  Run Pascal test (VAD + non-streaming ASR)
        shell: bash
        run: |
          export PATH=/c/lazarus/fpc/3.2.2/bin/x86_64-win64:$PATH

          cd ./pascal-api-examples

          pushd vad-with-non-streaming-asr
          time ./run-vad-with-moonshine.sh
          rm -rf sherpa-onnx-*
          echo "---"

          time ./run-vad-with-whisper.sh
          rm -rf sherpa-onnx-*
          echo "---"

          time ./run-vad-with-sense-voice.sh
          rm -rf sherpa-onnx-*
          echo "---"

          ls -lh

          popd

      - name:  Run Pascal test (VAD test)
        shell: bash
        run: |
          export PATH=/c/lazarus/fpc/3.2.2/bin/x86_64-win64:$PATH

          cd ./pascal-api-examples

          pushd vad
          ./run-circular-buffer.sh
          echo "---"

          time ./run-remove-silence.sh
          echo "---"

          ls -lh

          popd

      - name:  Run Pascal test (Read wav test)
        shell: bash
        run: |
          export PATH=/c/lazarus/fpc/3.2.2/bin/x86_64-win64:$PATH

          cd ./pascal-api-examples

          pushd read-wav
          ./run.sh
          echo "---"
          ls -lh
          popd

      - name:  Run Pascal test (Non Streaming ASR)
        shell: bash
        run: |
          export PATH=/c/lazarus/fpc/3.2.2/bin/x86_64-win64:$PATH

          cd ./pascal-api-examples

          pushd non-streaming-asr
          ./run-zipformer-transducer.sh
          rm -rf sherpa-onnx-*
          echo "---"

          ./run-moonshine.sh
          rm -rf sherpa-onnx-*
          echo "---"

          ./run-whisper.sh
          rm -rf sherpa-onnx-*
          echo "---"

          ./run-nemo-transducer.sh
          rm -rf sherpa-onnx-*
          echo "---"

          ./run-nemo-ctc.sh
          rm -rf sherpa-onnx-*
          echo "---"

          ./run-sense-voice.sh
          rm -rf sherpa-onnx-*
          echo "---"

          ./run-telespeech-ctc.sh
          rm -rf sherpa-onnx-*
          echo "---"

          ./run-paraformer.sh

          ./run-paraformer-itn.sh

          rm -rf sherpa-onnx-*
          echo "---"

          ls -lh
          popd

      - name:  Run Pascal test (Streaming ASR)
        shell: bash
        run: |
          export PATH=/c/lazarus/fpc/3.2.2/bin/x86_64-win64:$PATH

          cd ./pascal-api-examples

          pushd streaming-asr

          ./run-zipformer-transducer.sh
          rm -rf sherpa-onnx-*
          echo "---"

          ./run-nemo-transducer.sh
          rm -rf sherpa-onnx-*
          echo "---"

          if [[ ${{ matrix.os }} != 'windows-latest' ]]; then
            ./run-paraformer.sh
            rm -rf sherpa-onnx-*
            echo "---"

            ./run-zipformer-ctc.sh
            echo "---"

            ./run-zipformer-ctc-hlg.sh
            rm -rf sherpa-onnx-*
            echo "---"
          fi

          ls -lh
          popd
