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.

Obtaining an API Token

To use the DVoice TTS API, you need a valid API token. Visit the main website at dvoice.uz and navigate to profile.dvoice.uz to create an account or log in. Once logged in, you can generate or retrieve your API token from your profile dashboard.

🔊 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.3.tar.gz (4.6 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.3-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dvoice_tts-1.0.3.tar.gz
  • Upload date:
  • Size: 4.6 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.3.tar.gz
Algorithm Hash digest
SHA256 89372c3f5b04c734b3674353a1352cca584c24ca48ce0325a0e39e43272e04d4
MD5 c201845d7d22d93603ff1dd6236e27d6
BLAKE2b-256 dec10a55c18497ce5d93f3df1ece6bb238c4164dd48f4d8df91c8015112b1973

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dvoice_tts-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.0 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5f830030ab902c9dd393a19efab7777cbe599b2b2f235829db79331e5efefca5
MD5 66edd75606c1bc0593f24b098ddb1cb9
BLAKE2b-256 34ec5d646bba418b90a586e96ca35824858e4e742058632d8b2bd04d8d5e5848

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