AnyVoice
AnyVoice is a small async library that puts streaming Text-to-Speech providers behind one interface. Feed it text as it arrives (for example token by token from an LLM), and it buffers the text into sentences, synthesizes them and sends raw PCM audio to a pluggable sink: your speakers, a WAV file or your own callback.
Features
- Multiple TTS providers: OpenAI TTS and Microsoft Edge TTS (free, no API key)
- Pluggable audio sinks: speaker playback, WAV file output, custom callbacks
- Streaming input: incremental
feed()with sentence-boundary buffering - Synchronization modes: choose whether
feed()waits for audio or synthesis runs in the background - Lightweight: no required dependencies; provider libraries are optional extras and imported lazily
Installation
# OpenAI TTS (needs an OpenAI API key)
pip install "anyvoice[openai]"
# Edge TTS (free, no API key)
pip install "anyvoice[tts-edge]"
Speaker playback uses sounddevice, which needs the PortAudio system library.
Quick Start
Basic usage with OpenAI
import asyncio
from anyvoice import OpenAITTSProvider, SoundDeviceSink, TTSStream
async def main():
provider = OpenAITTSProvider() # uses OPENAI_API_KEY, or pass api_key="sk-..."
session = provider.session(voice="nova", speed=1.0)
async with TTSStream(session, sink=SoundDeviceSink()) as tts:
await tts.feed("Hello! ")
await tts.feed("This is streaming TTS. ")
await tts.feed("Pretty cool, right?")
asyncio.run(main())
Using Edge TTS (free, no API key)
import asyncio
from anyvoice import EdgeTTSProvider, SoundDeviceSink, TTSStream
async def main():
provider = EdgeTTSProvider()
session = provider.session(voice="en-GB-SoniaNeural", rate="+10%")
async with TTSStream(session, sink=SoundDeviceSink()) as tts:
await tts.feed("Hello from Edge TTS!")
asyncio.run(main())
Streaming from an LLM
Any async iterator of text chunks works:
from collections.abc import AsyncIterator
from anyvoice import OpenAITTSProvider, TTSStream
async def speak(chunks: AsyncIterator[str]):
session = OpenAITTSProvider().session(voice="nova")
async with TTSStream(session, mode="sync_run") as tts: # plays through the speakers
async for chunk in chunks:
print(chunk, end="", flush=True)
await tts.feed(chunk)
# Audio has finished playing when the context exits
Save to a file
from anyvoice import EdgeTTSProvider, FileSink, TTSStream
async def save_audio():
session = EdgeTTSProvider().session()
async with TTSStream(session, sink=FileSink("output.wav")) as tts:
await tts.feed("This will be saved to a WAV file.")
Custom audio handling
from anyvoice import CallbackSink, OpenAITTSProvider, TTSStream
async def custom_handler():
chunks: list[bytes] = []
session = OpenAITTSProvider().session(voice="echo")
async with TTSStream(session, sink=CallbackSink(chunks.append)) as tts:
await tts.feed("Process this audio yourself!")
total_bytes = sum(len(c) for c in chunks)
print(f"Received {total_bytes} bytes of audio")
Callbacks may be sync or async. Audio is always 16-bit signed mono PCM (24 kHz by default).
Using a session directly
Skip the orchestrator if you just want the audio chunks:
from anyvoice import EdgeTTSProvider
async def synthesize():
session = EdgeTTSProvider().session()
async for pcm_chunk in session.synthesize("Hello world."):
...
Synchronization Modes
Text is buffered until a sentence terminator (., !, ?, newline) appears after at least
min_text_length characters; the remainder is synthesized when the context exits.
| Mode | Behavior |
|---|---|
sync_sentence |
feed() waits until each sentence has been synthesized and written to the sink |
sync_run |
feed() returns immediately, sentences are synthesized in the background; the context exit waits for all audio (default) |
async_queue |
Like sync_run, but the context exit does not wait: pending audio plays in the background, then the session and sink close (await tts.wait_closed() waits for that) |
To drop pending audio (in any mode, e.g. when a new answer replaces the old one), call
await tts.cancel(). An exception or a cancellation inside the context drops the pending audio in every mode. If
background synthesis fails, the error is raised from the next feed() or the context exit
(wait_closed() in the async modes).
async with TTSStream(session, mode="sync_sentence") as tts:
await tts.feed("This sentence plays completely.")
# Audio finished before the next feed()
await tts.feed("Then this one plays.")
API Overview
Providers
OpenAITTSProvider
provider = OpenAITTSProvider(api_key="...", base_url=None)
session = provider.session(
model="tts-1", # or "tts-1-hd"
voice="alloy", # alloy, echo, fable, onyx, nova, shimmer
speed=1.0, # 0.25 to 4.0
chunk_size=1024,
)
EdgeTTSProvider
provider = EdgeTTSProvider(default_voice="en-US-AriaNeural", sample_rate=24000)
session = provider.session(
voice="en-US-AriaNeural", # list voices with `edge-tts --list-voices`
rate="+0%", # e.g., "+25%", "-10%"
volume="+0%",
pitch="+0Hz",
)
Sinks
SoundDeviceSink(sample_rate=24000, channels=1): play through the speakersFileSink(path, sample_rate=24000, channels=1): save to a WAV fileCallbackSink(callback, on_close=None): custom handling
Implement your own by subclassing anyvoice.AudioSink and defining async write(chunk).
New providers subclass TTSProvider and TTSSession.
TTSStream
TTSStream(
session, # From provider.session()
sink=SoundDeviceSink(), # Audio output (default: SoundDeviceSink)
mode="sync_run", # Synchronization mode
min_text_length=20, # Minimum characters before a sentence is synthesized
sentence_terminators=None, # Defaults to {".", "!", "?", "\n"}
)
Methods:
await tts.feed(text): add text to the streamawait tts.cancel(): drop pending synthesis and buffered text
License
MIT
Metadata
Release files for anyvoice 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| anyvoice-0.1.0.tar.gz | 18.4 kB | With provenance | Details for anyvoice-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| anyvoice-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for anyvoice-0.1.0-py3-none-any.whl |
Total release size: 35.0 kB
Release files / anyvoice-0.1.0.tar.gz
| Download URL | anyvoice-0.1.0.tar.gz |
|---|---|
| Size | 18.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4275bc6ac14f3eb3f09431eebb894e463ff7b5924afed16059330ddb1c3bb3a6
|
|
BLAKE2b-256 checksum How to use checksums |
de4e586faf72ade1dde0dfae72556a00a2ad622f659bbff4e01843878c69b74c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.
Transparency logRelease files / anyvoice-0.1.0-py3-none-any.whl
| Download URL | anyvoice-0.1.0-py3-none-any.whl |
|---|---|
| Size | 16.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
62b0939fddf326d47a01e3318c01767ccccbeaa9dd53b0d9c3fd1ec7722e7924
|
|
BLAKE2b-256 checksum How to use checksums |
26f99cbc1f6dc3524686bc68d5fb15b30548afdb0f95ceefae5b633baf75d616
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.
Transparency logSecurity for anyvoice 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for anyvoice-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.