Sample code for 30+ languages & platforms
Xbase++

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

See more IBM Text to Speech Examples

Synthesizes text to audio that is spoken in the specified voice.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL cMyBaseUrl
LOCAL oSbUrl
LOCAL nNumReplaced
LOCAL cTextToSynthesize
LOCAL oBdResponseBody
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

//  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?accept=ACCEPT_TYPE&voice=CHOSEN_VOICE&text=TEXT_TO_SYNTHESIZE")

//  Choose "audio/wav" for the output file type.
nNumReplaced := oSbUrl:Replace("ACCEPT_TYPE", oHttp:UrlEncode("audio/wav"))
//  See the choices for voices at https://cloud.ibm.com/apidocs/text-to-speech#synthesize-audio-get
nNumReplaced := oSbUrl:Replace("CHOSEN_VOICE", "en-US_MichaelVoice")
cTextToSynthesize := "When life gives you lemons, order the lobster tail."
nNumReplaced := oSbUrl:Replace("TEXT_TO_SYNTHESIZE", oHttp:UrlEncode(cTextToSynthesize))

//  Send the GET to synthesize the voice.
//  The response file will be contained in bdResponseBody
oBdResponseBody := CreateObject("Chilkat.BinData")
nSuccess := oHttp:QuickGetBd(oSbUrl:GetAsString(), oBdResponseBody)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oSbUrl:destroy()
    oBdResponseBody:destroy()
    RETURN
ENDIF

nRespStatusCode := oHttp:LastStatus
? "response status code = " + Str(nRespStatusCode)

IF (nRespStatusCode == 200)
    nSuccess := oBdResponseBody:WriteFile("qa_output/lobster.wav")
    ? "Success!"
ELSE
    ? oBdResponseBody:GetString("utf-8")
ENDIF

oHttp:destroy()
oSbUrl:destroy()
oBdResponseBody:destroy()