Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzongming committed Feb 4, 2025
1 parent f417be4 commit 558b361
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 78 deletions.
2 changes: 1 addition & 1 deletion client/esp-ai/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=esp-ai
version=2.42.32
version=2.46.32
author=小明IO
maintainer[email protected]
sentence=ESP32 connects to the AI scheme library
Expand Down
4 changes: 3 additions & 1 deletion client/esp-ai/src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@
#include "globals.h"
#include <vector>

String ESP_AI_VERSION = "2.42.32";
String ESP_AI_VERSION = "2.46.32";

String esp_ai_start_ed = "0";
bool esp_ai_ws_connected = false;
String esp_ai_session_id = "";
String esp_ai_prev_session_id = "";
String esp_ai_tts_task_id = "";
String esp_ai_status = "";
bool esp_ai_is_listen_model = true;
bool esp_ai_user_has_spoken = false;
bool esp_ai_sleep = false;
bool esp_ai_is_first_send = true;
bool esp_ai_played_connected = false;

// 开始搜集音频
bool esp_ai_start_get_audio = false;
Expand Down
2 changes: 2 additions & 0 deletions client/esp-ai/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,14 @@ extern String ESP_AI_VERSION;
extern String esp_ai_start_ed;
extern bool esp_ai_ws_connected;
extern String esp_ai_session_id;
extern String esp_ai_prev_session_id;
extern String esp_ai_tts_task_id;
extern String esp_ai_status;
extern bool esp_ai_sleep;
extern bool esp_ai_is_first_send;
// 聆听模式
extern bool esp_ai_is_listen_model;
extern bool esp_ai_played_connected;

// 用户已经发话
extern bool esp_ai_user_has_spoken;
Expand Down
2 changes: 1 addition & 1 deletion client/esp-ai/src/sdk/begin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void ESP_AI::begin(ESP_AI_CONFIG config)
esp_ai_dec.write(lian_jie_cheng_gong, lian_jie_cheng_gong_len);
esp_ai_dec.write(fu_wu_lian_jie_zhong, fu_wu_lian_jie_zhong_len);


esp_ai_played_connected = false;
// 内置状态处理
status_change("2");
// 设备状态回调
Expand Down
13 changes: 1 addition & 12 deletions client/esp-ai/src/sdk/onNetStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,4 @@
void ESP_AI::onNetStatus(void (*func)(String status))
{
onNetStatusCb = func;
}


// void ESP_AI::onNetStatus(std::function<void(String)> func) {
// onNetStatusCb = [func](String status) {
// func(status); // Call the original function
// // Add additional logic here
// // For example, logging or updating other states
// Serial.println("---收到状态---------");
// Serial.println(status);
// };
// }
}
3 changes: 1 addition & 2 deletions client/esp-ai/src/wakeUp/edge-impulse/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ void ESP_AI::capture_samples()
/* read data at once from i2s */
i2s_read(MIC_i2s_num, (void *)mic_sample_buffer, i2s_bytes_to_read, &bytes_read, 100);
_is_silence = is_silence(mic_sample_buffer, bytes_read);



