Skip to content

Commit

Permalink
Support passing TTS callback in Swift API (#1218)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Aug 5, 2024
1 parent 9caa488 commit 6422966
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
19 changes: 19 additions & 0 deletions swift-api-examples/SherpaOnnx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,14 @@ class SherpaOnnxGeneratedAudioWrapper {
}
}

typealias TtsCallbackWithArg = (
@convention(c) (
UnsafePointer<Float>?, // const float* samples
Int32, // int32_t n
UnsafeMutableRawPointer? // void *arg
) -> Int32
)?

class SherpaOnnxOfflineTtsWrapper {
/// A pointer to the underlying counterpart in C
let tts: OpaquePointer!
Expand All @@ -780,6 +788,17 @@ class SherpaOnnxOfflineTtsWrapper {

return SherpaOnnxGeneratedAudioWrapper(audio: audio)
}

func generateWithCallbackWithArg(
text: String, callback: TtsCallbackWithArg, arg: UnsafeMutableRawPointer, sid: Int = 0,
speed: Float = 1.0
) -> SherpaOnnxGeneratedAudioWrapper {
let audio: UnsafePointer<SherpaOnnxGeneratedAudio>? =
SherpaOnnxOfflineTtsGenerateWithCallbackWithArg(
tts, toCPointer(text), Int32(sid), speed, callback, arg)

return SherpaOnnxGeneratedAudioWrapper(audio: audio)
}
}

// spoken language identification
Expand Down
39 changes: 35 additions & 4 deletions swift-api-examples/tts.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
class MyClass {
func playSamples(samples: [Float]) {
print("Play \(samples.count) samples")
}
}

func run() {
let model = "./vits-piper-en_US-amy-low/en_US-amy-low.onnx"
let tokens = "./vits-piper-en_US-amy-low/tokens.txt"
Expand All @@ -11,18 +17,43 @@ func run() {
let modelConfig = sherpaOnnxOfflineTtsModelConfig(vits: vits)
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 =
"“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.”"
let sid = 99
let speed: Float = 1.0

let audio = tts.generate(text: text, sid: sid, speed: speed)
let audio = tts.generateWithCallbackWithArg(
text: text, callback: callback, arg: arg, sid: sid, speed: speed)
let filename = "test.wav"
audio.save(filename: filename)

print("\nSaved to:\n\(filename)")
let ok = audio.save(filename: filename)
if ok == 1 {
print("\nSaved to:\(filename)")
} else {
print("Failed to save to \(filename)")
}
}

@main
Expand Down

0 comments on commit 6422966

Please sign in to comment.