Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL cMyBaseUrl
LOCAL oSbUrl
LOCAL oJson
LOCAL cUrl
LOCAL oResp
LOCAL nRespStatusCode

nSuccess := 0

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

oHttp := CreateObject("Chilkat.Http")

oHttp:Login := "apikey"
oHttp:Password := "my_apikey"
oHttp:BasicAuth := 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)
cMyBaseUrl := "https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/31941e96-7b89-4d56-8993-9cd8f18ec2d8"

oSbUrl := CreateObject("Chilkat.StringBuilder")
oSbUrl:Append(cMyBaseUrl)
oSbUrl:Append("/v1/synthesize?voice=en-US_AllisonV3Voice")

oHttp:Accept := "audio/wav"

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("text", "When life gives you lemons, order the lobster tail.")

cUrl := oSbUrl:GetAsString()
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", cUrl, oJson, "application/json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oSbUrl:destroy()
    oJson:destroy()
    oResp:destroy()
    RETURN
ENDIF

nRespStatusCode := oResp:StatusCode
? "response status code = " + Str(nRespStatusCode)

IF (nRespStatusCode == 200)
    nSuccess := oResp:SaveBodyBinary("qa_output/lobster.wav")
    ? "Success!"
ELSE
    ? oResp:BodyStr
ENDIF

oHttp:destroy()
oSbUrl:destroy()
oJson:destroy()
oResp:destroy()