Skip to main content

A python SDK for the Neuphonic TTS Engine.

Project description

PyNeuphonic

The official Neuphonic Python library providing simple, convenient access to the Neuphonic text-to-speech websocket API from any Python 3.9+ application.

For support or to get involved, join our Discord!

Documentation

See https://docs.neuphonic.com for the complete API documentation.

Installation

Install this package into your environment using your chosen package manager:

pip install pyneuphonic

List Voices

from pyneuphonic import Neuphonic
import os

client = Neuphonic(api_key=os.environ.get('NEUPHONIC_API_TOKEN'))
voices = client.voices.get()  # get's all available voices
print(voices)

Audio Generation

SSE (Server Side Events)

from pyneuphonic import Neuphonic, AudioPlayer, TTSConfig
import os

client = Neuphonic(api_key=os.environ.get('NEUPHONIC_API_TOKEN'))

sse = client.tts.SSEClient()

# TTSConfig is a pydantic model so check out the source code for all valid options
tts_config = TTSConfig(speed=1.05, voice='ebf2c88e-e69d-4eeb-9b9b-9f3a648787a5')

# Create an audio player with `pyaudio`
with AudioPlayer() as player:
    response = sse.send('Hello, world!', tts_config=tts_config)

    for item in response:
        player.play(item.data.audio)

Asynchronous SSE

from pyneuphonic import Neuphonic, AudioPlayer, TTSConfig
import os
import asyncio

async def main():
    client = Neuphonic(api_key=os.environ.get('NEUPHONIC_API_TOKEN'))

    sse = client.tts.AsyncSSEClient()
    tts_config = TTSConfig(speed=1.05)

    with AudioPlayer() as player:
        response = sse.send('Hello, world!', tts_config=tts_config)

        async for item in response:
            player.play(item.data.audio)

asyncio.run(main())

Asynchronous Websocket

from pyneuphonic import Neuphonic, AudioPlayer, TTSConfig, WebsocketEvents
from pyneuphonic.models import WebsocketResponse
import os
import asyncio

async def main():
    client = Neuphonic(api_key=os.environ.get('NEUPHONIC_API_TOKEN'))

    ws = client.tts.AsyncWebsocketClient()
    tts_config = TTSConfig(voice='ebf2c88e-e69d-4eeb-9b9b-9f3a648787a5')

    player = AudioPlayer()
    player.open()

    # Attach event handlers. Check WebsocketEvents enum for all valid events.
    async def on_message(message: WebsocketResponse):
        player.play(message.data.audio)

    async def on_close():
        player.close()

    ws.on(WebsocketEvents.MESSAGE, on_message)
    ws.on(WebsocketEvents.CLOSE, on_close)

    await ws.open(tts_config=tts_config)

    # A special symbol ' <STOP>' must be sent to the server, otherwise the server will wait for
    # more text to be sent before generating the last few snippets of audio
    await ws.send('Hello, world!', autocomplete=True)
    await ws.send('Hello, world! <STOP>')  # Both the above line, and this line, are equivalent

    await asyncio.sleep(4)  # let the audio play
    await ws.close()  # close the websocket and terminate the audio resources

asyncio.run(main())

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

pyneuphonic-1.1.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

pyneuphonic-1.1.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file pyneuphonic-1.1.0.tar.gz.

File metadata

  • Download URL: pyneuphonic-1.1.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/23.4.0

File hashes

Hashes for pyneuphonic-1.1.0.tar.gz
Algorithm Hash digest
SHA256 7c214ff3a21fd99f15cc39347ce9f9a03bc3ce759dafa8c3699171a59f7f72df
MD5 fca6cc558453f8a8cbee2b04c660e178
BLAKE2b-256 fcad40061e389d6c197e573b50a5d95ef90eb56b6b2b017ce3925005930f5db3

See more details on using hashes here.

File details

Details for the file pyneuphonic-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyneuphonic-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/23.4.0

File hashes

Hashes for pyneuphonic-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a834ce7a7bf37dc5ab2dbee6ced5a68821bc0f1e901c19fcb0f3825dddecd60
MD5 1384f84ae55115661e9d0af9c8833424
BLAKE2b-256 4aee4ef676679612aee98dd38366009788aa071c13d827831e4f7e829cffc061

See more details on using hashes here.

Supported by

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