Guest User

Google Speech API with libcurl

a guest
Aug 17th, 2014
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. // Aralox - http://stackoverflow.com/questions/25307487/how-to-use-libcurl-with-google-speech-api-what-is-the-equivalent-for-data-bin/25310710#25310710
  2.  
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <curl/curl.h>
  6. #include <direct.h>
  7. #include <string>
  8.  
  9. struct WriteThis {
  10.     const char *readptr;
  11.     long sizeleft;
  12. };
  13.  
  14. // callback function from http://curl.haxx.se/libcurl/c/post-callback.html
  15. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  16. {
  17.     struct WriteThis *pooh = (struct WriteThis *)userp;
  18.  
  19.     if (size*nmemb < 1)
  20.         return 0;
  21.  
  22.     if (pooh->sizeleft) {
  23.         *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
  24.         pooh->readptr++;                 /* advance pointer */
  25.         pooh->sizeleft--;                /* less data left */
  26.         return 1;                        /* we return 1 byte at a time! */
  27.     }
  28.  
  29.     return 0;                          /* no more data left to deliver */
  30. }
  31.  
  32. int main(void)
  33. {
  34.     CURL *curl;         // curl handle
  35.     CURLcode res;
  36.  
  37.     curl = curl_easy_init();
  38.     if (curl)
  39.     {
  40.         FILE *file;
  41.         errno_t err = fopen_s(&file, "testaudio.wav", "r");
  42.         fseek(file, 0, SEEK_END);
  43.         int fileSize = ftell(file);
  44.         fseek(file, 0, SEEK_SET);
  45.  
  46.         std::cout << "file open status: " << err << std::endl;
  47.         std::cout << "file " << fileSize << std::endl;
  48.  
  49.         struct curl_slist *chunk = NULL;
  50.         chunk = curl_slist_append(chunk, "Content-Type: audio/l16; rate=44100");
  51.  
  52.         char *audioData = (char*)malloc(fileSize);
  53.         struct WriteThis pooh;
  54.         fread(audioData, fileSize, 1, file);
  55.         fclose(file);
  56.  
  57.         pooh.readptr = audioData;
  58.         pooh.sizeleft = fileSize;
  59.  
  60.         std::string sizeHeader = "Content-Length: ";
  61.         sizeHeader += std::to_string(fileSize);
  62.         chunk = curl_slist_append(chunk, sizeHeader.c_str());
  63.  
  64.         std::cout << curl_easy_setopt(curl, CURLOPT_POST, 1L) << std::endl;
  65.         curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  66.         curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
  67.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  68.  
  69.         std::cout << curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk) << std::endl;
  70.         std::cout << curl_easy_setopt(curl, CURLOPT_CAINFO, "ca-bundle.crt") << std::endl;
  71.         std::cout << curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=yourkey") << std::endl;
  72.  
  73.         res = curl_easy_perform(curl);
  74.         std::cout << res;
  75.         curl_easy_cleanup(curl);
  76.  
  77.     }
  78.     return 0;
  79. }
Add Comment
Please, Sign In to add comment