if (esp_ai_start_get_audio)
{
int vad = esp_ai_user_has_spoken ? wake_up_config.vad_course : wake_up_config.vad_first;
Expand Down
39 changes: 26 additions & 13 deletions client/esp-ai/src/webSocketEvent/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ void ESP_AI::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
data_1["type"] = "play_audio_ws_conntceed";
String sendData = JSON.stringify(data_1);
esp_ai_webSocket.sendTXT(sendData);

// 内置状态处理
status_change("3");
// 设备状态回调
Expand Down Expand Up @@ -202,25 +201,25 @@ void ESP_AI::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
Serial.println("[Error] -> 服务错误:" + at_pos + " " + code + " " + message);

if (code == "4002")
{
{
esp_ai_dec.end();
delay(100);
esp_ai_dec.begin();
esp_ai_dec.write(yu_e_bu_zuo, yu_e_bu_zuo_len);
}
else if (code == "4001")
{
{
esp_ai_dec.end();
delay(100);
esp_ai_dec.begin();
esp_ai_dec.write(e_du_ka_bu_cun_zai, e_du_ka_bu_cun_zai_len);
}
else if (code == "4000")
{
{
esp_ai_dec.end();
delay(100);
esp_ai_dec.begin();
esp_ai_dec.write(chao_ti_wei_qi_yong, chao_ti_wei_qi_yong_len);
esp_ai_dec.write(chao_ti_wei_qi_yong, chao_ti_wei_qi_yong_len);
}

if (onErrorCb != nullptr)
Expand Down Expand Up @@ -258,7 +257,7 @@ void ESP_AI::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
}
else if (type == "set_wifi_config")
{

JSONVar JSON_data = parseRes["configs"];
bool is_ok = setWifiConfig(JSON_data);

Expand Down Expand Up @@ -299,9 +298,9 @@ void ESP_AI::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
String fn_name = (const char *)parseRes["fn_name"];
String str_val = (const char *)parseRes["str_val"];
int num_val = (int)parseRes["num_val"];

if (fn_name == "pinMode")
{
{
str_val == "OUTPUT" && (pinMode(pin, OUTPUT), true);
str_val == "INPUT" && (pinMode(pin, INPUT), true);
str_val == "INPUT_PULLUP" && (pinMode(pin, INPUT_PULLUP), true);
Expand Down Expand Up @@ -339,10 +338,11 @@ void ESP_AI::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
memcpy(session_id_string, payload, 4);
session_id_string[4] = '\0';
String sid = String(session_id_string);

/**
* sid
* 0000 -> 嘟提示音数据
* 0001 -> 服务连接成功提示语
* 1000 -> 提示音缓存数据
* 1001 -> 唤醒问候语缓存数据
* 1002 -> 休息时回复缓存数据
Expand Down Expand Up @@ -374,7 +374,12 @@ void ESP_AI::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
}
}
else if (sid == "2001")
{
{
if (esp_ai_played_connected == false && esp_ai_prev_session_id == "0001")
{
esp_ai_played_connected = true;
}

esp_ai_tts_task_id = "";
// 内置状态处理
status_change("tts_real_end");
Expand All @@ -387,18 +392,24 @@ void ESP_AI::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
}
DEBUG_PRINTLN(debug, ("\n[TTS] -> TTS 数据全部接收完毕,无需继续对话。"));
}
esp_ai_prev_session_id = sid;
return;
}
else if (sid == "2002")
{
DEBUG_PRINTLN(debug, ("\n[TTS] -> TTS CHUNK 接收完毕。"));
esp_ai_tts_task_id = "";
}

if (sid == "0001" && esp_ai_played_connected == true)
{
esp_ai_prev_session_id = sid;
return;
}

// 提取音频数据
uint8_t *audioData = payload + 4;
size_t audioLength = length - 4;

if (sid == "1000")
{
esp_ai_cache_audio_du.insert(esp_ai_cache_audio_du.end(), audioData, audioData + audioLength);
Expand All @@ -412,11 +423,13 @@ void ESP_AI::webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
esp_ai_cache_audio_sleep_reply.insert(esp_ai_cache_audio_sleep_reply.end(), audioData, audioData + audioLength);
}

if (session_id_string && sid != "0000" && sid != esp_ai_session_id)
if (session_id_string && sid != "0000" && sid != "0001" && sid != esp_ai_session_id)
{
esp_ai_prev_session_id = sid;
return;
}
esp_ai_dec.write(audioData, audioLength);
esp_ai_prev_session_id = sid;
break;
}
// case WStype_PING:
Expand Down
Binary file removed client/libraries/esp-ai-2.42.32.zip
Binary file not shown.
Binary file removed client/libraries/esp-ai-2.45.32.zip
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esp-ai",
"version": "2.42.31",
"version": "2.46.32",
"description": "Provide a complete set of AI dialogue solutions for your development board, including but not limited to the IAT+LLM+TTS integration solution for the ESP32 series development board. | 为你的开发板提供全套的AI对话方案,包括但不限于 `ESP32` 系列开发板的 `IAT+LLM+TTS` 集成方案。",
"repository": "https://github.com/wangzongming/esp-ai",
"main": "src/index.js",
Expand Down
10 changes: 5 additions & 5 deletions src/functions/client_messages/play_audio_ws_conntceed.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const du_cache = require("../../audio_temp/du_cache");
async function fn({ device_id }) {
try {
const { devLog, gen_client_config } = G_config;
const { ws, client_params, client_version, error_catch, tts_buffer_chunk_queue } = G_devices.get(device_id);
const { ws, client_params, client_version, error_catch } = G_devices.get(device_id);
const user_config = await gen_client_config({
client_params,
ws,
Expand Down Expand Up @@ -105,18 +105,18 @@ async function fn({ device_id }) {
})

// await TTS_FN(device_id, {
// text: "哦,听起来不太好受。记得多穿衣服保暖",
// text: "哦,听起来不太好受。记得多穿衣服保暖",
// text_is_over: true,
// tts_task_id: "connected_reply"
// })

// return;

// 缓存提示音
du_cache(ws);
// 缓存问候语
const f_reply = _user_config.f_reply;
if (f_reply !== false) {
TTS_FN(device_id, {
await TTS_FN(device_id, {
text: f_reply,
text_is_over: true,
session_id: G_session_ids.cache_hello,
Expand All @@ -125,7 +125,7 @@ async function fn({ device_id }) {
}
const sleep_reply = _user_config.sleep_reply;
if (sleep_reply !== false) {
TTS_FN(device_id, {
await TTS_FN(device_id, {
text: sleep_reply,
text_is_over: true,
session_id: G_session_ids.cache_sleep_reply,
Expand Down
12 changes: 6 additions & 6 deletions src/functions/iat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ const log = require("../../utils/log");
*/
async function cb({ device_id, text }) {
try {
const { onIATcb } = G_config;
const { onIATcb, onSleep } = G_config;
const TTS_FN = require(`../tts`);
const LLM_FN = require(`../llm`);
if (!G_devices.get(device_id)) return;
const { first_session, ws: ws_client, user_config: { sleep_reply } } = G_devices.get(device_id);
const { ws: ws_client, client_params } = G_devices.get(device_id);

ws_client && ws_client.send(JSON.stringify({
type: "log",
Expand All @@ -56,6 +56,7 @@ async function cb({ device_id, text }) {
LLM_FN(device_id, { text })
}
} else {
onSleep && onSleep({ instance: G_Instance, device_id, client_params });
G_devices.set(device_id, {
...G_devices.get(device_id),
first_session: true,
Expand All @@ -71,7 +72,7 @@ async function cb({ device_id, text }) {
module.exports = async (device_id, connected_cb) => {
try {
const TTS_FN = require(`../tts`);
const { devLog, plugins = [], onIAT } = G_config;
const { devLog, plugins = [], onIAT, onSleep } = G_config;
const { ws: ws_client, session_id, error_catch, user_config: { iat_server, llm_server, tts_server, iat_config, sleep_reply } } = G_devices.get(device_id)

devLog && log.info('');
Expand All @@ -97,8 +98,6 @@ module.exports = async (device_id, connected_cb) => {
*/
const connectServerCb = (connected) => {
if (!G_devices.get(device_id)) return;
// const { session_id: now_session_id } = G_devices.get(device_id)
// console.log('是否是同一个会话:', now_session_id === session_id, connected)
if (connected) {
if (!G_devices.get(device_id)) return;
G_devices.set(device_id, {
Expand Down Expand Up @@ -174,13 +173,14 @@ module.exports = async (device_id, connected_cb) => {
* 当 IAT 服务连接成功了,但是长时间不响应时
*/
const serverTimeOutCb = () => {
const { iat_ws, ws: ws_client, iat_server_connected, iat_end_frame_timer, session_id: now_session_id } = G_devices.get(device_id)
const { iat_ws, ws: ws_client, client_params, iat_server_connected, iat_end_frame_timer, session_id: now_session_id } = G_devices.get(device_id)

clearTimeout(iat_end_frame_timer);
if (!iat_server_connected) return;
iat_ws && iat_ws.close();
connectServerCb(false);

onSleep && onSleep({ instance: G_Instance, device_id, client_params });
devLog && log.iat_info('-> IAT服务响应超时,会话结束');
G_devices.set(device_id, {
...G_devices.get(device_id),
Expand Down
Loading

0 comments on commit 558b361

Please sign in to comment.