Skip to content

Commit

Permalink
TTS API for Object Pascal.
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Aug 20, 2024
1 parent e34a1a2 commit 4ab24c0
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 18 deletions.
24 changes: 19 additions & 5 deletions .github/workflows/pascal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,27 @@ jobs:
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/sherpa_onnx.pas ../pascal-api-examples/read-wav
cp -v ../sherpa-onnx/pascal-api/sherpa_onnx.pas ../pascal-api-examples/streaming-asr
cp -v ../sherpa-onnx/pascal-api/sherpa_onnx.pas ../pascal-api-examples/non-streaming-asr
cp -v ../sherpa-onnx/pascal-api/sherpa_onnx.pas ../pascal-api-examples/vad
cp -v ../sherpa-onnx/pascal-api/sherpa_onnx.pas ../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 (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
popd
rm -rf vits-piper-*
ls -lh
echo "---"
- name: Run Pascal test (VAD + non-streaming ASR)
shell: bash
run: |
Expand Down
2 changes: 2 additions & 0 deletions pascal-api-examples/tts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!run-*.sh
piper
55 changes: 55 additions & 0 deletions pascal-api-examples/tts/piper.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{ Copyright (c) 2024 Xiaomi Corporation }
program piper;
{
This file shows how to use the text to speech API of sherpa-onnx
with Piper models.
It generates speech fro text and saves it to a wave file.
If you want to play it while it is generating, please see
./piper-playback.pas
}

{$mode objfpc}
{$ASSERTIONS ON}

uses
SysUtils,
sherpa_onnx;

function GetOfflineTts: TSherpaOnnxOfflineTts;
var
Config: TSherpaOnnxOfflineTtsConfig;
begin
Config.Model.Vits.Model := './vits-piper-en_US-libritts_r-medium/en_US-libritts_r-medium.onnx';
Config.Model.Vits.Tokens := './vits-piper-en_US-libritts_r-medium/tokens.txt';
Config.Model.Vits.DataDir := './vits-piper-en_US-libritts_r-medium/espeak-ng-data';
Config.Model.NumThreads := 1;
Config.Model.Debug := False;
Config.MaxNumSentences := 1;

Result := TSherpaOnnxOfflineTts.Create(Config);
end;

var
Tts: TSherpaOnnxOfflineTts;
Audio: TSherpaOnnxGeneratedAudio;

Text: AnsiString;
Speed: Single = 1.0; {Use a large value to speak faster}
SpeakerId: Integer = 0;

begin
Tts := GetOfflineTts;

WriteLn('There are ', Tts.GetNumSpeakers, ' speakers');

Text := 'Today as always, men fall into two groups: slaves and free men. Whoever does not have two-thirds of his day for himself, is a slave, whatever he may be: a statesman, a businessman, an official, or a scholar.';

Audio := Tts.Generate(Text, SpeakerId, Speed);
SherpaOnnxWriteWave('./libritts_r-generated.wav', Audio.Samples, Audio.SampleRate);
WriteLn('Saved to ./libritts_r-generated.wav');

FreeAndNil(Tts);
end.

41 changes: 41 additions & 0 deletions pascal-api-examples/tts/run-piper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

set -ex

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SHERPA_ONNX_DIR=$(cd $SCRIPT_DIR/../.. && pwd)

echo "SHERPA_ONNX_DIR: $SHERPA_ONNX_DIR"

if [[ ! -f ../../build/install/lib/libsherpa-onnx-c-api.dylib && ! -f ../../build/install/lib/libsherpa-onnx-c-api.so && ! -f ../../build/install/lib/sherpa-onnx-c-api.dll ]]; then
mkdir -p ../../build
pushd ../../build
cmake \
-DCMAKE_INSTALL_PREFIX=./install \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DBUILD_SHARED_LIBS=ON \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
..

cmake --build . --target install --config Release
popd
fi

if [[ ! -f ./vits-piper-en_US-libritts_r-medium/tokens.txt ]]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-en_US-libritts_r-medium.tar.bz2
tar xf vits-piper-en_US-libritts_r-medium.tar.bz2
rm vits-piper-en_US-libritts_r-medium.tar.bz2
fi

fpc \
-dSHERPA_ONNX_USE_SHARED_LIBS \
-Fu$SHERPA_ONNX_DIR/sherpa-onnx/pascal-api \
-Fl$SHERPA_ONNX_DIR/build/install/lib \
./piper.pas

export LD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/install/lib:$LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/install/lib:$DYLD_LIBRARY_PATH

./piper
6 changes: 3 additions & 3 deletions scripts/apk/generate-tts-apk-script.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ def get_piper_models() -> List[TtsModel]:
TtsModel(model_dir="vits-piper-nl_BE-nathalie-x_low"),
TtsModel(model_dir="vits-piper-nl_BE-rdh-medium"),
TtsModel(model_dir="vits-piper-nl_BE-rdh-x_low"),
TtsModel(model_dir="vits-piper-nl_NL-mls-medium"),
TtsModel(model_dir="vits-piper-nl_NL-mls_5809-low"),
TtsModel(model_dir="vits-piper-nl_NL-mls_7432-low"),
# TtsModel(model_dir="vits-piper-nl_NL-mls-medium"),
# TtsModel(model_dir="vits-piper-nl_NL-mls_5809-low"),
# TtsModel(model_dir="vits-piper-nl_NL-mls_7432-low"),
TtsModel(model_dir="vits-piper-no_NO-talesyntese-medium"),
TtsModel(model_dir="vits-piper-pl_PL-darkman-medium"),
TtsModel(model_dir="vits-piper-pl_PL-gosia-medium"),
Expand Down
6 changes: 3 additions & 3 deletions scripts/flutter/generate-tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ def get_piper_models() -> List[TtsModel]:
TtsModel(model_dir="vits-piper-nl_BE-nathalie-x_low"),
TtsModel(model_dir="vits-piper-nl_BE-rdh-medium"),
TtsModel(model_dir="vits-piper-nl_BE-rdh-x_low"),
TtsModel(model_dir="vits-piper-nl_NL-mls-medium"),
TtsModel(model_dir="vits-piper-nl_NL-mls_5809-low"),
TtsModel(model_dir="vits-piper-nl_NL-mls_7432-low"),
# TtsModel(model_dir="vits-piper-nl_NL-mls-medium"),
# TtsModel(model_dir="vits-piper-nl_NL-mls_5809-low"),
# TtsModel(model_dir="vits-piper-nl_NL-mls_7432-low"),
TtsModel(model_dir="vits-piper-no_NO-talesyntese-medium"),
TtsModel(model_dir="vits-piper-pl_PL-darkman-medium"),
TtsModel(model_dir="vits-piper-pl_PL-gosia-medium"),
Expand Down
Loading

0 comments on commit 4ab24c0

Please sign in to comment.