Example: curl example

This example shows:

  1. How to use curl and jq in a shell script to access the Daisys API.

  2. How to create the synchronous client using a context manager.

  3. Get a list of voices and select the last one.

  4. Reference the voice to generate audio (a “take”) for some text.

  5. Download the resulting audio.

  6. Play the audio using aplay (Linux).

To run it, you must supply your email and password in the respecting environment variables, as shown below.

Requires: curl, jq.

Example output
$ curl -O https://raw.githubusercontent.com/daisys-ai/daisys-api-python/main/examples/curl_example.sh
$ jq --version  # "jq" is needed for the example program to parse API responses
$ export DAISYS_EMAIL=user@example.com
$ export DAISYS_PASSWORD=example_password123
$ bash examples/curl_example.sh
Found Daisys Speak API  {"version":1,"minor":0}
GET https://api.daisys.ai/v1/speak/voices
"Deirdre" is speaking!
POST https://api.daisys.ai/v1/speak/takes/generate: {"voice_id": "v01hasgezqjcsnc91zdfzpx0apj",
"text": "Hello there, I am Daisys!", "prosody": {"pace": -8, "pitch": 2, "expression": 8}}
Take is "waiting".
GET https://api.daisys.ai/v1/speak/takes/t01hawm80qzj60bf2w9z0np7wej
Take is "started".
GET https://api.daisys.ai/v1/speak/takes/t01hawm80qzj60bf2w9z0np7wej
Take is "ready".
Getting audio!
GET https://api.daisys.ai/v1/speak/takes/t01hawm80qzj60bf2w9z0np7wej/wav
Wrote 'hello_daisys.wav'.
Playing WAVE 'hello_daisys.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono

The “Playing” message will only appear if you have the aplay command installed, otherwise you may play the resulting hello_daisys.wav file in any audio player.

examples/curl_example.sh
 1#!/bin/bash
 2
 3# The following is an example of how to use the Daisys API for generating a voice and then
 4# using it in a speech generation task using the "curl" program.  The API generates
 5# "takes" representing one or more sentences from a speaker.
 6
 7# This program downloads the resulting .wav file and tries to play it using "aplay" if
 8# that program is available.
 9
10set -e  # Stop if we hit any problems along the way.
11
12EMAIL="${DAISYS_EMAIL:=user@example.com}"
13PASSWORD="${DAISYS_PASSWORD:=example_password}"
14
15DAISYS_AUTH="${DAISYS_AUTH_URL:=https://api.daisys.ai}"
16DAISYS="${DAISYS_API_URL:=https://api.daisys.ai}"
17API="$DAISYS/v1"
18SPEAK="$API/speak"
19
20TOKEN=$(curl -s -X POST -H 'Content-Type: application/json' -d '{"email": "'$EMAIL'", "password": "'$PASSWORD'"}' $DAISYS_AUTH/auth/login | jq -r ".access_token")
21
22AUTH="Authorization: Bearer $TOKEN"
23
24# Some functions for authenticated GET and POST methods using curl.
25speak_get() {
26    echo "GET $SPEAK/$1" >/dev/stderr
27    curl -s -L -H "$AUTH" "$SPEAK/$1"
28}
29speak_post() {
30    echo "POST $SPEAK/$1: $2" >/dev/stderr
31    curl -s -H "Content-Type: application/json" -H "$AUTH" -d "$2" "$SPEAK/$1"
32}
33
34VERSION=$(curl -s $API/speak/version)
35echo 'Found Daisys Speak API ' $VERSION
36
37# Get a list of all voices, select the last one.
38VOICE=$(speak_get voices | jq '.[-1]')
39if [ "$VOICE" = null ]; then
40    echo No voices found.
41    MODEL=$(speak_get models | jq '.[-1]')
42    if [ "$MODEL" = null ]; then
43        echo No models found.
44        exit 1
45    fi
46    echo Using model $(echo $MODEL | jq .displayname)
47    echo Generating a voice.
48    VOICE=$(speak_post voices/generate '{"name": "Tina", "gender": "female", "model": '$(echo $MODEL | jq .name)'}')
49fi
50echo "$(echo $VOICE | jq .name) is speaking!"
51VOICE_ID="$(echo $VOICE | jq .voice_id)"
52
53TAKE=$(speak_post takes/generate '{"voice_id": '$VOICE_ID', "text": "Hello there, I am Daisys!", "prosody": {"pace": -8, "pitch": 2, "expression": 8}}')
54TAKE_ID="$(echo $TAKE | jq -r .take_id)"
55echo "Take is $(echo $TAKE | jq .status)."
56while [ $(echo $TAKE | jq -r .status) != 'ready' ] && [ $(echo $TAKE | jq -r .status) != 'error' ]; do
57    sleep 0.5
58    TAKE=$(speak_get takes/$TAKE_ID)
59    echo "Take is $(echo $TAKE | jq .status)."
60done
61
62echo "Getting audio!"
63speak_get takes/$TAKE_ID/wav > hello_daisys.wav
64echo "Wrote 'hello_daisys.wav'."
65
66# Play the audio if we have aplay (Linux), otherwise just print a nice message.
67if which aplay >/dev/null; then
68    aplay hello_daisys.wav
69else
70    echo "aplay not found, but audio was written to 'hello_daisys.wav'."
71fi