openai
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| OpenAITTSProvider anyvoice.providers.openai TTS provider using OpenAI's Text-to-Speech API. |
||
| OpenAITTSSession anyvoice.providers.openai TTS session using OpenAI's streaming API. |
🛈 DocStrings¶
OpenAI TTS provider implementation.
OpenAITTSProvider
¶
Bases: TTSProvider
TTS provider using OpenAI's Text-to-Speech API.
Provides high-quality neural TTS with streaming support. Requires an OpenAI API key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str | None
|
OpenAI API key. If not provided, uses OPENAI_API_KEY env var. |
None
|
base_url
|
str | None
|
Optional custom API base URL. |
None
|
Example
provider = OpenAITTSProvider(api_key="sk-...") session = provider.session(voice="nova", speed=1.0)
async with TTSStream(session, sink=SoundDeviceSink()) as tts: await tts.feed("Hello world!")
Source code in src/anyvoice/providers/openai.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
__aenter__
async
¶
Initialize the OpenAI client shared by the sessions created inside.
__aexit__
async
¶
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Close the OpenAI client.
Source code in src/anyvoice/providers/openai.py
session
¶
session(
*,
model: TTSModel = "tts-1",
voice: TTSVoice = "alloy",
speed: float = 1.0,
chunk_size: int = 1024,
**_: Any,
) -> OpenAITTSSession
Create a new OpenAI TTS session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
TTSModel
|
TTS model ("tts-1" for speed, "tts-1-hd" for quality). |
'tts-1'
|
voice
|
TTSVoice
|
Voice to use (alloy, echo, fable, onyx, nova, shimmer). |
'alloy'
|
speed
|
float
|
Speech speed from 0.25 to 4.0. Defaults to 1.0. |
1.0
|
chunk_size
|
int
|
Size of audio chunks in bytes. Defaults to 1024. |
1024
|
Returns:
| Type | Description |
|---|---|
OpenAITTSSession
|
Configured OpenAI TTS session. Inside |
OpenAITTSSession
|
provider's client; otherwise it creates its own and closes it on exit. |
Source code in src/anyvoice/providers/openai.py
OpenAITTSSession
¶
Bases: TTSSession
TTS session using OpenAI's streaming API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
AsyncOpenAI | None
|
OpenAI async client (owned by the caller, not closed by the session). |
None
|
model
|
TTSModel
|
TTS model to use. |
'tts-1'
|
voice
|
TTSVoice
|
Voice for synthesis. |
'alloy'
|
speed
|
float
|
Speech speed (0.25 to 4.0). |
1.0
|
chunk_size
|
int
|
Size of audio chunks to yield. |
1024
|
client_factory
|
Callable[[], AsyncOpenAI] | None
|
Creates a client when none is given; the session owns that client and closes it on exit (a later use creates a new one). |
None
|
Source code in src/anyvoice/providers/openai.py
__aexit__
async
¶
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Close the client if the session created it.
Source code in src/anyvoice/providers/openai.py
synthesize
async
¶
Synthesize text to PCM audio using OpenAI's streaming API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to synthesize. |
required |
Yields:
| Type | Description |
|---|---|
AsyncIterator[bytes]
|
PCM audio chunks (16-bit signed, mono, 24kHz). |