Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

http.login = "apikey"
http.password = "my_apikey"
http.basic_auth = true

-- 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)
local myBaseUrl = "https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/31941e96-7b89-4d56-8993-9cd8f18ec2d8"

local sbUrl = chilkat.StringBuilder.new()
sbUrl:append(myBaseUrl)
sbUrl:append("/v1/synthesize?voice=en-US_AllisonV3Voice")

http.accept = "audio/wav"

local json = chilkat.JsonObject.new()
json:update_string("text", "When life gives you lemons, order the lobster tail.")

local url = sbUrl:get_as_string()
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", url, json, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

local respStatusCode = resp.status_code
print("response status code = " .. respStatusCode)

if respStatusCode == 200 then
    success = resp:save_body_binary("qa_output/lobster.wav")
    print("Success!")
else
    print(resp.body_str)
end