|
| 1 | +import Foundation |
| 2 | + |
| 3 | +class SupertonicTtsProgressHandler { |
| 4 | + func progress(samples: [Float], progress: Float) { |
| 5 | + print(String(format: "Received %d samples, Progress: %.2f%%", samples.count, progress * 100)) |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +func runSupertonicTtsDemo() { |
| 10 | + let supertonic = sherpaOnnxOfflineTtsSupertonicModelConfig( |
| 11 | + durationPredictor: "./sherpa-onnx-supertonic-tts-int8-2026-03-06/duration_predictor.int8.onnx", |
| 12 | + textEncoder: "./sherpa-onnx-supertonic-tts-int8-2026-03-06/text_encoder.int8.onnx", |
| 13 | + vectorEstimator: "./sherpa-onnx-supertonic-tts-int8-2026-03-06/vector_estimator.int8.onnx", |
| 14 | + vocoder: "./sherpa-onnx-supertonic-tts-int8-2026-03-06/vocoder.int8.onnx", |
| 15 | + ttsJson: "./sherpa-onnx-supertonic-tts-int8-2026-03-06/tts.json", |
| 16 | + unicodeIndexer: "./sherpa-onnx-supertonic-tts-int8-2026-03-06/unicode_indexer.bin", |
| 17 | + voiceStyle: "./sherpa-onnx-supertonic-tts-int8-2026-03-06/voice.bin" |
| 18 | + ) |
| 19 | + |
| 20 | + let modelConfig = sherpaOnnxOfflineTtsModelConfig(numThreads: 2, supertonic: supertonic) |
| 21 | + var ttsConfig = sherpaOnnxOfflineTtsConfig(model: modelConfig) |
| 22 | + ttsConfig.model.debug = 1 |
| 23 | + |
| 24 | + let tts = SherpaOnnxOfflineTtsWrapper(config: &ttsConfig) |
| 25 | + |
| 26 | + var genConfig = SherpaOnnxGenerationConfigSwift() |
| 27 | + genConfig.sid = 6 |
| 28 | + genConfig.numSteps = 5 |
| 29 | + genConfig.speed = 1.25 |
| 30 | + genConfig.extra = ["lang": "en"] |
| 31 | + |
| 32 | + let text = |
| 33 | + "Today as always, men fall into two groups: slaves and free men. Whoever " |
| 34 | + + "does not have two-thirds of his day for himself, is a slave, whatever " |
| 35 | + + "he may be: a statesman, a businessman, an official, or a scholar." |
| 36 | + |
| 37 | + func generateAndSave( |
| 38 | + outputFile: String, callback: TtsProgressCallbackWithArg? = nil, |
| 39 | + arg: UnsafeMutableRawPointer? = nil |
| 40 | + ) { |
| 41 | + let audio = tts.generateWithConfig( |
| 42 | + text: text, |
| 43 | + config: genConfig, |
| 44 | + callback: callback, |
| 45 | + arg: arg |
| 46 | + ) |
| 47 | + |
| 48 | + if audio.save(filename: outputFile) == 1 { |
| 49 | + print("Saved to: \(outputFile)") |
| 50 | + } else { |
| 51 | + print("Failed to save to \(outputFile)") |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // ------------------------- |
| 56 | + // Option 1: with callback |
| 57 | + // ------------------------- |
| 58 | + let useCallback = true |
| 59 | + if useCallback { |
| 60 | + let progressHandler = SupertonicTtsProgressHandler() |
| 61 | + let arg = Unmanaged.passUnretained(progressHandler).toOpaque() |
| 62 | + |
| 63 | + let callback: TtsProgressCallbackWithArg = { samples, n, progress, arg in |
| 64 | + let handler = Unmanaged<SupertonicTtsProgressHandler>.fromOpaque(arg!).takeUnretainedValue() |
| 65 | + |
| 66 | + let buffer: [Float] = |
| 67 | + samples != nil ? Array(UnsafeBufferPointer(start: samples, count: Int(n))) : [] |
| 68 | + handler.progress(samples: buffer, progress: progress) |
| 69 | + return 1 // continue generating |
| 70 | + } |
| 71 | + |
| 72 | + generateAndSave(outputFile: "generated-supertonic-callback.wav", callback: callback, arg: arg) |
| 73 | + } else { |
| 74 | + // ------------------------- |
| 75 | + // Option 2: direct generation |
| 76 | + // ------------------------- |
| 77 | + generateAndSave(outputFile: "generated-supertonic-direct.wav") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// ------------------------- |
| 82 | +// Run demo |
| 83 | +// ------------------------- |
| 84 | +@main |
| 85 | +struct App { |
| 86 | + static func main() { |
| 87 | + runSupertonicTtsDemo() |
| 88 | + } |
| 89 | +} |
0 commit comments