Commit e22b2880 authored by 957dd's avatar 957dd

加入了tts播放,但是有bug,会一直播放

parent 5352b4df
File added
...@@ -1025,6 +1025,30 @@ drivers/sensors/audioplay.c.s: ...@@ -1025,6 +1025,30 @@ drivers/sensors/audioplay.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/sensors/audioplay.c.s $(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/sensors/audioplay.c.s
.PHONY : drivers/sensors/audioplay.c.s .PHONY : drivers/sensors/audioplay.c.s
drivers/sensors/audiotts_play.o: drivers/sensors/audiotts_play.c.o
.PHONY : drivers/sensors/audiotts_play.o
# target to build an object file
drivers/sensors/audiotts_play.c.o:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/sensors/audiotts_play.c.o
.PHONY : drivers/sensors/audiotts_play.c.o
drivers/sensors/audiotts_play.i: drivers/sensors/audiotts_play.c.i
.PHONY : drivers/sensors/audiotts_play.i
# target to preprocess a source file
drivers/sensors/audiotts_play.c.i:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/sensors/audiotts_play.c.i
.PHONY : drivers/sensors/audiotts_play.c.i
drivers/sensors/audiotts_play.s: drivers/sensors/audiotts_play.c.s
.PHONY : drivers/sensors/audiotts_play.s
# target to generate assembly for a file
drivers/sensors/audiotts_play.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/sensors/audiotts_play.c.s
.PHONY : drivers/sensors/audiotts_play.c.s
drivers/sensors/infrared_gun.o: drivers/sensors/infrared_gun.c.o drivers/sensors/infrared_gun.o: drivers/sensors/infrared_gun.c.o
.PHONY : drivers/sensors/infrared_gun.o .PHONY : drivers/sensors/infrared_gun.o
...@@ -2000,6 +2024,9 @@ help: ...@@ -2000,6 +2024,9 @@ help:
@echo "... drivers/sensors/audioplay.o" @echo "... drivers/sensors/audioplay.o"
@echo "... drivers/sensors/audioplay.i" @echo "... drivers/sensors/audioplay.i"
@echo "... drivers/sensors/audioplay.s" @echo "... drivers/sensors/audioplay.s"
@echo "... drivers/sensors/audiotts_play.o"
@echo "... drivers/sensors/audiotts_play.i"
@echo "... drivers/sensors/audiotts_play.s"
@echo "... drivers/sensors/infrared_gun.o" @echo "... drivers/sensors/infrared_gun.o"
@echo "... drivers/sensors/infrared_gun.i" @echo "... drivers/sensors/infrared_gun.i"
@echo "... drivers/sensors/infrared_gun.s" @echo "... drivers/sensors/infrared_gun.s"
......
No preview for this file type
CMAKE_PROGRESS_1 = CMAKE_PROGRESS_1 =
CMAKE_PROGRESS_2 = CMAKE_PROGRESS_2 = 81
CMAKE_PROGRESS_3 = 81 CMAKE_PROGRESS_3 =
CMAKE_PROGRESS_4 = CMAKE_PROGRESS_4 =
CMAKE_PROGRESS_5 = 82 CMAKE_PROGRESS_5 = 82
CMAKE_PROGRESS_6 = CMAKE_PROGRESS_6 =
......
...@@ -34,8 +34,8 @@ CMAKE_PROGRESS_33 = ...@@ -34,8 +34,8 @@ CMAKE_PROGRESS_33 =
CMAKE_PROGRESS_34 = CMAKE_PROGRESS_34 =
CMAKE_PROGRESS_35 = 12 CMAKE_PROGRESS_35 = 12
CMAKE_PROGRESS_36 = CMAKE_PROGRESS_36 =
CMAKE_PROGRESS_37 = 13 CMAKE_PROGRESS_37 =
CMAKE_PROGRESS_38 = CMAKE_PROGRESS_38 = 13
CMAKE_PROGRESS_39 = CMAKE_PROGRESS_39 =
CMAKE_PROGRESS_40 = 14 CMAKE_PROGRESS_40 = 14
CMAKE_PROGRESS_41 = CMAKE_PROGRESS_41 =
......
...@@ -7,8 +7,8 @@ CMAKE_PROGRESS_6 = ...@@ -7,8 +7,8 @@ CMAKE_PROGRESS_6 =
CMAKE_PROGRESS_7 = CMAKE_PROGRESS_7 =
CMAKE_PROGRESS_8 = 18 CMAKE_PROGRESS_8 = 18
CMAKE_PROGRESS_9 = CMAKE_PROGRESS_9 =
CMAKE_PROGRESS_10 = 19 CMAKE_PROGRESS_10 =
CMAKE_PROGRESS_11 = CMAKE_PROGRESS_11 = 19
CMAKE_PROGRESS_12 = CMAKE_PROGRESS_12 =
CMAKE_PROGRESS_13 = 20 CMAKE_PROGRESS_13 = 20
CMAKE_PROGRESS_14 = CMAKE_PROGRESS_14 =
...@@ -24,11 +24,11 @@ CMAKE_PROGRESS_23 = ...@@ -24,11 +24,11 @@ CMAKE_PROGRESS_23 =
CMAKE_PROGRESS_24 = CMAKE_PROGRESS_24 =
CMAKE_PROGRESS_25 = 24 CMAKE_PROGRESS_25 = 24
CMAKE_PROGRESS_26 = CMAKE_PROGRESS_26 =
CMAKE_PROGRESS_27 = 25 CMAKE_PROGRESS_27 =
CMAKE_PROGRESS_28 = CMAKE_PROGRESS_28 = 25
CMAKE_PROGRESS_29 = CMAKE_PROGRESS_29 =
CMAKE_PROGRESS_30 = 26 CMAKE_PROGRESS_30 =
CMAKE_PROGRESS_31 = CMAKE_PROGRESS_31 = 26
CMAKE_PROGRESS_32 = CMAKE_PROGRESS_32 =
CMAKE_PROGRESS_33 = 27 CMAKE_PROGRESS_33 = 27
CMAKE_PROGRESS_34 = CMAKE_PROGRESS_34 =
......
...@@ -3,8 +3,8 @@ CMAKE_PROGRESS_2 = ...@@ -3,8 +3,8 @@ CMAKE_PROGRESS_2 =
CMAKE_PROGRESS_3 = CMAKE_PROGRESS_3 =
CMAKE_PROGRESS_4 = 86 CMAKE_PROGRESS_4 = 86
CMAKE_PROGRESS_5 = CMAKE_PROGRESS_5 =
CMAKE_PROGRESS_6 = CMAKE_PROGRESS_6 = 87
CMAKE_PROGRESS_7 = 87 CMAKE_PROGRESS_7 =
CMAKE_PROGRESS_8 = CMAKE_PROGRESS_8 =
CMAKE_PROGRESS_9 = 88 CMAKE_PROGRESS_9 = 88
CMAKE_PROGRESS_10 = CMAKE_PROGRESS_10 =
......
CMAKE_PROGRESS_1 = 54 CMAKE_PROGRESS_1 =
CMAKE_PROGRESS_2 = CMAKE_PROGRESS_2 =
CMAKE_PROGRESS_3 = CMAKE_PROGRESS_3 = 55
CMAKE_PROGRESS_4 = 55 CMAKE_PROGRESS_4 =
CMAKE_PROGRESS_5 = CMAKE_PROGRESS_5 =
CMAKE_PROGRESS_6 = CMAKE_PROGRESS_6 = 56
CMAKE_PROGRESS_7 = 56 CMAKE_PROGRESS_7 =
CMAKE_PROGRESS_8 = CMAKE_PROGRESS_8 =
CMAKE_PROGRESS_9 = 57 CMAKE_PROGRESS_9 = 57
CMAKE_PROGRESS_10 = CMAKE_PROGRESS_10 =
...@@ -14,14 +14,14 @@ CMAKE_PROGRESS_13 = ...@@ -14,14 +14,14 @@ CMAKE_PROGRESS_13 =
CMAKE_PROGRESS_14 = CMAKE_PROGRESS_14 =
CMAKE_PROGRESS_15 = 59 CMAKE_PROGRESS_15 = 59
CMAKE_PROGRESS_16 = CMAKE_PROGRESS_16 =
CMAKE_PROGRESS_17 = CMAKE_PROGRESS_17 = 60
CMAKE_PROGRESS_18 = 60 CMAKE_PROGRESS_18 =
CMAKE_PROGRESS_19 = CMAKE_PROGRESS_19 =
CMAKE_PROGRESS_20 = CMAKE_PROGRESS_20 = 61
CMAKE_PROGRESS_21 = 61 CMAKE_PROGRESS_21 =
CMAKE_PROGRESS_22 = CMAKE_PROGRESS_22 =
CMAKE_PROGRESS_23 = CMAKE_PROGRESS_23 = 62
CMAKE_PROGRESS_24 = 62 CMAKE_PROGRESS_24 =
CMAKE_PROGRESS_25 = CMAKE_PROGRESS_25 =
CMAKE_PROGRESS_26 = 63 CMAKE_PROGRESS_26 = 63
CMAKE_PROGRESS_27 = CMAKE_PROGRESS_27 =
...@@ -34,11 +34,11 @@ CMAKE_PROGRESS_33 = ...@@ -34,11 +34,11 @@ CMAKE_PROGRESS_33 =
CMAKE_PROGRESS_34 = CMAKE_PROGRESS_34 =
CMAKE_PROGRESS_35 = 66 CMAKE_PROGRESS_35 = 66
CMAKE_PROGRESS_36 = CMAKE_PROGRESS_36 =
CMAKE_PROGRESS_37 = CMAKE_PROGRESS_37 = 67
CMAKE_PROGRESS_38 = 67 CMAKE_PROGRESS_38 =
CMAKE_PROGRESS_39 = CMAKE_PROGRESS_39 =
CMAKE_PROGRESS_40 = CMAKE_PROGRESS_40 = 68
CMAKE_PROGRESS_41 = 68 CMAKE_PROGRESS_41 =
CMAKE_PROGRESS_42 = CMAKE_PROGRESS_42 =
CMAKE_PROGRESS_43 = 69 CMAKE_PROGRESS_43 = 69
CMAKE_PROGRESS_44 = CMAKE_PROGRESS_44 =
...@@ -54,8 +54,8 @@ CMAKE_PROGRESS_53 = ...@@ -54,8 +54,8 @@ CMAKE_PROGRESS_53 =
CMAKE_PROGRESS_54 = CMAKE_PROGRESS_54 =
CMAKE_PROGRESS_55 = 73 CMAKE_PROGRESS_55 = 73
CMAKE_PROGRESS_56 = CMAKE_PROGRESS_56 =
CMAKE_PROGRESS_57 = CMAKE_PROGRESS_57 = 74
CMAKE_PROGRESS_58 = 74 CMAKE_PROGRESS_58 =
CMAKE_PROGRESS_59 = CMAKE_PROGRESS_59 =
CMAKE_PROGRESS_60 = 75 CMAKE_PROGRESS_60 = 75
CMAKE_PROGRESS_61 = CMAKE_PROGRESS_61 =
...@@ -71,6 +71,6 @@ CMAKE_PROGRESS_70 = ...@@ -71,6 +71,6 @@ CMAKE_PROGRESS_70 =
CMAKE_PROGRESS_71 = CMAKE_PROGRESS_71 =
CMAKE_PROGRESS_72 = 79 CMAKE_PROGRESS_72 = 79
CMAKE_PROGRESS_73 = CMAKE_PROGRESS_73 =
CMAKE_PROGRESS_74 = CMAKE_PROGRESS_74 = 80
CMAKE_PROGRESS_75 = 80 CMAKE_PROGRESS_75 =
...@@ -108,6 +108,10 @@ void audioplay_cycle(){ ...@@ -108,6 +108,10 @@ void audioplay_cycle(){
} }
audioplay_send_mqtt(); audioplay_send_mqtt();
} }
video_tts_play();
delay_us(500);
} }
} }
......
...@@ -16,6 +16,8 @@ static size_t http_response_len = 0; ...@@ -16,6 +16,8 @@ static size_t http_response_len = 0;
static bool audio_tts_index=0; static bool audio_tts_index=0;
static double g_volume=1.0;
// HTTP 响应回调函数:接收服务器返回的 JSON 数据 // HTTP 响应回调函数:接收服务器返回的 JSON 数据
static size_t http_write_callback(void *ptr, size_t size, size_t nmemb, void *userdata) { static size_t http_write_callback(void *ptr, size_t size, size_t nmemb, void *userdata) {
...@@ -106,13 +108,58 @@ int save_audio_file(const void *data, size_t len, const char *filepath) { ...@@ -106,13 +108,58 @@ int save_audio_file(const void *data, size_t len, const char *filepath) {
return 0; return 0;
} }
/*
*功能:获得随机字符串,不使用uuid
*/
char* generate_random_string() {
// 设置随机种子(建议在main函数中调用一次即可)
static int seeded = 0;
if (!seeded) {
srand((unsigned int)time(NULL));
seeded = 1;
}
// 动态分配内存(10个字符 + 终止符)
char* str = (char*)malloc(11 * sizeof(char));
if (!str) return NULL;
// 生成数字字符(ASCII 48-57)
for (int i = 0; i < 10; i++) {
str[i] = '0' + (rand() % 10);
}
str[10] = '\0'; // 添加终止符
return str;
}
char *get_audio_text_tts(char *date ,bool flag){
static char text[256] = {0};
if(flag==1){
return text;
}else {
size_t date_len = strlen(date);
if (date) {
strcpy(text, date);
text[date_len] = '\0'; // 确保以null结尾
} else {
text[0] = '\0'; // 如果date为NULL,清空文本
}
}
}
/**
* 保存音频数据到文件(目录已存在)
* @param text 传入需要转化的文字指针
* @return 0成功,1失败
*/
int audio_tts(const char *text) { int audio_tts(const char *text) {
// 1. 配置参数(替换为你的真实信息,已验证有效) // 1. 配置参数(替换为你的真实信息,已验证有效)
const char *appid = "5745089507"; // 你的火山引擎 AppID const char *appid = "5745089507"; // 你的火山引擎 AppID
const char *access_token = "0BeoBb_DOjrxfM4aTsJfUFVVS0k5V9mt"; // 你的访问令牌 const char *access_token = "0BeoBb_DOjrxfM4aTsJfUFVVS0k5V9mt"; // 你的访问令牌
const char *voice_type = "zh_female_linjianvhai_moon_bigtts"; // 非流式音色 const char *voice_type = "zh_female_linjianvhai_moon_bigtts"; // 非流式音色
const char *cluster = "volcano_tts"; // 固定值(必选) const char *cluster = "volcano_tts"; // 固定值(必选)
const char *uid = "user123456"; // 任意非空字符串 char *uid = generate_random_string(); // 任意非空字符串
const char *encoding = "mp3"; // 输出格式(mp3/wav/pcm/ogg_opus) const char *encoding = "mp3"; // 输出格式(mp3/wav/pcm/ogg_opus)
const char *endpoint = "https://openspeech.bytedance.com/api/v1/tts"; // HTTP 接口地址 const char *endpoint = "https://openspeech.bytedance.com/api/v1/tts"; // HTTP 接口地址
...@@ -125,6 +172,7 @@ int audio_tts(const char *text) { ...@@ -125,6 +172,7 @@ int audio_tts(const char *text) {
cJSON *root = cJSON_CreateObject(); cJSON *root = cJSON_CreateObject();
if (!root) { if (!root) {
my_zlog_error("Create JSON root failed"); my_zlog_error("Create JSON root failed");
free(uid);
return 1; return 1;
} }
...@@ -165,6 +213,7 @@ int audio_tts(const char *text) { ...@@ -165,6 +213,7 @@ int audio_tts(const char *text) {
cJSON_Delete(root); cJSON_Delete(root);
if (!payload) { if (!payload) {
my_zlog_error("Generate JSON payload failed"); my_zlog_error("Generate JSON payload failed");
free(uid);
return 1; return 1;
} }
my_zlog_info("Request Payload: %s", payload); my_zlog_info("Request Payload: %s", payload);
...@@ -175,6 +224,7 @@ int audio_tts(const char *text) { ...@@ -175,6 +224,7 @@ int audio_tts(const char *text) {
if (!curl) { if (!curl) {
my_zlog_error("Initialize curl failed"); my_zlog_error("Initialize curl failed");
free(payload); free(payload);
free(uid);
return 1; return 1;
} }
...@@ -186,6 +236,12 @@ int audio_tts(const char *text) { ...@@ -186,6 +236,12 @@ int audio_tts(const char *text) {
headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Accept: application/json"); headers = curl_slist_append(headers, "Accept: application/json");
if (http_response == NULL) {
http_response_len = 0;
}else {
return 1;
}
// curl 核心配置 // curl 核心配置
curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_URL, endpoint);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
...@@ -199,10 +255,11 @@ int audio_tts(const char *text) { ...@@ -199,10 +255,11 @@ int audio_tts(const char *text) {
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); // 连接超时 10 秒 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); // 连接超时 10 秒
// 5. 执行 HTTP 请求 // 5. 执行 HTTP 请求
printf("Sending request to %s...\n", endpoint); my_zlog_info("Sending request to %s...", endpoint);
CURLcode res = curl_easy_perform(curl); CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) { if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform failed: %s\n", curl_easy_strerror(res)); my_zlog_error("curl_easy_perform failed: %s", curl_easy_strerror(res));
free(uid);
free(payload); free(payload);
curl_slist_free_all(headers); curl_slist_free_all(headers);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
...@@ -211,9 +268,10 @@ int audio_tts(const char *text) { ...@@ -211,9 +268,10 @@ int audio_tts(const char *text) {
} }
// 6. 解析 HTTP 响应(JSON 格式) // 6. 解析 HTTP 响应(JSON 格式)
printf("Response Length: %zu bytes\n", http_response_len); my_zlog_info("Response Length: %zu bytes", http_response_len);
if (http_response_len == 0) { if (http_response_len == 0) {
fprintf(stderr, "Empty response from server\n"); my_zlog_error("Empty response from server");
free(uid);
free(payload); free(payload);
curl_slist_free_all(headers); curl_slist_free_all(headers);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
...@@ -224,7 +282,8 @@ int audio_tts(const char *text) { ...@@ -224,7 +282,8 @@ int audio_tts(const char *text) {
// 解析 JSON 响应 // 解析 JSON 响应
cJSON *response_json = cJSON_Parse(http_response); cJSON *response_json = cJSON_Parse(http_response);
if (!response_json) { if (!response_json) {
fprintf(stderr, "Parse response JSON failed: %s\n", cJSON_GetErrorPtr()); my_zlog_error("Parse response JSON failed: %s", cJSON_GetErrorPtr());
free(uid);
free(payload); free(payload);
free(http_response); free(http_response);
curl_slist_free_all(headers); curl_slist_free_all(headers);
...@@ -242,10 +301,11 @@ int audio_tts(const char *text) { ...@@ -242,10 +301,11 @@ int audio_tts(const char *text) {
cJSON *code = cJSON_GetObjectItem(response_json, "code"); cJSON *code = cJSON_GetObjectItem(response_json, "code");
cJSON *message = cJSON_GetObjectItem(response_json, "message"); cJSON *message = cJSON_GetObjectItem(response_json, "message");
if (!code || code->valueint != 3000 || !message || strcmp(message->valuestring, "Success") != 0) { if (!code || code->valueint != 3000 || !message || strcmp(message->valuestring, "Success") != 0) {
fprintf(stderr, "Request failed! Code: %d, Message: %s\n", my_zlog_error("Request failed! Code: %d, Message: %s",
code ? code->valueint : -1, code ? code->valueint : -1,
message ? message->valuestring : "No message"); message ? message->valuestring : "No message");
cJSON_Delete(response_json); cJSON_Delete(response_json);
free(uid);
free(payload); free(payload);
free(http_response); free(http_response);
curl_slist_free_all(headers); curl_slist_free_all(headers);
...@@ -253,13 +313,14 @@ int audio_tts(const char *text) { ...@@ -253,13 +313,14 @@ int audio_tts(const char *text) {
curl_global_cleanup(); curl_global_cleanup();
return 1; return 1;
} }
printf("Request succeeded! Code: %d, Message: %s\n", code->valueint, message->valuestring); my_zlog_info("Request succeeded! Code: %d, Message: %s", code->valueint, message->valuestring);
// 关键:直接提取 data 字段的 Base64 音频字符串(接口实际返回格式) // 关键:直接提取 data 字段的 Base64 音频字符串(接口实际返回格式)
cJSON *audio_base64 = cJSON_GetObjectItem(response_json, "data"); cJSON *audio_base64 = cJSON_GetObjectItem(response_json, "data");
if (!audio_base64 || !audio_base64->valuestring || strlen(audio_base64->valuestring) == 0) { if (!audio_base64 || !audio_base64->valuestring || strlen(audio_base64->valuestring) == 0) {
fprintf(stderr, "Response missing audio data (data field is empty)\n"); my_zlog_error("Response missing audio data (data field is empty)");
cJSON_Delete(response_json); cJSON_Delete(response_json);
free(uid);
free(payload); free(payload);
free(http_response); free(http_response);
curl_slist_free_all(headers); curl_slist_free_all(headers);
...@@ -267,7 +328,7 @@ int audio_tts(const char *text) { ...@@ -267,7 +328,7 @@ int audio_tts(const char *text) {
curl_global_cleanup(); curl_global_cleanup();
return 1; return 1;
} }
my_zlog_info("Audio Base64 Length: %zu bytes\n", strlen(audio_base64->valuestring)); my_zlog_info("Audio Base64 Length: %zu bytes", strlen(audio_base64->valuestring));
// 7. Base64 解码音频数据 // 7. Base64 解码音频数据
size_t audio_len = 0; size_t audio_len = 0;
...@@ -275,6 +336,7 @@ int audio_tts(const char *text) { ...@@ -275,6 +336,7 @@ int audio_tts(const char *text) {
if (!audio_data || audio_len == 0) { if (!audio_data || audio_len == 0) {
my_zlog_error("Base64 decode audio failed"); my_zlog_error("Base64 decode audio failed");
cJSON_Delete(response_json); cJSON_Delete(response_json);
free(uid);
free(payload); free(payload);
free(http_response); free(http_response);
curl_slist_free_all(headers); curl_slist_free_all(headers);
...@@ -290,6 +352,7 @@ int audio_tts(const char *text) { ...@@ -290,6 +352,7 @@ int audio_tts(const char *text) {
// 9. 清理资源(避免内存泄漏) // 9. 清理资源(避免内存泄漏)
cJSON_Delete(response_json); cJSON_Delete(response_json);
free(uid);
free(audio_data); free(audio_data);
free(payload); free(payload);
free(http_response); free(http_response);
...@@ -297,13 +360,123 @@ int audio_tts(const char *text) { ...@@ -297,13 +360,123 @@ int audio_tts(const char *text) {
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_global_cleanup(); curl_global_cleanup();
// 初始化响应缓冲
http_response = NULL;
http_response_len = 0;
return 0; return 0;
} }
/*
*功能: 设置播放标识
*/
void set_audio_tts_index(bool index){ void set_audio_tts_index(bool index){
audio_tts_index=index; audio_tts_index=index;
} }
bool get_audio_tts_index(){ /*
return audio_tts_index; *功能: 发送mqtt
*/
int mqtt_audio_tts_send(int status){
// 创建根对象
cJSON* root = cJSON_CreateObject();
if (root == NULL) {
return 1;
}
// 创建body对象
cJSON* body = cJSON_CreateObject();
if (body == NULL) {
cJSON_Delete(root);
return 1;
}
cJSON *head = cJSON_CreateObject();
if (head == NULL) {
cJSON_Delete(root);
return 1;
}
// 将body添加到root
cJSON_AddItemToObject(root, "body", body);
// 添加type和status字段到body
cJSON_AddStringToObject(body, "type", "tts_audio");
cJSON_AddNumberToObject(body, "status", status);
cJSON_AddNumberToObject(head, "message_type",3020);
cJSON_AddItemToObject(root, "body", body);
cJSON_AddItemToObject(root, "head",head);
// 生成JSON字符串
char* json_str = cJSON_PrintUnformatted(root);
my_zlog_debug("%s",json_str);
for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(json_str), json_str, 0, false);
}
// 释放 JSON 对象内存
cJSON_Delete(root);
return 0;
}
int mqtt_audio_tts(cJSON *body){
// 解析type字段
cJSON* type = cJSON_GetObjectItem(body, "type");
if(type == NULL || strcmp(type->valuestring, "tts_audio") != 0){
my_zlog_error("type is tts_audio error");
return 1;
}
// 解析date字段
cJSON* date = cJSON_GetObjectItem(body, "date");
if(date!=NULL){
get_audio_text_tts(date->valuestring,0);
my_zlog_info("text:%s",date->valuestring);
}else {
return 1;
}
// 解析status字段
cJSON* status = cJSON_GetObjectItem(body, "status");
if(mqtt_audio_tts_send(status->valueint)==0){
set_audio_tts_index(status->valueint);
}else{
set_audio_tts_index(0);
my_zlog_error("statis error");
return 1;
}
cJSON* volume = cJSON_GetObjectItem(body, "volume");
g_volume = volume->valuedouble;
return 0;
}
/*
*功能,tts播放
*/
void video_tts_play(){
if(audio_tts_index == 1) {
audio_tts(get_audio_text_tts(NULL,1));
char command[256];
snprintf(command, sizeof(command),
"ffmpeg -i /home/orangepi/car/master/audio.mp3 -af \"volume=%.1f\" -f alsa default",
g_volume);
int res = system(command);
if(res == 0)
my_zlog_info("play success, volume: %.1f", g_volume);
else
my_zlog_error("system play fail, volume: %.1f", g_volume);
audio_tts_index == 0;
}
} }
\ No newline at end of file
...@@ -3,8 +3,14 @@ ...@@ -3,8 +3,14 @@
#include "common.h" #include "common.h"
void set_audio_tts_index(bool index); /*
*将接收到的保存到指定文件
*/
int mqtt_audio_tts(cJSON *body);
int audio_tts(const char *text); /*
*功能,tts播放
*/
void video_tts_play();
bool get_audio_tts_index(); #endif
\ No newline at end of file
...@@ -337,7 +337,10 @@ int device_message_receive(cJSON *json){//接收到的控制设备的mqtt消息 ...@@ -337,7 +337,10 @@ int device_message_receive(cJSON *json){//接收到的控制设备的mqtt消息
break; break;
case 2026: case 2026:
receive_self_contorl_date_mqtt(body); receive_self_contorl_date_mqtt(body);
my_zlog_debug("device self comtrol date save"); my_zlog_info("device self comtrol date save");
case 2027:
mqtt_audio_tts(body);
my_zlog_info("device audio_tts");
default: default:
break; break;
} }
......
...@@ -244,35 +244,29 @@ void mqtt_clean(){ ...@@ -244,35 +244,29 @@ void mqtt_clean(){
int mqtt_cycle() {//非阻塞型 int mqtt_cycle() {//非阻塞型
my_zlog_info("MQTT 异步监控线程启动");
int check_interval = 30000; // 30秒检查一次,减少日志频率
while (1) { while (1) {
// bool all_connected = true; // 简单的存活检查,记录连接状态
int active_count = 0;
// for (int i = 0; i < g_mqtt_cam_config_t->mqtt_count; i++) { int total_count = g_mqtt_cam_config_t ? g_mqtt_cam_config_t->mqtt_count : 0;
// if (!g_clients_t[i].mosq) continue;
for (int i = 0; i < total_count; i++) {
// int state = mosquitto_socket(g_clients_t[i].mosq); if (g_clients_t[i].mosq && !g_clients_t[i].permanently_failed) {
// if (state == -1) { active_count++;
// all_connected = false; }
// my_zlog_warn("检测到 MQTT [%s:%d] 已断开,尝试重连...", }
// g_clients_t[i].host, g_clients_t[i].port);
// //mosquitto_reconnect_async(g_clients_t[i].mosq); // 只在状态变化或定期记录
// } static int last_active_count = -1;
// } if (active_count != last_active_count) {
my_zlog_info("MQTT 连接状态: %d/%d 活跃", active_count, total_count);
delay_ms(1000); // 每 1 秒检测一次连接状态 last_active_count = active_count;
}
delay_ms(check_interval);
} }
// while (1) {
// for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
// int rc= mosquitto_loop(g_clients_t[i].mosq, 17, 1); // 每秒检查一次
// if(rc != MOSQ_ERR_SUCCESS){
// my_zlog_warn("服务器 %s:%d 断开,尝试重连...",
// g_clients_t[i].host, g_clients_t[i].port);
// mosquitto_reconnect(g_clients_t[i].mosq);
// }
// }
// delay_us(50);
// }
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment