2 Star 3 Fork 1

刘煜 / 竹间智能机器人客户端

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
baidu_tts.c 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
刘煜 提交于 2019-10-24 15:41 . suppress debug output
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "cJSON.h"
#define APPKEY "rKCHBLmYiFPuCQTS0HttLbUD"
#define SECRETKEY "037dc446820ec143d1628c20146b9d34"
extern CURL* curl;
char* get_token(const char* appkey, const char* secretkey)
{
char* request;
char* response;
size_t resplen;
asprintf(&request, "grant_type=client_credentials&client_id=%s&client_secret=%s", appkey, secretkey);
FILE* fp = open_memstream(&response, &resplen);
if (!fp)
perror("open_memstream");
curl_easy_setopt(curl, CURLOPT_URL, "http://openapi.baidu.com/oauth/2.0/token");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
puts(curl_easy_strerror(res));
fclose(fp);
cJSON* resp = cJSON_Parse(response);
//puts(cJSON_Print(resp));
cJSON* token = cJSON_GetObjectItemCaseSensitive(resp, "access_token");
char* retval = strdup(token->valuestring);
free(response);
cJSON_Delete(resp);
return retval;
}
void tts_run(char* input)
{
char* request;
char* token = get_token(APPKEY, SECRETKEY);
char* temp = curl_easy_escape(curl, input, strlen(input));
char* text = curl_easy_escape(curl, temp, strlen(temp));
curl_free(temp);
asprintf(&request, "ctp=1&lan=zh&cuid=1234&tok=%s&tex=%s&per=0&spd=5&pit=5&vol=5&aue=6", token, text);
//puts(request);
FILE* fp = fopen("tts.wav", "w+");
if (!fp)
perror("fopen");
curl_easy_setopt(curl, CURLOPT_URL, "http://tsn.baidu.com/text2audio");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
puts(curl_easy_strerror(res));
fclose(fp);
curl_free(text);
free(token);
system("aplay -q tts.wav");
}
C
1
https://gitee.com/tinytaro/emotibot_client.git
git@gitee.com:tinytaro/emotibot_client.git
tinytaro
emotibot_client
竹间智能机器人客户端
master

搜索帮助