Skip to main content

Playback and WAV export

Generate and play

let result = try await tts.speak(
"This is generated and played locally.",
voice: .rosie,
speed: 1.0
)

speak() returns after playback completes and returns the generated result. On iOS, audio session activation can fail and surface as audioSessionFailed.

Stop playback

await tts.stopSpeaking()

Because KittenTTS is an actor, calls from outside its isolation use await even though stopSpeaking() itself is synchronous.

Encode WAV

let result = try await tts.generate("Save this audio.")
let wav: Data = result.wavData()

The encoder produces a standard RIFF WAV with 16-bit PCM, one channel, and a 24 kHz sample rate.

Write atomically

let destination = FileManager.default.temporaryDirectory
.appendingPathComponent("kitten.wav")

try result.writeWAV(to: destination)

writeWAV(to:) uses an atomic Data write and surfaces normal Cocoa file errors. Choose an app-owned documents, caches, or temporary directory appropriate to the file's lifetime.