当前位置: 首页 > news >正文

ESP32调用本地部署的ChatTTS

1 本地部署ChatTTS

参考 windows10部署ChatTTS+Apifox调用

2 实现过程

部署好本地ChatTTS模型后,测试接口正常。让ESP32接入局域网络,发送post请求生成wav文件,ESP32通过远程获取文件来得到音频文件,并播放音频文件。

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>const char* ssid = "EzhanNet";
const char* password = "11111111";
const char* serverUrl = "http://192.168.0.121:9994/tts";void sendPostTTSRequest() {if (WiFi.status() == WL_CONNECTED) {HTTPClient http;http.begin(serverUrl);  // 目标URLString payload = "text=windows&voice=1111&top_p=0.7&top_k=20&temperature=0.3&custom_voice=3531&refine_max_new_token=384";http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // 设置请求头int httpResponseCode = http.POST(payload); // 发送 POST 请求if (httpResponseCode > 0) {String response = http.getString(); // 获取响应Serial.printf("Response code: %d\n", httpResponseCode);// Serial.println("Response: " + response);getWavFile(response);} else {Serial.println("发送 POST 时出错: " + String(httpResponseCode));}http.end(); // 结束请求} else {Serial.println("WiFi not connected");}
}void getWavFile(String response) {// 解析 JSON 响应DynamicJsonDocument docJson(4096);DeserializationError error = deserializeJson(docJson, response);if (!error) {const char* response_info = docJson["url"];Serial.println("chattts speak file: " + String(response_info));HTTPClient http2;http2.begin(String(response_info));int httpCode = http2.GET();if(httpCode > 0) {// HTTP header has been send and Server response header has been handledSerial.printf("[HTTP] GET... code: %d\n", httpCode);// file found at serverif(httpCode == HTTP_CODE_OK) {String payload = http2.getString();Serial.println(payload);}} else {Serial.printf("[HTTP] GET... failed, error: %s\n", http2.errorToString(httpCode).c_str());}http2.end();} else {Serial.println("解析 JSON 失败: " + String(error.c_str()));}
}void setup() {Serial.begin(115200);// 连接到WiFiWiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.println("Connecting to WiFi...");}Serial.println("Connected to WiFi");Serial.print("IP : ");Serial.println(WiFi.localIP());sendPostTTSRequest();
}void loop() {
}

http://www.mrgr.cn/news/31843.html

相关文章:

  • C++第1课——输出、变量和输入(含视频讲解)
  • 我与Linux的爱恋:命令行参数|环境变量
  • 2024CSP-J初赛
  • string类(C++)
  • LeetCode 滑动窗口 最少交换次数来组合所有的 1 II
  • 湖北师范大学-Java入门 - 分支结构
  • 基于单片机汽车驾驶防瞌睡防疲劳报警器自动熄火设计
  • 浅谈Python之协程
  • 以STM32CubeMX创建DSP库工程方法二
  • 与姜妍同款冰箱,容声516WILL养鲜冰箱领“鲜”上市
  • Tomcat 乱码问题彻底解决
  • Go语言笔记
  • 计算机网络:物理层 --- 基本概念、编码与调制
  • mac新手入门(快捷键)
  • 电脑是否有问题?检测软件别再乱下载了,只要用这一款工具箱,就可以满足你的需求!
  • FLUX.1+ComfyUI部署与使用:图像合成技术的新高度
  • 大数据实验一: Linux系统安装和使用
  • JDK1.8的新特性
  • 计算机图形学 中心画圆算法 原理及matlab代码实现
  • 智慧医院人工智能应用场景 | 智能导诊系统源码