Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    const wchar_t *myBaseUrl;
    HCkStringBuilderW sbUrl;
    HCkJsonObjectW json;
    const wchar_t *url;
    HCkHttpResponseW resp;
    int respStatusCode;

    success = FALSE;

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

    http = CkHttpW_Create();

    CkHttpW_putLogin(http,L"apikey");
    CkHttpW_putPassword(http,L"my_apikey");
    CkHttpW_putBasicAuth(http,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)
    myBaseUrl = L"https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/31941e96-7b89-4d56-8993-9cd8f18ec2d8";

    sbUrl = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbUrl,myBaseUrl);
    CkStringBuilderW_Append(sbUrl,L"/v1/synthesize?voice=en-US_AllisonV3Voice");

    CkHttpW_putAccept(http,L"audio/wav");

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"text",L"When life gives you lemons, order the lobster tail.");

    url = CkStringBuilderW_getAsString(sbUrl);
    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"POST",url,json,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbUrl);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"response status code = %d\n",respStatusCode);

    if (respStatusCode == 200) {
        success = CkHttpResponseW_SaveBodyBinary(resp,L"qa_output/lobster.wav");
        wprintf(L"Success!\n");
    }
    else {
        wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
    }



    CkHttpW_Dispose(http);
    CkStringBuilderW_Dispose(sbUrl);
    CkJsonObjectW_Dispose(json);
    CkHttpResponseW_Dispose(resp);

    }