Package: aspeak
:speaking_head: aspeakA simple text-to-speech client for Azure TTS API. :laughing:NoteStarting from version 6.0.0,aspeakby default uses the RESTful API of Azure TTS. If you want to use the WebSocket API,
you can specify--mode websocketwhen invokingaspeakor setmode = "websocket"in theauthsection of your profile.Starting from version 4.0.0,aspeakis rewritten in rust. The old python version is available at thepythonbranch.You can sign up for an Azure account and thenchoose a payment plan as needed (or stick to free tier).
The free tier includes a quota of 0.5 million characters per month, free of charge.Please refer to theAuthentication sectionto learn how to set up authentication for aspeak.InstallationDownload from GitHub Releases (Recommended for most users)Download the latest release fromhere.After downloading, extract the archive and you will get a binary executable file.You can put it in a directory that is in yourPATHenvironment variable so that you can run it from anywhere.Installl from AUR (Recommended for Arch Linux users)From v4.1.0, You can installaspeak-binfrom AUR.Install from PyPIInstalling from PyPI will also install the python binding ofaspeakfor you. CheckLibrary Usage#Pythonfor more information on using the python binding.pipinstall-Uaspeak==6.0.0Now the prebuilt wheels are only available for x86_64 architecture.
Due to some technical issues, I haven't uploaded the source distribution to PyPI yet.
So to build wheel from source, you need to follow the instructions inInstall from Source.Because of manylinux compatibility issues, the wheels for linux are not available on PyPI. (But you can still build them from source.)Install from SourceCLI OnlyThe easiest way to installaspeakfrom source is to use cargo:cargoinstallaspeak-FbinaryAlternatively, you can also installaspeakfrom AUR.Python WheelTo build the python wheel, you need to installmaturinfirst:pipinstallmaturinAfter cloning the repository andcdinto the directory
, you can build the wheel by running:maturinbuild--release--strip-Fpython--bindingspyo3--interpreterpython--manifest-pathCargo.toml--outdist-pyo3
maturinbuild--release--strip--bindingsbin-Fbinary--interpreterpython--manifest-pathCargo.toml--outdist-bin
bashmerge-wheel.bashIf everything goes well, you will get a wheel file in thedistdirectory.UsageRunaspeak helpto see the help message.Runaspeak help <subcommand>to see the help message of a subcommand.AuthenticationThe authentication options should be placed before any subcommand.For example, to utilize your subscription key and
an official endpoint designated by a region,
run the following command:$aspeak--region<YOUR_REGION>--key<YOUR_SUBSCRIPTION_KEY>text"Hello World"If you are using a custom endpoint, you can use the--endpointoption instead of--region.To avoid repetition, you can store your authentication details
in your aspeak profile.
Read the following section for more details.From v5.2.0, you can also set the authentication secrets via the following environment variables:ASPEAK_AUTH_KEYfor authentication using subscription keyASPEAK_AUTH_TOKENfor authentication using authorization tokenFrom v4.3.0, you can let aspeak use a proxy server to connect to the endpoint.
For now, only http and socks5 proxies are supported (no https support yet). For example:$aspeak--proxyhttp://your_proxy_server:porttext"Hello World"$aspeak--proxysocks5://your_proxy_server:porttext"Hello World"aspeak also respects theHTTP_PROXY(orhttp_proxy) environment variable.Configurationaspeak v4 introduces the concept of profiles.
A profile is a configuration file where you can specify default values for the command line options.Run the following command to create your default profile:$aspeakconfiginitTo edit the profile, run:$aspeakconfigeditIf you have trouble running the above command, you can edit the profile manually:Fist get the path of the profile by running:$aspeakconfigwhereThen edit the file with your favorite text editor.The profile is a TOML file. The default profile looks like this:Check the comments in the config file for more information about available options.# Profile for aspeak# GitHub: https://github.com/kxxt/aspeak# Output verbosity# 0   - Default# 1   - Verbose# The following output verbosity levels are only supported on debug build# 2   - Debug# >=3 - Traceverbosity=0## Authentication configuration#[auth]# Endpoint for TTS# endpoint = "wss://eastus.tts.speech.microsoft.com/cognitiveservices/websocket/v1"# Alternatively, you can specify the region if you are using official endpoints# region = "eastus"# Synthesizer Mode, "rest" or "websocket"# mode = "rest"# Azure Subscription Key# key = "YOUR_KEY"# Authentication Token# token = "Your Authentication Token"# Extra http headers (for experts)# headers = [["X-My-Header", "My-Value"], ["X-My-Header2", "My-Value2"]]# Proxy# proxy = "socks5://127.0.0.1:7890"# Voice list API url# voice_list_api = "Custom voice list API url"## Configuration for text subcommand#[text]# Voice to use. Note that it takes precedence over the locale# voice = "en-US-JennyNeural"# Locale to uselocale="en-US"# Rate# rate = 0# Pitch# pitch = 0# Role# role = "Boy"# Style, "general" by default# style = "general"# Style degree, a floating-point number between 0.1 and 2.0# style_degree = 1.0## Output Configuration#[output]# Container Format, Only wav/mp3/ogg/webm is supported.container="wav"# Audio Quality. Run `aspeak list-qualities` to see available qualities.## If you choose a container format that does not support the quality level you specified here,# we will automatically select the closest level for you.quality=0# Audio Format(for experts). Run `aspeak list-formats` to see available formats.# Note that it takes precedence over container and quality!# format = "audio-16khz-128kbitrate-mono-mp3"If you want to use a profile other than your default profile, you can use the--profileargument:aspeak--profile<PATH_TO_A_PROFILE>text"Hello"If you want to temporarily disable the profile, you can use the--no-profileargument:aspeak--no-profile--regioneastus--key<YOUR_KEY>text"Hello"Pitch and Raterate: The speaking rate of the voice.If you use a float value (say0.5), the value will be multiplied by 100% and become50.00%.You can use the following values as well:x-slow,slow,medium,fast,x-fast,default.You can also use percentage values directly:+10%.You can also use a relative float value (withfpostfix),1.2f:According to theAzure documentation,A relative value, expressed as a number that acts as a multiplier of the default.For example, a value of1fresults in no change in the rate. A value of0.5fresults in a halving of the rate. A value of3fresults in a tripling of the rate.pitch: The pitch of the voice.If you use a float value (say-0.5), the value will be multiplied by 100% and become-50.00%.You can also use the following values as well:x-low,low,medium,high,x-high,default.You can also use percentage values directly:+10%.You can also use a relative value, (e.g.-2stor+80Hz):According to theAzure documentation,A relative value, expressed as a number preceded by "+" or "-" and followed by "Hz" or "st" that specifies an amount to change the pitch.The "st" indicates the change unit is semitone, which is half of a tone (a half step) on the standard diatonic scale.You can also use an absolute value: e.g.600HzNote: Unreasonable high/low values will be clipped to reasonable values by Azure Cognitive Services.ExamplesThe following examples assume that you have already set up authentication in your profile.Speak "Hello, world!" to default speaker.$aspeaktext"Hello, world"SSML to Speech$aspeakssml<< EOF<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-US'><voice name='en-US-JennyNeural'>Hello, world!</voice></speak>EOFList all available voices.$aspeaklist-voicesList all available voices for Chinese.$aspeaklist-voices-lzh-CNGet information about a voice.$aspeaklist-voices-ven-US-SaraNeuralOutputMicrosoft Server Speech Text to Speech Voice (en-US, SaraNeural)
Display name: Sara
Local name: Sara @ en-US
Locale: English (United States)
Gender: Female
ID: en-US-SaraNeural
Voice type: Neural
Status: GA
Sample rate: 48000Hz
Words per minute: 157
Styles: ["angry", "cheerful", "excited", "friendly", "hopeful", "sad", "shouting", "terrified", "unfriendly", "whispering"]Save synthesized speech to a file.$aspeaktext"Hello, world"-ooutput.wavIf you prefer mp3/ogg/webm, you can use-c mp3/-c ogg/-c webmoption.$aspeaktext"Hello, world"-ooutput.mp3-cmp3
$aspeaktext"Hello, world"-ooutput.ogg-cogg
$aspeaktext"Hello, world"-ooutput.webm-cwebmList available quality levels$aspeaklist-qualitiesOutputQualities for MP3:
  3: audio-48khz-192kbitrate-mono-mp3
  2: audio-48khz-96kbitrate-mono-mp3
 -3: audio-16khz-64kbitrate-mono-mp3
  1: audio-24khz-160kbitrate-mono-mp3
 -2: audio-16khz-128kbitrate-mono-mp3
 -4: audio-16khz-32kbitrate-mono-mp3
 -1: audio-24khz-48kbitrate-mono-mp3
  0: audio-24khz-96kbitrate-mono-mp3

