# anyvoice-rs
Fast, unified async Text-to-Speech in Rust with native bindings for **Node.js / Bun / TypeScript** (`napi-rs`), **Python** (`PyO3`), and a **CLI** (`anyvoice`).
## Features
- **Multiple providers behind one typed interface:**
- **Microsoft Edge Neural TTS** (`edge`) — free, zero API key, native WebSocket + `Sec-MS-GEC` clock-skew handling + `WordBoundary` timings + in-process `symphonia` MP3→PCM decoding.
- **OpenAI & OpenAI-compatible** (`openai`) — `gpt-4o-mini-tts` (with delivery `instructions`), `tts-1`, `tts-1-hd`, streaming raw PCM, and custom `base_url` for local servers (Kokoro-FastAPI, Speaches, OpenedAI Speech).
- **ElevenLabs** (`elevenlabs`) — streaming 24 kHz PCM, native word timings (`/with-timestamps`), and live voice listing.
- **Google Gemini TTS** (`gemini`) — `gemini-2.5-flash-preview-tts` / `pro-tts`, natural-language delivery `instructions`, raw 24 kHz PCM, and 30 prebuilt voices (`Kore`, `Puck`, `Fenrir`, ...).
- **Cartesia Sonic** (`cartesia`) — `sonic-3` / `sonic-2` ultra-low-latency SSE PCM streaming + native word timestamps + live voice listing.
- **Deepgram Aura-2** (`deepgram`) — low-latency `linear16` 24 kHz HTTP PCM streaming.
- **Explicit audio formats & timings:** every `Audio` / `AudioChunk` carries `{ sample_rate, channels }`, `.duration()`, `.to_wav_bytes()`, `.convert_format()`, and `WordTiming` + WebVTT (`.to_vtt()`) export.
- **Pipelined `SpeechStream`:** prefetches upcoming sentences concurrently while playing strictly in order, with a smart `SentenceSplitter` that preserves decimals (`3.14`), versions (`v0.5.1`), URLs, and abbreviations (`Dr.`, `e.g.`).
- **Content-addressed disk cache (`CachedProvider`):** SHA-256 `.wav` + `.timings.json` cache for 0 ms / \$0 repeated runs (ideal for scripted demo videos and tests).
- **Optional native playback (`playback` feature):** cross-platform speaker output via `cpal` (no PortAudio required).
## Workspace
| `crates/anyvoice` | Pure Rust core library |
| `crates/anyvoice-cli` | `anyvoice` CLI (`synthesize`, `voices`, optional `speak`) |
| `crates/anyvoice-node` | `@phil65/anyvoice` native Node.js / Bun bindings (`napi-rs`) |
| `crates/anyvoice-py` | `anyvoice-rs` async Python bindings (`PyO3` + `maturin`) |
## Quick Start
### CLI
```bash
# Free Edge TTS -> WAV + WebVTT subtitles + JSON metadata
cargo run -p anyvoice-cli -- synthesize "FormCAD builds exact B-Rep solids in WebAssembly." \
-o hello.wav --vtt hello.vtt --json
# List English voices
cargo run -p anyvoice-cli -- voices --locale en-US
# OpenAI gpt-4o-mini-tts with style instructions + disk cache
OPENAI_API_KEY=... cargo run -p anyvoice-cli -- synthesize "Welcome to the demo." \
-p openai -v coral --instructions "Calm, crisp product demo narrator" \
--cache-dir .cache/voice -o demo.wav
```
### Rust
```rust
use std::sync::Arc;
use anyvoice::{CachedProvider, EdgeProvider, SynthesisRequest, TtsProvider};
#[tokio::main]
async fn main() -> anyvoice::Result<()> {
let provider = CachedProvider::new(EdgeProvider::new(), ".cache/voice");
let audio = provider
.synthesize(&SynthesisRequest::new("Hello from anyvoice-rs!").speed(1.1))
.await?;
audio.write_wav("out.wav")?;
println!("{} ms, {} word timings", audio.duration_ms(), audio.timings.len());
Ok(())
}
```
### TypeScript / Node.js (`@phil65/anyvoice`)
```ts
import { VoiceClient } from "@phil65/anyvoice";
const tts = VoiceClient.edge("en-US-EmmaMultilingualNeural").withCache(".cache/voice");
const audio = await tts.synthesize("FormCAD builds exact B-Rep solids in WebAssembly.");
console.log(audio.durationMs, audio.timings.length);
```
### Python (`anyvoice_rs`)
```python
import asyncio
from anyvoice_rs import VoiceClient
async def main():
tts = VoiceClient.edge().with_cache(".cache/voice")
audio = await tts.synthesize("Hello from Rust-backed Python!")
audio.write_wav("out.wav")
print(audio.duration_ms, len(audio.timings))
asyncio.run(main())
```
## License
MIT