Skip to content

EdgeTTSSession

Base classes

Name Children Inherits
TTSSession
anyvoice.base
Abstract base class for TTS synthesis sessions.

⋔ Inheritance diagram

graph TD
  298729951["edge.EdgeTTSSession"]
  2396137972["base.TTSSession"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  2396137972 --> 298729951
  925645236 --> 2396137972
  2751680898 --> 925645236

🛈 DocStrings

Bases: TTSSession

TTS session using Microsoft Edge's TTS service.

Parameters:

Name Type Description Default
voice str

Voice name (e.g., "en-US-AriaNeural").

'en-US-AriaNeural'
rate str

Speaking rate adjustment (e.g., "+25%", "-10%").

'+0%'
volume str

Volume adjustment (e.g., "+10%", "-20%").

'+0%'
pitch str

Pitch adjustment in Hz (e.g., "+10Hz", "-5Hz").

'+0Hz'
sample_rate int

Target sample rate for decoded audio.

24000
Source code in src/anyvoice/providers/edge.py
class EdgeTTSSession(TTSSession):
    """TTS session using Microsoft Edge's TTS service.

    Args:
        voice: Voice name (e.g., "en-US-AriaNeural").
        rate: Speaking rate adjustment (e.g., "+25%", "-10%").
        volume: Volume adjustment (e.g., "+10%", "-20%").
        pitch: Pitch adjustment in Hz (e.g., "+10Hz", "-5Hz").
        sample_rate: Target sample rate for decoded audio.
    """

    def __init__(
        self,
        *,
        voice: str = "en-US-AriaNeural",
        rate: str = "+0%",
        volume: str = "+0%",
        pitch: str = "+0Hz",
        sample_rate: int = 24000,
    ) -> None:
        self._voice = voice
        self._rate = rate
        self._volume = volume
        self._pitch = pitch
        self._sample_rate = sample_rate

    async def synthesize(self, text: str) -> AsyncIterator[bytes]:
        """Synthesize text to PCM audio using Edge TTS.

        Edge TTS returns MP3 which is decoded to PCM via miniaudio.

        Args:
            text: Text to synthesize.

        Yields:
            PCM audio chunks (16-bit signed, mono).
        """
        import edge_tts
        import miniaudio  # type: ignore[import-untyped]

        if not text.strip():
            return

        communicate = edge_tts.Communicate(
            text,
            voice=self._voice,
            rate=self._rate,
            volume=self._volume,
            pitch=self._pitch,
        )

        # Collect MP3 chunks
        mp3_data = bytearray()
        async for chunk in communicate.stream():
            if chunk["type"] == "audio":
                assert "data" in chunk
                mp3_data.extend(chunk["data"])

        if mp3_data:
            # Decode MP3 to mono 16-bit PCM at target sample rate, off the event
            # loop (CPU-bound; runs in parallel under free-threading)
            decoded = await asyncio.to_thread(
                miniaudio.decode,
                bytes(mp3_data),
                output_format=miniaudio.SampleFormat.SIGNED16,
                nchannels=1,
                sample_rate=self._sample_rate,
            )
            yield decoded.samples.tobytes()

synthesize async

synthesize(text: str) -> AsyncIterator[bytes]

Synthesize text to PCM audio using Edge TTS.

Edge TTS returns MP3 which is decoded to PCM via miniaudio.

Parameters:

Name Type Description Default
text str

Text to synthesize.

required

Yields:

Type Description
AsyncIterator[bytes]

PCM audio chunks (16-bit signed, mono).

Source code in src/anyvoice/providers/edge.py
async def synthesize(self, text: str) -> AsyncIterator[bytes]:
    """Synthesize text to PCM audio using Edge TTS.

    Edge TTS returns MP3 which is decoded to PCM via miniaudio.

    Args:
        text: Text to synthesize.

    Yields:
        PCM audio chunks (16-bit signed, mono).
    """
    import edge_tts
    import miniaudio  # type: ignore[import-untyped]

    if not text.strip():
        return

    communicate = edge_tts.Communicate(
        text,
        voice=self._voice,
        rate=self._rate,
        volume=self._volume,
        pitch=self._pitch,
    )

    # Collect MP3 chunks
    mp3_data = bytearray()
    async for chunk in communicate.stream():
        if chunk["type"] == "audio":
            assert "data" in chunk
            mp3_data.extend(chunk["data"])

    if mp3_data:
        # Decode MP3 to mono 16-bit PCM at target sample rate, off the event
        # loop (CPU-bound; runs in parallel under free-threading)
        decoded = await asyncio.to_thread(
            miniaudio.decode,
            bytes(mp3_data),
            output_format=miniaudio.SampleFormat.SIGNED16,
            nchannels=1,
            sample_rate=self._sample_rate,
        )
        yield decoded.samples.tobytes()

:fa-brands-github: Show source on GitHub