-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Swift API for Kokoro TTS models (#1721)
- Loading branch information
1 parent
cc812e6
commit ad61ad6
Showing
8 changed files
with
134 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ keyword-spotting-from-file | |
add-punctuations | ||
tts-matcha-zh | ||
tts-matcha-en | ||
tts-kokoro-en |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -ex | ||
|
||
if [ ! -d ../build-swift-macos ]; then | ||
echo "Please run ../build-swift-macos.sh first!" | ||
exit 1 | ||
fi | ||
|
||
# please visit | ||
# https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/kokoro.html | ||
# to download more models | ||
if [ ! -f ./kokoro-en-v0_19/model.onnx ]; then | ||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kokoro-en-v0_19.tar.bz2 | ||
tar xf kokoro-en-v0_19.tar.bz2 | ||
rm kokoro-en-v0_19.tar.bz2 | ||
fi | ||
|
||
if [ ! -e ./tts-kokoro-en ]; then | ||
# Note: We use -lc++ to link against libc++ instead of libstdc++ | ||
swiftc \ | ||
-lc++ \ | ||
-I ../build-swift-macos/install/include \ | ||
-import-objc-header ./SherpaOnnx-Bridging-Header.h \ | ||
./tts-kokoro-en.swift ./SherpaOnnx.swift \ | ||
-L ../build-swift-macos/install/lib/ \ | ||
-l sherpa-onnx \ | ||
-l onnxruntime \ | ||
-o tts-kokoro-en | ||
|
||
strip tts-kokoro-en | ||
else | ||
echo "./tts-kokoro-en exists - skip building" | ||
fi | ||
|
||
export DYLD_LIBRARY_PATH=$PWD/../build-swift-macos/install/lib:$DYLD_LIBRARY_PATH | ||
./tts-kokoro-en |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
class MyClass { | ||
func playSamples(samples: [Float]) { | ||
print("Play \(samples.count) samples") | ||
} | ||
} | ||
|
||
func run() { | ||
let model = "./kokoro-en-v0_19/model.onnx" | ||
let voices = "./kokoro-en-v0_19/voices.bin" | ||
let tokens = "./kokoro-en-v0_19/tokens.txt" | ||
let dataDir = "./kokoro-en-v0_19/espeak-ng-data" | ||
let kokoro = sherpaOnnxOfflineTtsKokoroModelConfig( | ||
model: model, | ||
voices: voices, | ||
tokens: tokens, | ||
dataDir: dataDir | ||
) | ||
let modelConfig = sherpaOnnxOfflineTtsModelConfig(kokoro: kokoro, debug: 0) | ||
var ttsConfig = sherpaOnnxOfflineTtsConfig(model: modelConfig) | ||
|
||
let myClass = MyClass() | ||
|
||
// We use Unretained here so myClass must be kept alive as the callback is invoked | ||
// | ||
// See also | ||
// https://medium.com/codex/swift-c-callback-interoperability-6d57da6c8ee6 | ||
let arg = Unmanaged<MyClass>.passUnretained(myClass).toOpaque() | ||
|
||
let callback: TtsCallbackWithArg = { samples, n, arg in | ||
let o = Unmanaged<MyClass>.fromOpaque(arg!).takeUnretainedValue() | ||
var savedSamples: [Float] = [] | ||
for index in 0..<n { | ||
savedSamples.append(samples![Int(index)]) | ||
} | ||
|
||
o.playSamples(samples: savedSamples) | ||
|
||
// return 1 so that it continues generating | ||
return 1 | ||
} | ||
|
||
let tts = SherpaOnnxOfflineTtsWrapper(config: &ttsConfig) | ||
|
||
let text = | ||
"Friends fell out often because life was changing so fast. The easiest thing in the world was to lose touch with someone." | ||
let sid = 0 | ||
let speed: Float = 1.0 | ||
|
||
let audio = tts.generateWithCallbackWithArg( | ||
text: text, callback: callback, arg: arg, sid: sid, speed: speed) | ||
let filename = "test-kokoro-en.wav" | ||
let ok = audio.save(filename: filename) | ||
if ok == 1 { | ||
print("\nSaved to:\(filename)") | ||
} else { | ||
print("Failed to save to \(filename)") | ||
} | ||
} | ||
|
||
@main | ||
struct App { | ||
static func main() { | ||
run() | ||
} | ||
} |