The Text-to-Speech (TTS) API converts text to spoken audio using on-device neural voice synthesis with Piper TTS. All synthesis happens locally on the device.
// Slower and lower pitchconst slow = await RunAnywhere.synthesize('This is spoken slowly with a lower pitch.', { rate: 0.75, pitch: 0.8, volume: 1.0,})// Faster and higher pitchconst fast = await RunAnywhere.synthesize('This is spoken quickly with a higher pitch!', { rate: 1.5, pitch: 1.2, volume: 1.0,})
For simpler playback using platform’s built-in TTS:
// Use system TTS (AVSpeechSynthesizer on iOS, Android TTS)await RunAnywhere.speak('Hello from system TTS!', { rate: 1.0, pitch: 1.0, volume: 1.0,})// Check if currently speakingconst speaking = await RunAnywhere.isSpeaking()// Stop playbackawait RunAnywhere.stopSpeaking()
The synthesized audio is base64-encoded float32 PCM. Use the SDK’s Audio utility to convert and play:
import { RunAnywhere } from '@runanywhere/core'// Convert to WAV and play using SDK's Audio utilityasync function playTTSResult(result: TTSResult) { // Convert float32 PCM to playable WAV format const wavBase64 = RunAnywhere.Audio.createWavFromPCMFloat32(result.audio, result.sampleRate) // Play the audio await RunAnywhere.Audio.playAudio(wavBase64)}// Stop playback if neededawait RunAnywhere.Audio.stopPlayback()
AudioContext / Web Audio API does not work in React Native. Always use
RunAnywhere.Audio.createWavFromPCMFloat32() to convert TTS output before playback.
import { isSDKError, SDKErrorCode } from '@runanywhere/core'try { const result = await RunAnywhere.synthesize(text)} catch (error) { if (isSDKError(error)) { switch (error.code) { case SDKErrorCode.notInitialized: console.error('SDK not initialized') break case SDKErrorCode.modelNotLoaded: console.error('Load a TTS model first') break case SDKErrorCode.ttsFailed: console.error('Synthesis failed:', error.message) break } }}