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.0.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.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dvoice_tts-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 fb1196f9fee9a20ce65742429196fe6e21025ce0754818f0b2b6fb159f815551
MD5 4eea990041883bbaec733d6c33f2913d
BLAKE2b-256 baa3465b86554d662863b5fea939997f4962e428cd420396d3b6cdcfda462249

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dvoice_tts-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df7ebc7342e76c373e5c8948b8c024ed5556fff131f86867200e4c930c64890a
MD5 2be52146c1818397793880b43485580f
BLAKE2b-256 651ecc7efdd825e321ab6d425fba9254b8ec86dece94a7fbfe2e87583112c135

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