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

Uploaded Python 3

File details

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

File metadata

  • Download URL: dvoice_tts-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 ad7ed51603904995100684d301a625a9e57bbc090e834b262ceb8c07245ecf42
MD5 cd8536307c645d341c054de862787bbe
BLAKE2b-256 74c532cec70ece6b78930181f969f7f864da3bf9438fbcdc8fa3e757a3ba453e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dvoice_tts-1.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8fcbd97b691d51cfbdc29ac4e0310691aa748081957470f8cfcb1d2d418d0a66
MD5 817cc2ccc1c1bc4741f24e446953acff
BLAKE2b-256 38fb1d21ef0e7f086722cfaf38959e788b828ea7b89b388f5cb5477d7bc9ff70

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