Swift implementation of the Piper speech synthesis engine. While the core is now written in Swift, it maintains full compatibility with Objective‑C.
| Platform | Minimum |
|---|---|
| iOS | 13.0 |
| macOS | 10.15 |
dependencies: [
.package(url: "https://github.com/IhorShevchuk/piper-objc.git", from: "0.2.15")
]Two library products are available:
- piper-objc — Core engine and low‑level Swift/Objective‑C API.
- piper-player — High‑level Swift player built on AVFoundation.
import piper_player
let params = PiperPlayer.Params(
modelPath: "/path/to/model.onnx",
configPath: "/path/to/model.onnx.json",
espeakNGData: "/path/to/espeak-ng-data" // optional
)
let player = try PiperPlayer(params: params)try await player.play(text: "Hello, world!")try await player.play(ssml: "<speak>Hello</speak>")
// With a specific speaker (for multi-speaker models)
try await player.play(ssml: "<speak>Hello</speak>", speakerId: 1)Returns the path to the generated .wav file, useful when you need custom playback (e.g. pitch shifting, speed control via AVAudioEngine).
// From plain text
if let path = await player.synthesizeToFile(text: "Hello, world!") {
// use the .wav file at `path`
}
// From SSML
if let path = await player.synthesizeSSMLToFile(ssml: "<speak>Hello</speak>", speakerId: 0) {
// use the .wav file at `path`
}await player.stopAndCancel()#import <piper_objc/piper_objc.h>
Piper *piper = [[Piper alloc] initWithModelPath:@"model.onnx"
configPath:@"model.onnx.json"
espeakNGData:@""];
// Synthesize text to file
[piper synthesize:@"Hello" toFileAtPath:@"/tmp/out.wav" completion:^{
// playback or processing
}];
// Synthesize SSML to file
[piper synthesizeSSML:@"<speak>Hello</speak>"
speakerId:0
toFileAtPath:@"/tmp/out.wav"
completion:^{
// playback or processing
}];GPL-2.0