aflr is the official api.audio Python 3 SDK. This SDK provides easy access to the api.audio API from applications written in python.
This repository is actively maintained by Aflorithmic Labs. For examples, recipes and api reference see the api.audio docs.
You don't need this source code unless you want to modify it. If you want to use the package, just run:
pip install aflr -UInstall from source with:
python setup.py installPython 3.6+
Create a file hello.py
touch hello.pyThe library needs to be configured with your account's secret key which is available in your Aflorithmic Dashboard. Import the aflr package and set aflr.api_key with the api-key you got from the dashboard:
import aflr
aflr.api_key = "your-key"Let's create our first speech from text.
✍️ Create a new script:
script = aflr.Script().create(scriptText="Hello world")🎤 Create an speech audio file from the script:
response = aflr.Speech().create(scriptId=script["scriptId"])🎉 Finally, get the urls of the audio files generated:
urls = aflr.Speech().retrieve(scriptId=script["scriptId"])
print(urls)Or download the files in your current folder:
aflr.Speech().download(scriptId=script["scriptId"], destination=".")Easy right? 🔮 This is the hello.py final picture:
import aflr
aflr.api_key = "your-key"
# script creation
script = aflr.Script().create(scriptText="Hello world")
# speech creation and retrieval
response = aflr.Speech().create(scriptId=script["scriptId"])
urls = aflr.Speech().retrieve(scriptId=script["scriptId"])
aflr.Speech().download(scriptId=script["scriptId"], destination=".")Now let's run the code:
python hello.pyOnce completed, check the files in the hello.py root folder - you will see a new audio file. Play it!
import aflrThe library needs to be configured with your account's secret key which is available in your Aflorithmic Dashboard. Set aflr.api_key with the api-key you got from the dashboard:
aflr.api_key = "your-key"You can also authenticate using aflr_key environment variable and the aflr SDK will automatically use it. To setup, open the terminal and type:
export aflr_key=<your-key>If you provide both environment variable and aflr.api_key authentication, the aflr.api_key will be used.
There are two approaches to use the resources.
First approach is to get the resource class first, then use resource methods. For example, to create a Script, we could do:
Script = aflr.Script()
Script.create()The second approach is to use it directly:
aflr.Script().create()Same logic applies for other resources (speech, voice, sound...)
The Script resource/class allows you to create, retrieve and list scripts. Learn more about scripts here.
Script methods are:
create()Create a new script.retrieve()Retrieve a script by id.list()List all scripts available in your organization.
Speech allows you to do Text-To-Speech (TTS) with our API using all the voices available. Use it to create a speech audio file from your script.
Speech methods are:
create()Send a Text-To-Speech request to our Text-To-Speech service.retrieve()Retrieve the speech file urls.download()Download the speech files in your preferred folder.
Voice allows you to retrieve a list of the available voices from our API.
Voice methods are:
list()List all the available voices in our API.
Sound allows you to design your own sound template.
Available soon.
Mastering allows you to create a mastered version of your audio file.
Available soon.
File allows you to retrieve all the files available in api.audio for your organization.
Available soon.
## CREATE SCRIPT
# Create a script item with two sections (hello, bye) & 2 personalisation parameters (username, location).
# Scripts can have any number of sections.
# Every section will be a separated speech file.
# A script will always follow a hierarchical structure: project/module/script.
# Only required parameter is scriptText.
# projectName, moduleName and scriptName are optional.
# If not provided, projectName, moduleName and scriptName will be "default".
# scriptId is optional.
script = aflr.Script().create(
scriptText="<<sectionName::hello>> Hello {{username|buddy}} <<sectionName::bye>> Good bye from {{location|barcelona}}",
projectName="myProject",
moduleName="myModule",
scriptName="myScript",
scriptId="id-1234"
)
print(script)
## RETRIEVE SCRIPT
# Retrieve the script item and print the script created.
# Required parameter: scriptId
script = aflr.Script().retrieve(scriptId=script["scriptId"])
print(script)
## LIST SCRIPTS
# Retrieve all scripts in your organization and print the list of scripts.
# List does not require any parameter.
# print the scriptText from the first script item found.
scripts = aflr.Script().list()
print(scripts)
print(scripts["scripts"][0]["scriptText"])# create a text-to-speech
response = aflr.Speech().create(scriptId=script["scriptId"])
print(response)
# get the speech audio files
audio_files = aflr.Speech().retrieve(scriptId=script["scriptId"])
print(audio_files)
# download all speech audio files
audio_files = aflr.Speech().download(scriptId=script["scriptId"], destination=".")
# check your folder :) you should have the following audio_files
print(audio_files)
# Get all available voices
all_voices = aflr.Voice().list()
print(all_voices)This project is licensed under the terms of the MIT license.
