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
import time

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)

# 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)

    player.save_audio()  # save the audio to a .wav file
    time.sleep(1)  # ensure all the audio has played before the python program terminates

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)

        player.save_audio()  # save the audio to a .wav file
        await asyncio.sleep(1)  # ensure all the audio has played before the python program terminates

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(3)  # let the audio play
    player.save_audio()  # save the audio to a .wav file
    await ws.close()  # close the websocket and terminate the audio resources

asyncio.run(main())

Saving Audio

As per the examples above, you can use the AudioPlayer object to save audio.

player.save_audio()

However, if you do not want to play audio and simply want to save it, check out the examples in snippets/sse/save_audio.py and snippets/websocket/save_audio.py for examples on how to do this.

Example Applications

Check out the snippets folder for some example applications.

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.5.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

pyneuphonic-1.1.5-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyneuphonic-1.1.5.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure

File hashes

Hashes for pyneuphonic-1.1.5.tar.gz
Algorithm Hash digest
SHA256 0644d0bf4f76169b01f0b5567ac88789bdb6b40b925905e45a13ce0b056264ad
MD5 b33ec72f4f12515079a731409ae5a4ed
BLAKE2b-256 9519c13647e64aa7598ed708fa8e0d0470faa88faf725060467a1843ae558492

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyneuphonic-1.1.5-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.20 Linux/6.5.0-1025-azure

File hashes

Hashes for pyneuphonic-1.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f5a48ebd48f260f75af34a4c7d42cb637fcbb0afdcb58bd21927e4f31edc3fca
MD5 d174565c2b0f604ea9a985d77f3ccf26
BLAKE2b-256 da7b105883b08dda585d79b2e263a1e600d79af5e232cfc0ac7f66f3704052da

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