Skip to content

Commit

Permalink
Add MeloTTS example for ios (#1223)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Aug 6, 2024
1 parent 6422966 commit 52830cc
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 82 deletions.
158 changes: 76 additions & 82 deletions build-ios.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,96 +39,90 @@ echo "SHERPA_ONNXRUNTIME_INCLUDE_DIR $SHERPA_ONNXRUNTIME_INCLUDE_DIR"
# The symbol _NSLog is not defined
#

if [[ ! -f ./build/simulator_x86_64/lib/libsherpa-onnx-c-api.a ]]; then
cmake \
-DBUILD_PIPER_PHONMIZE_EXE=OFF \
-DBUILD_PIPER_PHONMIZE_TESTS=OFF \
-DBUILD_ESPEAK_NG_EXE=OFF \
-DBUILD_ESPEAK_NG_TESTS=OFF \
-S .. \
-DCMAKE_TOOLCHAIN_FILE=./toolchains/ios.toolchain.cmake \
-DPLATFORM=SIMULATOR64 \
-DENABLE_BITCODE=0 \
-DENABLE_ARC=1 \
-DENABLE_VISIBILITY=0 \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=OFF \
-DSHERPA_ONNX_ENABLE_C_API=ON \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DDEPLOYMENT_TARGET=13.0 \
-B build/simulator_x86_64

cmake --build build/simulator_x86_64 -j 4 --verbose
fi
cmake \
-DBUILD_PIPER_PHONMIZE_EXE=OFF \
-DBUILD_PIPER_PHONMIZE_TESTS=OFF \
-DBUILD_ESPEAK_NG_EXE=OFF \
-DBUILD_ESPEAK_NG_TESTS=OFF \
-S .. \
-DCMAKE_TOOLCHAIN_FILE=./toolchains/ios.toolchain.cmake \
-DPLATFORM=SIMULATOR64 \
-DENABLE_BITCODE=0 \
-DENABLE_ARC=1 \
-DENABLE_VISIBILITY=0 \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=OFF \
-DSHERPA_ONNX_ENABLE_C_API=ON \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DDEPLOYMENT_TARGET=13.0 \
-B build/simulator_x86_64

cmake --build build/simulator_x86_64 -j 4 --verbose

echo "Building for simulator (arm64)"

if [[ ! -f ./build/simulator_arm64/lib/libsherpa-onnx-c-api.a ]]; then
cmake \
-DBUILD_PIPER_PHONMIZE_EXE=OFF \
-DBUILD_PIPER_PHONMIZE_TESTS=OFF \
-DBUILD_ESPEAK_NG_EXE=OFF \
-DBUILD_ESPEAK_NG_TESTS=OFF \
-S .. \
-DCMAKE_TOOLCHAIN_FILE=./toolchains/ios.toolchain.cmake \
-DPLATFORM=SIMULATORARM64 \
-DENABLE_BITCODE=0 \
-DENABLE_ARC=1 \
-DENABLE_VISIBILITY=0 \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=./install \
-DBUILD_SHARED_LIBS=OFF \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=OFF \
-DSHERPA_ONNX_ENABLE_C_API=ON \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DDEPLOYMENT_TARGET=13.0 \
-B build/simulator_arm64

cmake --build build/simulator_arm64 -j 4 --verbose
fi
cmake \
-DBUILD_PIPER_PHONMIZE_EXE=OFF \
-DBUILD_PIPER_PHONMIZE_TESTS=OFF \
-DBUILD_ESPEAK_NG_EXE=OFF \
-DBUILD_ESPEAK_NG_TESTS=OFF \
-S .. \
-DCMAKE_TOOLCHAIN_FILE=./toolchains/ios.toolchain.cmake \
-DPLATFORM=SIMULATORARM64 \
-DENABLE_BITCODE=0 \
-DENABLE_ARC=1 \
-DENABLE_VISIBILITY=0 \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=./install \
-DBUILD_SHARED_LIBS=OFF \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=OFF \
-DSHERPA_ONNX_ENABLE_C_API=ON \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DDEPLOYMENT_TARGET=13.0 \
-B build/simulator_arm64

cmake --build build/simulator_arm64 -j 4 --verbose

echo "Building for arm64"

export SHERPA_ONNXRUNTIME_LIB_DIR=$PWD/ios-onnxruntime/onnxruntime.xcframework/ios-arm64

if [[ ! -f ./build/os64/lib/libsherpa-onnx-c-api.a ]]; then
cmake \
-DBUILD_PIPER_PHONMIZE_EXE=OFF \
-DBUILD_PIPER_PHONMIZE_TESTS=OFF \
-DBUILD_ESPEAK_NG_EXE=OFF \
-DBUILD_ESPEAK_NG_TESTS=OFF \
-S .. \
-DCMAKE_TOOLCHAIN_FILE=./toolchains/ios.toolchain.cmake \
-DPLATFORM=OS64 \
-DENABLE_BITCODE=0 \
-DENABLE_ARC=1 \
-DENABLE_VISIBILITY=0 \
-DCMAKE_INSTALL_PREFIX=./install \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=OFF \
-DSHERPA_ONNX_ENABLE_C_API=ON \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DDEPLOYMENT_TARGET=13.0 \
-B build/os64

cmake --build build/os64 -j 4
# Generate headers for sherpa-onnx.xcframework
cmake --build build/os64 --target install
fi
cmake \
-DBUILD_PIPER_PHONMIZE_EXE=OFF \
-DBUILD_PIPER_PHONMIZE_TESTS=OFF \
-DBUILD_ESPEAK_NG_EXE=OFF \
-DBUILD_ESPEAK_NG_TESTS=OFF \
-S .. \
-DCMAKE_TOOLCHAIN_FILE=./toolchains/ios.toolchain.cmake \
-DPLATFORM=OS64 \
-DENABLE_BITCODE=0 \
-DENABLE_ARC=1 \
-DENABLE_VISIBILITY=0 \
-DCMAKE_INSTALL_PREFIX=./install \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=OFF \
-DSHERPA_ONNX_ENABLE_C_API=ON \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DDEPLOYMENT_TARGET=13.0 \
-B build/os64

cmake --build build/os64 -j 4
# Generate headers for sherpa-onnx.xcframework
cmake --build build/os64 --target install

echo "Generate xcframework"

Expand Down
38 changes: 38 additions & 0 deletions ios-swiftui/SherpaOnnxTts/SherpaOnnxTts/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,50 @@ func getTtsFor_en_US_amy_low() -> SherpaOnnxOfflineTtsWrapper {
return SherpaOnnxOfflineTtsWrapper(config: &config)
}

// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/vits.html#vits-melo-tts-zh-en-chinese-english-1-speaker
func getTtsFor_zh_en_melo_tts() -> SherpaOnnxOfflineTtsWrapper {
// please see https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-melo-tts-zh_en.tar.bz2

let model = getResource("model", "onnx")

let tokens = getResource("tokens", "txt")
let lexicon = getResource("lexicon", "txt")

let dictDir = resourceURL(to: "dict")

let numFst = getResource("number", "fst")
let dateFst = getResource("date", "fst")
let phoneFst = getResource("phone", "fst")
let ruleFsts = "\(dateFst),\(phoneFst),\(numFst)"

let vits = sherpaOnnxOfflineTtsVitsModelConfig(
model: model, lexicon: lexicon, tokens: tokens,
dataDir: "",
noiseScale: 0.667,
noiseScaleW: 0.8,
lengthScale: 1.0,
dictDir: dictDir
)

let modelConfig = sherpaOnnxOfflineTtsModelConfig(vits: vits)
var config = sherpaOnnxOfflineTtsConfig(
model: modelConfig,
ruleFsts: ruleFsts
)

return SherpaOnnxOfflineTtsWrapper(config: &config)
}

func createOfflineTts() -> SherpaOnnxOfflineTtsWrapper {
// Please enable only one of them

return getTtsFor_en_US_amy_low()

// return getTtsForVCTK()

// return getTtsForAishell3()

// return getTtsFor_zh_en_melo_tts()

// please add more models on need by following the above two examples
}

0 comments on commit 52830cc

Please sign in to comment.