Skip to main content

Python client for DVoice TTS API

Project description

dvoice-tts-py

A Python client for the DVoice TTS API, providing text-to-speech functionality with support for single audio generation via HTTP and real-time audio streaming over WebSocket.

🚀 Features

  • Generate audio files in multiple formats (mp3, wav, ogg, aac, flac).
  • Stream audio in real-time for low-latency applications.
  • Simple and intuitive API for easy integration.
  • Robust error handling for reliable usage.

📦 Installation

Install the package using pip:

pip install dvoice-tts

🧠 Usage

Importing and Initializing

from dvoice_tts import TTS

# Initialize the client with your API token
tts = TTS(token="YOUR_API_TOKEN")

Replace "YOUR_API_TOKEN" with your actual API token from the DVoice TTS service.

🔊 Generating a Single Audio File

Use the single() method to generate an audio file from text:

try:
    audio = tts.single(
        model="default",
        text="Salom!",
        format="mp3"  # Options: 'mp3', 'wav', 'ogg', 'aac', 'flac'
    )
    # Save audio to file
    with open("output.mp3", "wb") as f:
        f.write(audio)
    print("Audio file saved as output.mp3")
except Exception as e:
    print(f"Error generating audio: {e}")

🌊 Real-Time Audio Streaming

Use the stream() method to receive audio chunks in real-time over WebSocket:

def on_chunk(err, chunk, close):
    if err:
        print(f"Stream error: {err}")
        close()  # Close the connection on error
    elif chunk:
        # Process the audio chunk (e.g., feed to an audio player)
        print(f"Received audio chunk: {len(chunk)} bytes")
    else:
        # Stream has ended
        print("Stream completed")
        close()  # Close the connection

tts.stream(
    model="default",
    text="Davomiy nutqni real vaqtda translatsiya qilish!",
    format="mp3",  # Options: 'mp3', 'wav', 'ogg', 'aac', 'flac'
    callback=on_chunk
)

The close() function can be called within the callback to terminate the WebSocket connection manually.

📜 API Reference

TTS(token: str)

Creates a new TTS client instance.

  • Parameters:
    • token (str): Your API token for DVoice TTS authentication.

tts.single(model: str, text: str, format: str) -> bytes

Generates a single audio file from text via HTTP.

  • Parameters:
    • model (str): The TTS model to use (e.g., "default").
    • text (str): The text to convert to speech.
    • format (str): Audio format ("mp3", "wav", "ogg", "aac", "flac").
  • Returns: bytes - The audio data as bytes.
  • Raises: Exception if the request fails.

tts.stream(model: str, text: str, format: str, callback: Callable[[Optional[Exception], Optional[bytes], Callable[[], None]], None]) -> None

Streams audio chunks in real-time over WebSocket.

  • Parameters:
    • model (str): The TTS model to use (e.g., "default").
    • text (str): The text to convert to speech.
    • format (str): Audio format ("mp3", "wav", "ogg", "aac", "flac").
    • callback (Callable): A function that receives (err, chunk, close):
      • err (Exception | None): Any error that occurs.
      • chunk (bytes | None): Audio data chunk, or None if the stream ends.
      • close (Callable): Function to close the WebSocket connection.
  • Returns: None

🛠️ Error Handling

Both single() and stream() methods include error handling:

  • For single(), errors are raised as exceptions and can be caught using try/except.
  • For stream(), errors are passed to the callback as the err parameter.

Example:

try:
    audio = tts.single(model="default", text="Test", format="mp3")
except Exception as e:
    print(f"Failed to generate audio: {e}")

📝 Notes

  • Ensure a valid API token is provided to authenticate with the DVoice TTS API.
  • The stream() method is suitable for applications requiring real-time audio, such as live voice assistants or interactive systems.
  • Refer to the DVoice TTS API documentation for supported models and additional configuration options.
  • WebSocket streaming uses the endpoint ws://oyqiz.airi.uz/stream.

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

🌐 Links

🌟 Contributing

Contributions are welcome! Please submit a pull request or open an issue on the GitHub repository for bug reports, feature requests, or improvements.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dvoice_tts-1.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dvoice_tts-1.0.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file dvoice_tts-1.0.1.tar.gz.

File metadata

  • Download URL: dvoice_tts-1.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dvoice_tts-1.0.1.tar.gz
Algorithm Hash digest
SHA256 24bf815405b2987f69e5d642cd7849858264ac56720a040458ed0ce1977689d5
MD5 02e4558baa9e016d0718e83271fddd16
BLAKE2b-256 019340cca42606ccf993473f27ca667a82cd59b0744afcf72a82b4849ad44821

See more details on using hashes here.

File details

Details for the file dvoice_tts-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: dvoice_tts-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dvoice_tts-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9efd344744d82b9df98feb32ed06d6fcfbe0501a27ee35991307e1dd54c862f3
MD5 b632aef24dbd43709806053ab49badc6
BLAKE2b-256 62e5b36ab7d370efaa3632419a6d9b98921cbcd0d76e356fada6d4ee0b8ed56c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page