Qualities for WAV:
 -2: riff-8khz-16bit-mono-pcm
  1: riff-24khz-16bit-mono-pcm
  0: riff-24khz-16bit-mono-pcm
 -1: riff-16khz-16bit-mono-pcm

Qualities for OGG:
  0: ogg-24khz-16bit-mono-opus
 -1: ogg-16khz-16bit-mono-opus
  1: ogg-48khz-16bit-mono-opus

Qualities for WEBM:
  0: webm-24khz-16bit-mono-opus
 -1: webm-16khz-16bit-mono-opus
  1: webm-24khz-16bit-24kbps-mono-opusList available audio formats (For expert users)$aspeaklist-formatsOutputamr-wb-16000hz
audio-16khz-128kbitrate-mono-mp3
audio-16khz-16bit-32kbps-mono-opus
audio-16khz-32kbitrate-mono-mp3
audio-16khz-64kbitrate-mono-mp3
audio-24khz-160kbitrate-mono-mp3
audio-24khz-16bit-24kbps-mono-opus
audio-24khz-16bit-48kbps-mono-opus
audio-24khz-48kbitrate-mono-mp3
audio-24khz-96kbitrate-mono-mp3
audio-48khz-192kbitrate-mono-mp3
audio-48khz-96kbitrate-mono-mp3
ogg-16khz-16bit-mono-opus
ogg-24khz-16bit-mono-opus
ogg-48khz-16bit-mono-opus
raw-16khz-16bit-mono-pcm
raw-16khz-16bit-mono-truesilk
raw-22050hz-16bit-mono-pcm
raw-24khz-16bit-mono-pcm
raw-24khz-16bit-mono-truesilk
raw-44100hz-16bit-mono-pcm
raw-48khz-16bit-mono-pcm
raw-8khz-16bit-mono-pcm
raw-8khz-8bit-mono-alaw
raw-8khz-8bit-mono-mulaw
riff-16khz-16bit-mono-pcm
riff-22050hz-16bit-mono-pcm
riff-24khz-16bit-mono-pcm
riff-44100hz-16bit-mono-pcm
riff-48khz-16bit-mono-pcm
riff-8khz-16bit-mono-pcm
riff-8khz-8bit-mono-alaw
riff-8khz-8bit-mono-mulaw
webm-16khz-16bit-mono-opus
webm-24khz-16bit-24kbps-mono-opus
webm-24khz-16bit-mono-opusIncrease/Decrease audio qualities# Less than default quality.$aspeaktext"Hello, world"-ooutput.mp3-cmp3-q=-1# Best quality for mp3$aspeaktext"Hello, world"-ooutput.mp3-cmp3-q=3Read text from file and speak it.$catinput.txt|aspeaktextor$aspeaktext-finput.txtwith custom encoding:$aspeaktext-finput.txt-egbkRead from stdin and speak it.$aspeaktextmaybe you prefer:$aspeaktext-lzh-CN<< EOF我能吞下玻璃而不伤身体。EOFSpeak Chinese.$aspeaktext"你好，世界！"-lzh-CNUse a custom voice.$aspeaktext"你好，世界！"-vzh-CN-YunjianNeuralCustom pitch, rate and style$aspeaktext"你好，世界！"-vzh-CN-XiaoxiaoNeural-p1.5-r0.5-Ssad
$aspeaktext"你好，世界！"-vzh-CN-XiaoxiaoNeural-p=-10%-r=+5%-Scheerful
$aspeaktext"你好，世界！"-vzh-CN-XiaoxiaoNeural-p=+40Hz-r=1.2f-Sfearful
$aspeaktext"你好，世界！"-vzh-CN-XiaoxiaoNeural-p=high-r=x-slow-Scalm
$aspeaktext"你好，世界！"-vzh-CN-XiaoxiaoNeural-p=+1st-r=-7%-SlyricalAdvanced UsageUse a custom audio format for outputNote: Some audio formats are not supported when outputting to speaker.$aspeaktext"Hello World"-Friff-48khz-16bit-mono-pcm-ohigh-quality.wavLibrary UsagePythonThe new version ofaspeakis written in Rust, and the Python binding is provided by PyO3.Here is a simple example:fromaspeakimportSpeechServiceservice=SpeechService(region="eastus",key="YOUR_AZURE_SUBSCRIPTION_KEY")service.speak_text("Hello, world")First you need to create aSpeechServiceinstance.When creating aSpeechServiceinstance, you can specify the following parameters:audio_format(Positional argument): The audio format of the output audio. Default isAudioFormat.Riff24KHz16BitMonoPcm.You can get an audio format by providing a container format and a quality level:AudioFormat("mp3", 2).endpoint: The endpoint of the speech service.region: Alternatively, you can specify the region of the speech service instead of typing the boring endpoint url.key: The subscription key of the speech service.token: The auth token for the speech service. If you provide a token, the subscription key will be ignored.headers: Additional HTTP headers for the speech service.mode: Choose the synthesizer to use. Eitherrestorwebsocket.In websocket mode, the synthesizer will connect to the endpoint when theSpeechServiceinstance is created.After that, you can callspeak_text()to speak the text orspeak_ssml()to speak the SSML.
Or you can callsynthesize_text()orsynthesize_ssml()to get the audio data.Forsynthesize_text()andsynthesize_ssml(), if you provide anoutput, the audio data will be written to that file and the function will returnNone. Otherwise, the function will return the audio data.Here are the common options forspeak_text()andsynthesize_text():locale: The locale of the voice. Default isen-US.voice: The voice name. Default isen-US-JennyNeural.rate: The speaking rate of the voice. It must be a string that fits the requirements as documented in this section:Pitch and Ratepitch: The pitch of the voice. It must be a string that fits the requirements as documented in this section:Pitch and Ratestyle: The style of the voice.You can get a list of available styles for a specific voice by executingaspeak -L -v <VOICE_ID>The default value isgeneral.style_degree: The degree of the style.According to theAzure documentation, style degree specifies the intensity of the speaking style.
It is a floating point number between 0.01 and 2, inclusive.At the time of writing, style degree adjustments are supported for Chinese (Mandarin, Simplified) neural voices.role: The role of the voice.According to theAzure documentation,rolespecifies the speaking role-play. The voice acts as a different age and gender, but the voice name isn't
changed.At the time of writing, role adjustments are supported for these Chinese (Mandarin, Simplified) neural voices:zh-CN-XiaomoNeural,zh-CN-XiaoxuanNeural,zh-CN-YunxiNeural, andzh-CN-YunyeNeural.RustAddaspeakto yourCargo.toml:$cargoaddaspeakThen follow thedocumentationofaspeakcrate.There are 4 examples for quick reference:Simple usage of RestSynthesizerSimple usage of WebsocketSynthesizerSynthesize all txt files in a given directoryRead-Synthesize-Speak-Loop: Read text from stdin line by line and speak it