Sample code for 30+ languages & platforms
Tcl

IBM Cloud - Text to Speech - Synthesize Audio (POST)

See more IBM Text to Speech Examples

Synthesizes text to audio that is spoken in the specified voice. Uses a POST which allows for more text to be synthesized.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set http [new_CkHttp]

CkHttp_put_Login $http "apikey"
CkHttp_put_Password $http "my_apikey"
CkHttp_put_BasicAuth $http 1

# Send the following request:

# curl -X POST -u "apikey:{apikey}" 
#    --header "Content-Type: application/json" 
#    --header "Accept: audio/wav" 
#    --data "{\"text\":\"When life gives you lemons, order the lobster tail.\"}" 
#    --output lobster.wav "{url}/v1/synthesize?voice=en-US_AllisonV3Voice"

# Use your base URL shown in the credentials web page (below your apikey)
set myBaseUrl "https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/31941e96-7b89-4d56-8993-9cd8f18ec2d8"

set sbUrl [new_CkStringBuilder]

CkStringBuilder_Append $sbUrl $myBaseUrl
CkStringBuilder_Append $sbUrl "/v1/synthesize?voice=en-US_AllisonV3Voice"

CkHttp_put_Accept $http "audio/wav"

set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "text" "When life gives you lemons, order the lobster tail."

set url [CkStringBuilder_getAsString $sbUrl]
set resp [new_CkHttpResponse]

set success [CkHttp_HttpJson $http "POST" $url $json "application/json" $resp]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkStringBuilder $sbUrl
    delete_CkJsonObject $json
    delete_CkHttpResponse $resp
    exit
}

set respStatusCode [CkHttpResponse_get_StatusCode $resp]
puts "response status code = $respStatusCode"

if {$respStatusCode == 200} then {
    set success [CkHttpResponse_SaveBodyBinary $resp "qa_output/lobster.wav"]
    puts "Success!"
} else {
    puts [CkHttpResponse_bodyStr $resp]
}


delete_CkHttp $http
delete_CkStringBuilder $sbUrl
delete_CkJsonObject $json
delete_CkHttpResponse $resp