bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc117.c (2735B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 08 Feb 2026
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Write a program to convert a given text into an audio file using OpenAI Audio API (TTS).
      9 */
     10 /* Let Us C, Chap- 24 (Interaction with ChatGPT through C), Qn No.: B(a) */
     11 
     12 /* This file is auto-generated by a bot. */
     13 /* This code is not compiled; it is for reference only. */
     14 
     15 /* NOTE: These programs require the 'libcurl' library to compile.
     16    Command: gcc luc117.c -o output -lcurl */
     17 
     18 
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include <string.h>
     22 #include <curl/curl.h>
     23 
     24 /* Pre-requisites:
     25    1. Install libcurl development package.
     26    2. Get OpenAI API Key.
     27 */
     28 
     29 #define API_KEY "YOUR_OPENAI_API_KEY_HERE"
     30 
     31 size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
     32     size_t written = fwrite(ptr, size, nmemb, stream);
     33     return written;
     34 }
     35 
     36 int main(void) {
     37     CURL *curl;
     38     CURLcode res;
     39     FILE *fp;
     40     
     41     // JSON Payload construction
     42     const char *url = "https://api.openai.com/v1/audio/speech";
     43     const char *data = "{"
     44                        "\"model\": \"tts-1\","
     45                        "\"input\": \"Hello! This is a C program talking to you.\","
     46                        "\"voice\": \"alloy\""
     47                        "}";
     48 
     49     struct curl_slist *headers = NULL;
     50     char auth_header[100];
     51     sprintf(auth_header, "Authorization: Bearer %s", API_KEY);
     52 
     53     curl_global_init(CURL_GLOBAL_ALL);
     54     curl = curl_easy_init();
     55 
     56     if(curl) {
     57         // Set Headers
     58         headers = curl_slist_append(headers, "Content-Type: application/json");
     59         headers = curl_slist_append(headers, auth_header);
     60         
     61         // Open file to save audio
     62         fp = fopen("output_audio.mp3", "wb");
     63         if(!fp) {
     64             printf("Error opening file for writing.\n");
     65             return 1;
     66         }
     67 
     68         // Configure CURL
     69         curl_easy_setopt(curl, CURLOPT_URL, url);
     70         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
     71         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
     72         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
     73         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
     74 
     75         // Perform Request
     76         printf("Sending request to OpenAI TTS API...\n");
     77         res = curl_easy_perform(curl);
     78 
     79         if(res != CURLE_OK)
     80             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
     81         else
     82             printf("Audio saved to 'output_audio.mp3' successfully.\n");
     83 
     84         // Cleanup
     85         fclose(fp);
     86         curl_slist_free_all(headers);
     87         curl_easy_cleanup(curl);
     88     }
     89     
     90     curl_global_cleanup();
     91     return 0;
     92 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror