# esp32s3-ai-chat **Repository Path**: chging/esp32s3-ai-chat ## Basic Information - **Project Name**: esp32s3-ai-chat - **Description**: esp32s3连接语言大模型实现实时语音对话功能 - **Primary Language**: C - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 288 - **Forks**: 126 - **Created**: 2024-08-19 - **Last Updated**: 2025-06-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: Arduino, ai语音助手, 百度大模型 ## README # 简介 此项目主要使用ESP32-S3实现一个自定义角色的AI语音聊天助手(比如医生角色),可以通过该项目熟悉ESP32-S3 arduino的开发,百度语音识别,百度语音合成API调用,百度APPBuilder API的调用实现自定义角色的方法,自定义唤醒词的训练,SD卡的读写,触摸屏的使用,Wifi的配置(smartconfig方式)等基本开发方法。本项目的所有软硬件工程开源,并配备了详细的教程文档,和对应的视频教程,对零基础的同学非常适用,希望能够帮助到大家。 ## 具备的功能 + 支持小程序实现Wifi配网 + 语音唤醒词唤醒ESP32-S3 + 自定义唤醒词模型训练 + 百度语音识别语音合成api访问 + 自定义角色agent + 独立电源供电 + 按键开关机 + 1.28TFT触摸屏唤醒 ## 开源地址 1. 项目开源网址: [https://gitee.com/chging/esp32s3-ai-chat](https://gitee.com/chging/esp32s3-ai-chat) 2. 视频教程网址: 【厚国兄的个人空间-哔哩哔哩】 [https://b23.tv/AsFNSeJ](https://b23.tv/AsFNSeJ) 3. 本教程的word文档版本: 我用夸克网盘分享了「ESP32-S3-AI-Chat-V2.docx」,点击链接即可保存。打开「夸克APP」在线查看,支持多种文档格式转换。 链接:https://pan.quark.cn/s/04ad7594edfb # AI集成套件购买链接 ![](./README.assets/1745290340530-d0077bf6-a232-4bf1-86f6-2d5e35e5ea4b.png) **购买链接:**[**https://h5.m.taobao.com/awp/core/detail.htm?ft=t&id=833542085705**](https://h5.m.taobao.com/awp/core/detail.htm?ft=t&id=833542085705) # 软件环境 ## Arduino 软件安装 ### 软件下载 + 下载网站:[https://www.arduino.cc/en/software](https://www.arduino.cc/en/software) + 下载操作步骤如下: ![](./README.assets/1725461257647-ac656273-6519-4c31-a0ab-f666fd0560f2.png) ![](./README.assets/1721479113465-708656ff-7687-44df-af57-704808b041dc.png) ![](./README.assets/1721479138427-2612eaab-2796-4605-8333-b0f751ae1566.png) ### 软件安装 双击下载的安装软件包,按照如下图所示步骤进行安装: ![](./README.assets/1721479296017-c798acb7-c09a-4401-b473-54bffae78b8d.png) ![](./README.assets/1721479309238-d6c34465-add5-49ce-af5b-d155bfa4d5cb.png) ![](./README.assets/1721479327002-865f0023-c086-42b4-ab63-6a481445438d.png) ![](./README.assets/1721479269079-12ab64ba-4f49-4407-bd20-4ab4a87a7427.png) 等待一会,安装完成。 ### arduino字体转换为中文 ![](./README.assets/1721577144448-57986afd-9acc-4c27-a4da-38ed13066c6a.png) ![](./README.assets/1721577231967-4eaf75da-bf37-4f40-ae0e-a087d8c2cd7c.png) ## ESP32芯片包安装 本项目需要安装esp32芯片包,可以先通过在线安装方式进行,如果失败,也可以选择用离线方式进行。 ### 在线安装 1. 打开Arduino IDE,选择 **文件->首选项->设置** ![](./README.assets/1725461464722-67181550-3b7d-4d3f-9672-8b7a218f7bb5.png) 2. 将以下这个链接粘贴到开发板管理器地址中: ```cpp https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json ``` 然后点击 **确定**,**保存**。 ![](./README.assets/1725461559496-bf7ac621-be49-402e-9007-412e4a851bfd.png) 3. 打开 **开发板管理器,**并搜索输入**esp32**,找到esp32 by Espressif Systems。选择版本(这里选择**2.0.13**,该版本测试没有问题,高版本可能会出现问题),点击安装进行安装,等待下载和安装成功。(如果失败,可以再次点击安装试一试) ![](./README.assets/1725893086214-33b1879d-b60f-4a60-955c-a2cdf3f78d3e.png) ### 离线安装 如果一直下载失败,安装失败,则可以通过离线方式进行安装。 1. 直接下载安装包: 我用夸克网盘分享了「esp32.rar」,点击链接即可保存。打开「夸克APP」,无需下载在线播放视频,畅享原画5倍速,支持电视投屏。 链接:https://pan.quark.cn/s/2093977f64f4 2. 选择解压路径。要放在对应用户的arduino器件包目录。以下为Arduino 版本的安装路径: C:\Users\用户名\AppData\Local\Arduino15\packages ![](./README.assets/1725545925465-5b0a84aa-dc51-43ea-a2d7-2f758eabcfc7.png) ![](./README.assets/1725544502676-86cd80ad-5e1f-47b3-90fd-8551f1f84e2d.png) 注意:AppData是个隐藏文件夹,需要配置文件夹查看选项,能够查看隐藏的文件夹。我这里的用户名Administrator。 3. 解压到对应文件夹完成后,关闭软件,重新打开arduino,点击开发板管理器,看到esp32-arduino已经安装完成。 ![](./README.assets/1725893683090-5204b1c7-97ab-4e68-818d-e65a8bd07050-1745300033663-33.png) 4. 安装完成。 ## 软件库安装 本项目需要安装下面的在线库和离线库。 ### 在线库安装 arduino可以直接在库管理器中进行搜索所需的库的名字进行安装。 #### 需要安装的在线库 | 库名称 | 版本 | | :---: | :---: | | ArduinoJson | 7.1.0 | | base64 | 1.3.0 | | UrlEncode | 1.0.1 | | lvgl | 8.3.10 | | TFT_eSPI | 2.5.43 | | bb_captouch | 1.2.2 | #### 安装步骤 1. 点击 **库管理->库名字搜索->选择对应版本**点击安装。 ![](./README.assets/1725691974364-9df4b5cd-e879-4905-a01d-fd523bb60fb3.png) 2. 安装完成,如下图,显示已安装。如果想要删除,则点击移除即可。 ![](./README.assets/1725692014672-cf50a7fa-29f1-45f8-be2d-f9c9cc469c2b.png) 3. 按照上面相同的方法安装base64 ![](./README.assets/1743213830192-5bc24f82-0159-4a1a-9d79-31b0b7bf85aa.png) 4. 按照上面相同的方法安装UrlEncode ![](./README.assets/1743213840377-deff871b-33b1-4e46-b198-ae2e7ac52b6b.png) ##### 安装tft_eSPI库 ###### 驱动库安装 ![](./README.assets/1731944004411-9acdc4c5-24db-4163-a398-a56f1709f33e.png) ###### 显示功能代码修改 修改 User_Setup_Select.h。在arduino的库安装文件夹中。 ![](./README.assets/1731944130422-af3355b9-e4ab-42fa-901c-a2d598b5b84e.png) 1. 把开头的头文件注释掉。 ![](./README.assets/1728558199800-c0bc098e-5e36-41d3-a999-f9b5e5f2778c.png) 2. 启用自己屏幕型号的头文件。 ![](./README.assets/1728357946814-cf60310d-ebd1-43a7-ba8b-8f39eda5dc8b.png) 3. 适配引脚配置,打开Setup302_Waveshare_ESP32S3_GC9A01.h文件进行修改。当前的项目默认这个引脚配置就行。 ![](./README.assets/1728357971402-01556a0f-12ba-477f-ba09-17f36a64b9f8.png) ##### 安装lvgl库 ###### 驱动库安装 ![](./README.assets/1731943940182-37f32d84-fc59-48e5-bb14-a6b2558dd921.png) ###### 库代码修改 1. 把LVGL文件夹下lv_conf_template.h复制一份,改名为 lv_conf.h。 ![](./README.assets/1731944410805-45801063-7728-4380-931f-b26fb7282920.png) 2. 将lv_conf.h放置到与lvgl文件夹平级目录。 ![](./README.assets/1731944461699-3aeac029-99e1-4916-91c6-bc2130c1404f.png) 3. 打开lv_conf.h文件 ,开头的#if 0改为#if 1。 ![](./README.assets/1727661982878-d8f4d22c-1a91-4a85-8040-dcd26488f062.png) 4. 第88行的#define LV_TICK_CUSTOM 0改为#define LV_TICK_CUSTOM 1(注意!!!这很重要,这是启用自己的时钟,如果没有设置,则会导致动画不能切换,帧率显示为1) 。 ![](./README.assets/1727662045666-34568ec0-3387-402b-9a8c-17b8748323c7.png) 5. 使能测试案例。 ![](./README.assets/1728553989219-136bcc92-7bca-49d4-ad55-e0ce4d374be9.png) 6. LV_FONT_MONTSERRAT_14、LV_FONT_MONTSERRAT_16都置为1。 ![](README.assets/1745766343103-cf8a3b0a-0185-4de7-b12c-1fb007ad0b88.png) 7. LV_FONT_FMT_TXT_LARGE置为1。 ![](README.assets/1745766395224-0dd1f4b7-1ad8-46e2-b8cb-4459eb938eae.png) 8. 更改文件路径。 将\lvgl\demos文件夹移动至\lvgl\src\demos\中去。 ![](README.assets/1731944556913-5faddd2b-0dfe-4c40-b4e2-af6f59bbbcce-17457683447214.png) 9. lv_font_SiYuanHeiTi_Normal_16.c文件导入到lvgl\src\font\文件夹下。 我用夸克网盘分享了「lv_font_SiYuanHeiTi_Normal_16.c」,点击链接即可保存。打开「夸克APP」在线查看,支持多种文档格式转换。 链接:[https://pan.quark.cn/s/3ffcf83d30aa](https://pan.quark.cn/s/3ffcf83d30aa) ![](README.assets/1745766625976-42988601-fd6e-467d-8649-010e9c6e5ae7.png) 10. ui文件夹导入。 我用夸克网盘分享了「test_ui.zip」,点击链接即可保存。 链接:[https://pan.quark.cn/s/fd4657269252](https://pan.quark.cn/s/fd4657269252) 将压缩包下载下来解压缩,然后复制到lvgl\src下。 ![](README.assets/1745250355855-b773718a-b35f-4c38-baab-ee1ce3331c66-17457683342331.png) ##### 安装bb_captouch库 ![](./README.assets/1731943974027-bcd0fb7c-6020-42ad-baff-8f4926bc5db1.png) ### 离线库安装 arduino可以直接导入离线的库文件进行安装,本项目需要安装的离线库主要为训练的唤醒词库。下面是我自己训练的,如果大家没有训练的话可以先用我的这个唤醒词库进行导入,如果想要训练自己的唤醒词,详细的训练见后面第6章节。 #### 需要安装的离线库 | 库名称 | | --- | | wakeup_detect_houguoxiong_inferencing | #### 安装步骤 1. 点击 **项目->导入库->添加.ZIP库**,选择本地的arduino库文件。 ![](./README.assets/1721618564327-4a1217c5-cb1d-4f67-903c-324b87a743b5.png) 2. 选择相应的库文件,点击打开。离线库在esp32s3-ai-chat\library文件夹下。 ![](./README.assets/1725462678461-0f8b5ff6-7c8a-4baa-bf30-1720b905ebfb.png) 3. 查看安装的库文件,贡献库显示已经安装成功。 ![](./README.assets/1725462733563-a436fc98-a6b9-4c80-bdf6-4182a74d18d0.png) # 百度API Key的申请和测试 ## API Key的申请 在调用百度api之前,我们需要在百度的百度智能云平台上面申请api key,申请通过后并且开通对应的api调用服务接口,才可以进行api的访问。 **百度智能云平台网址:**[https://cloud.baidu.com/](https://cloud.baidu.com/) ![](./README.assets/1725694301043-ca38bca7-cf84-47b4-ba06-6b0bf48c577e.png) ### 百度语音识别 我们首先需要创建语音识别的api key。 1. 点击 **产品->语音技术->语音识别->短语音识别标准版。** ![](./README.assets/1725694336755-464781a6-462f-4d15-8e27-210abd639691.png) 2. 点击 **立即使用**,跳出百度账号登录界面,直接用 **手机号登录/注册 **一下。 ![](./README.assets/1725694478885-f5e56f03-1fd9-4e7a-96d8-d931e3361ad6.png) 3. 注册后,需要进行实名认证,按照如下流程就行个人认证。 ![](./README.assets/1730645133525-ec3cb828-5827-46f9-9b54-b1826433a2df.png) ![](https://cdn.nlark.com/yuque/0/2024/png/43232928/1730645155009-9dcc76fb-cb41-4ab1-ac24-2dc6cfdb8c65.png) ![](./README.assets/1730645173095-d72897a6-45aa-4209-9bf0-a7d3981010e0.png) ![](./README.assets/1730645205761-53521046-9d50-4c49-b4a9-503038a558c4.png) ![](./README.assets/1730645273700-db1cab3b-44e7-44f8-b5fb-5a22a1473f90.png) 4. 进入语音技术页面。领取免费资源,点击 **去领取。** ![](./README.assets/1725695232595-538df2f8-0978-4d99-a8c2-2857e6268782.png) 5. 这里将语音识别的 **待领接口** 免费资源领取一下。 ![](./README.assets/1725695256832-c92749ae-babb-4561-88f9-867c6ea95146.png) 6. 点击 **创建应用。** ![](./README.assets/1725695371254-9799875c-eb7a-4019-80fb-d0adef19c879.png) 7. 填写 **应用名称**,**接口选择 **全选,**应用归属 **个人,填写 **应用描述**,点击 **立即创建。** ![](./README.assets/1725695447651-47c35a32-46a2-4065-b5c2-0f07844e23a8.png) 8. 创建完毕,点击 **返回应用列表**。 ![](./README.assets/1725695534220-ebaa52a8-f6ea-4c22-965e-4cfad4c725da.png) 9. 应用列表中,可以查看到刚才创建的应用,并且还可以查看到**API Key、Secret Key**,后面就是需要把这两个key拷贝到程序中去使用访问语音识别api。 ![](./README.assets/1725695629692-ededcaac-6aa6-40ff-92e4-df0c7c9c31ec.png) 10. 接着我们需要开通语音识别的服务。点击左侧 **概览->语音识别->短语音识别->开通**。 ![](./README.assets/1725723575919-b56d71d2-e12f-4643-8ecb-8c07aadd4a07.png) 11. 点击 **按量后付费->语音识别->短语音识别-中文普通话->勾选服务协议->确认开通。** ![](./README.assets/1725724277287-af9296b3-fd67-42f2-9b62-1b5f4fccefa9.png) 12. 至此,语音识别的API key申请成功,并且服务开通成功。 ### 百度语音合成 百度语音合成的api key与语音识别是同一个,所以上一节创建成功后,我们可以直接使用了。但是服务是需要另外开通。 1. 点击左侧 **概览->语音合成->短文本在线合成->基础音库->开通**。 ![](./README.assets/1725724059375-ed1f9ca0-0b3d-46d5-a031-439766168062.png) 2. 点击 **按量后付费->语音合成->短文本在线合成-基础音库->勾选服务协议->确认开通**。 ![](./README.assets/1725724419278-22a4c1f2-fb6a-412c-811d-7c191144539b.png) 3. 至此,语音合成的服务开通成功。注意,一般语音合成没有免费的资源包赠送,因此需要提前充值点费用进去。 ### 百度Agent角色创建 大模型应用Agent的api key申请同样在百度智能云平台上。 1. 百度智能云平台首页,点击选择 **千帆大模型应用开发平台AppBuilder。** ![](./README.assets/1736605777213-e0667524-8440-4037-8eaf-7ebb9cd25548.png) 2. 点击 **立即使用。** ![](./README.assets/1736605936649-7cc480f7-4708-42b5-af15-aa87443aad2a.png) 3. 此时需要登录百度账号,点击登录。然后进入了Appbuilder。 ![](./README.assets/1736606093584-d95a6b50-5c66-422c-b97d-e684b36f8198.png) 4. **创建密钥,点击密钥管理->新增密钥。** ![](./README.assets/1736606147702-00d8f273-0552-43bc-ae8c-c42ca21a260f.png) 5. **创建应用,**点击**个人空间->应用->创建应用->自主规划Agent。** ![](./README.assets/1736606293750-3e63682b-5b1b-449d-b00a-c5f848127bd7.png) 6. 接下来,我们根据下面流程建立Agent的角色设定,最后点击**更新发布**。 ![](./README.assets/1736606614574-48ef39ea-a9d6-43d5-b8de-943be0a1bf59.png) 7. 返回个人空间,可以看到应用发布成功,这里可以获取应用ID。 ![](./README.assets/1736606829965-bb27a175-7ad0-4d88-afcc-9b5c831637d4.png) ## API访问在线测试(可选步骤) 在开通上面的api服务后,我们可以在线测试一下服务是否开通成功。 1. 百度智能云平台首页,点击** 控制台**。 ![](./README.assets/1725808159144-027b8a0b-b98f-4049-bf1a-de2c6bdf1394.png) 2. 进入控制台后,点击 **文档->示例代码。** ![](./README.assets/1725808205626-b40a9e57-40fb-44a6-948b-990e988446e7.png) 3. 进入api调用的测试页面及示例代码页面,在这个页面,我们可以进行百度语音识别、语音合成、文心一言大模型的调用测试。 #### 语音合成 在这里我们可以首先去测试语音合成的api调用,**因为这个api的调用我们可以直接填写文本作为输入,而语音识别是需要传入音频数据作为输入**。因此这里我们先测试语音合成,在语音合成生成后的音频则可以保存下来作为接下来语音识别的输入的测试。 1. 点击 **全部产品->语音技术**,进入语音api测试界面。 ![](./README.assets/1725808865968-61eab3ff-21f2-45ef-bc5e-14f884b455fc.png) 2. 点击 **鉴权认证机制->获取AccessToken->立即前往。** ![](./README.assets/1725811362008-7d7e50c5-78a7-486e-9ee1-3fab755678c8.png) 3. 选择 **应用列表**中我们开通的应用服务,点击 **确定**。 ![](./README.assets/1725811439254-d2bbc520-e179-4c68-b478-e7bbbf44a42e.png) 4. 点击 **调试**,在调试结果中,我们可以查询到响应数据中的access_token。这个表面我们申请的api key可以成功响应了。 ![](./README.assets/1725811519458-ac5b32db-9681-40cc-b5d3-2ebcbfea8bf7.png) 5. 接着测试语音合成api服务接口是否开通成功。我们点击 **语音合成->短文本在线合成**,然后填写需要**合成音频的文本,选择音色,调整语速、音调、音量,选择音频格式wav**,点击 **合成**。 ![](./README.assets/1725808981315-7f582d4b-aafa-4d62-b42c-3430f93d331b.png) 6. 生成合成的音频,点击 **播放按钮**,可以查看生成的音频是否正确。(这里可以点击后面的3个点,将这个音频保存下来,作为后面的语音识别输入使用) ![](./README.assets/1725809009997-0e1f6e17-6436-4911-a6e6-24481893152e.png) 7. 点击 **调试结果**,可以查看请求数据及响应数据包。至此,我们可以通过这样的方式去测试我们的语音合成api服务是否开通成功。 ![](./README.assets/1725809047660-8d8c7b6f-3541-4c73-bd47-51067c5d842f.png) 8. 点击 **示例代码**,可以查看各种语言平台的api调用代码实现。通过这个我们就可以在其他的平台上调用百度的api服务。 ![](./README.assets/1725809120233-c230461c-2f58-43f7-b09f-a390c54b1074.png) #### 语音识别 1. 点击 **语音识别->短语音识别标准版**,点击 **上传文件**,上传上一节语音合成的音频文件,其他的参数都为默认都可以。 ![](./README.assets/1725809714764-6dd3db87-185e-460b-aad1-f6161804f7b3.png) 2. 点击 **调试**,运行成功,查看 **调试结果**,可以从响应数据中查看语音识别是否正确。至此,我们可以通过这样的方式去测试我们的语音识别api服务是否开通成功。 ![](./README.assets/1725809770499-f0464f49-5128-4968-8dc1-22fb4b9096a1.png) 3. 同样,点击 **示例代码**,这里有各种编程语言的api调用实现。 ![](./README.assets/1725809938893-612b6c86-b0f0-40ff-bfc1-b6fb4967eb3c.png) ## API访问Apipost测试(可选推荐工具) api访问有一个通用的工具,这个工具用的比较广泛,可以专门测试api访问接口服务是否正常的。 1. 下载链接:[https://www.apipost.cn/](https://www.apipost.cn/) ![](./README.assets/1725811727434-846d1c41-e174-43cb-8c7a-5a2ad15c09b5.png) 2. 下载后安装成功,进入软件如下图。 ![](./README.assets/1725811751904-f67a29ae-cf28-460b-97fd-af9222f5404f.png) 3. 这里以上一节百度api鉴权访问,获取access_token为例进行说明。apipost中的配置参数的格式去参考上一节中的示例代码访问。 点击 **示例代码->Curl->复制** 代码。 ![](./README.assets/1725813058674-d529e89b-bfc3-449c-9885-eda0babc4b76.png) 4. 点击 **API管理,点击“+”,curl导入**。 ![](./README.assets/1725813231493-766e3949-eb19-4f6a-811c-3acd7e9be951.png) 5. 粘贴 **复制的代码**,点击 **立即导入**。 ![](./README.assets/1725813285790-ac6c9b42-1840-427d-af42-101a0872a672.png) 6. 点击 **发送**。 ![](./README.assets/1725813352237-074c92b5-f4c1-4fe3-9f97-a3b5dfd0d977.png) 7. 从 **实时响应**中,查看响应的结果数据。至此,我们通过apipost的方式去测试了api的访问是否成功。 ![](./README.assets/1725813381932-d51d27cb-f2d7-47ba-8348-8c6eccf5b304.png) # 运行AI Agent角色扮演主程序 ## 运行前的软件环境准备 arduino开发esp32的时候,因为使用的都是各种的库,所以很可能为了实现功能而加了一堆库导致代码很大,超过了用来存储代码的空间,所以需要重新设置一下各个分区的大小。 ![](README.assets/1745767009779-698c5e75-f8fd-48ac-8d9a-97d7f56b048b.png) ### 修改board文件 1. 打开下面目录中的board文件 C:\Users\Administrator\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.13 (这里的Administrator为自己电脑上的用户名) 2. 找到对应开发板 ![](README.assets/1745767289578-5e4a185e-3fb6-4be1-8b8e-079ee38e58e6.png) 3. 修改对应的flash分区内容 如下图的位置,根据其它flash分配的配置方法添加下面的配置: ```cpp esp32s3.menu.PartitionScheme.default_16MB=16M with spiffs (6MB APP/3MB SPIFFS) esp32s3.menu.PartitionScheme.default_16MB.build.partitions=default_16MB esp32s3.menu.PartitionScheme.default_16MB.upload.maximum_size=6553600 ``` ![](README.assets/1745767383318-9b7fba68-3623-4df7-87af-a03cdd115190.png) 4. 刷新列表 只有arduino ide版本2.0以上才需要如下操作,其他版本重启arduino ide即可 C:\Users\Administrator\AppData\Roaming\arduino-ide\Local Storage\leveldb 删除上述链接的文件再重启arduino ide就能获取到新设置的分区配置,这里删除的是保存的一些缓存数据。 5. 开发板选择 点击上方的开发板选择栏,选择开发板和端口,开发板选择ESP32S3 Dev Module,端口选择开发板USB typeC连接后的串口显示的对应串口(可通过设备管理器查看)。 ![img](README.assets/wps1.jpg) 6. 选择修改后的增大的flash分区配置 ![](README.assets/1745767635254-222feea9-6382-4c45-bff9-35bfbde86912.png) 至此,flash的分区已经修改完毕,可以支持较大程序的编译了。 ## AI Agent角色扮演主程序的运行 AI Agent角色扮演主程序的工程在**esp32s3-ai-chat\ai-all\esp32s3_ai_chat_all\esp32s3_ai_chat_all.ino**,我们直接打开工程,修改wifi及api key等配置信息后,编译代码,烧写代码到板子中进行测试。 1. 修改wifi名称,将当前wifi的ssid、password赋值到对应的位置,填写api key,根据第4章操作获取api key。 ![](./README.assets/1745285602285-4cc854c2-95c3-4640-9465-4910b16a23dd.png) 2. 开启psram。 ![](./README.assets/1725261301164-902194fc-6987-4320-8365-07021dea6662.png) 3. 开发板端口选择 点击上方的开发板选择栏,选择开发板和端口,开发板选择ESP32S3 Dev Module,端口选择USB typeC连接后的串口显示的对应串口(可通过设备管理器查看)。 ![](./README.assets/1745285858983-3d1807aa-8352-4467-82e2-8a1cd6eadde9.png) 4. 编译下载。 ![](./README.assets/1725985241427-5edb5ec9-a88f-410c-ab32-d59c31b2958c.png) 5. 语音对话测试。 用 "**houguoxiong**" 唤醒ESP32-S3进行对话。或者直接触摸屏幕(一直触摸进行录音,松开就结束录音,系统开始将录音进行语音识别及大模型访问之类的流程),唤醒ESP32-S3进行对话。 ## 整体软件流程 ![画板](./README.assets/1745287691307-fa68bf34-d03b-478d-929d-6bddaccb7b79.jpeg) ## 主要模块的实现和代码分析 ### 唤醒词语音唤醒 该模块主要实现自己训练的唤醒词唤醒的功能。**esp32s3-ai-chat/example/wake_detect**这个工程主要就是实现唤醒词唤醒的功能。基于这个工程,我们可以在此基础上进行AI语音聊天的开发。 整体代码实现: ```cpp #include /* Edge Impulse Arduino examples * Copyright (c) 2022 EdgeImpulse Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ // If your target is limited in memory remove this macro to save 10K RAM #define EIDSP_QUANTIZE_FILTERBANK 0 /* ** NOTE: If you run into TFLite arena allocation issue. ** ** This may be due to may dynamic memory fragmentation. ** Try defining "-DEI_CLASSIFIER_ALLOCATION_STATIC" in boards.local.txt (create ** if it doesn't exist) and copy this file to ** `/arduino/hardware///`. ** ** See ** (https://support.arduino.cc/hc/en-us/articles/360012076960-Where-are-the-installed-cores-located-) ** to find where Arduino installs cores on your machine. ** ** If the problem persists then there's not enough memory for this model and application. */ /* Includes ---------------------------------------------------------------- */ #include #define SAMPLE_RATE 16000U #define LED_BUILT_IN 21 // INMP441 config #define I2S_IN_PORT I2S_NUM_0 #define I2S_IN_BCLK 4 #define I2S_IN_LRC 5 #define I2S_IN_DIN 6 /** Audio buffers, pointers and selectors */ typedef struct { int16_t *buffer; uint8_t buf_ready; uint32_t buf_count; uint32_t n_samples; } inference_t; static inference_t inference; static const uint32_t sample_buffer_size = 2048; static signed short sampleBuffer[sample_buffer_size]; static bool debug_nn = false; // Set this to true to see e.g. features generated from the raw signal static bool record_status = true; /** * @brief Arduino setup function */ void setup() { // put your setup code here, to run once: Serial.begin(115200); // comment out the below line to cancel the wait for USB connection (needed for native USB) while (!Serial) ; Serial.println("Edge Impulse Inferencing Demo"); pinMode(LED_BUILT_IN, OUTPUT); // Set the pin as output digitalWrite(LED_BUILT_IN, HIGH); //Turn off // Initialize I2S for audio input i2s_config_t i2s_config_in = { .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX), .sample_rate = SAMPLE_RATE, .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // 注意:INMP441 输出 32 位数据 .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S), .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, .dma_buf_count = 8, .dma_buf_len = 1024, }; i2s_pin_config_t pin_config_in = { .bck_io_num = I2S_IN_BCLK, .ws_io_num = I2S_IN_LRC, .data_out_num = -1, .data_in_num = I2S_IN_DIN }; i2s_driver_install(I2S_IN_PORT, &i2s_config_in, 0, NULL); i2s_set_pin(I2S_IN_PORT, &pin_config_in); // summary of inferencing settings (from model_metadata.h) ei_printf("Inferencing settings:\n"); ei_printf("\tInterval: "); ei_printf_float((float)EI_CLASSIFIER_INTERVAL_MS); ei_printf(" ms.\n"); ei_printf("\tFrame size: %d\n", EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE); ei_printf("\tSample length: %d ms.\n", EI_CLASSIFIER_RAW_SAMPLE_COUNT / 16); ei_printf("\tNo. of classes: %d\n", sizeof(ei_classifier_inferencing_categories) / sizeof(ei_classifier_inferencing_categories[0])); ei_printf("\nStarting continious inference in 2 seconds...\n"); ei_sleep(2000); if (microphone_inference_start(EI_CLASSIFIER_RAW_SAMPLE_COUNT) == false) { ei_printf("ERR: Could not allocate audio buffer (size %d), this could be due to the window length of your model\r\n", EI_CLASSIFIER_RAW_SAMPLE_COUNT); return; } ei_printf("Recording...\n"); } /** * @brief Arduino main function. Runs the inferencing loop. */ void loop() { bool m = microphone_inference_record(); if (!m) { ei_printf("ERR: Failed to record audio...\n"); return; } signal_t signal; signal.total_length = EI_CLASSIFIER_RAW_SAMPLE_COUNT; signal.get_data = µphone_audio_signal_get_data; ei_impulse_result_t result = { 0 }; EI_IMPULSE_ERROR r = run_classifier(&signal, &result, debug_nn); if (r != EI_IMPULSE_OK) { ei_printf("ERR: Failed to run classifier (%d)\n", r); return; } int pred_index = 0; // Initialize pred_index float pred_value = 0; // Initialize pred_value // print the predictions ei_printf("Predictions "); ei_printf("(DSP: %d ms., Classification: %d ms., Anomaly: %d ms.)", result.timing.dsp, result.timing.classification, result.timing.anomaly); ei_printf(": \n"); for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) { ei_printf(" %s: ", result.classification[ix].label); ei_printf_float(result.classification[ix].value); ei_printf("\n"); if (result.classification[ix].value > pred_value) { pred_index = ix; pred_value = result.classification[ix].value; } } // Display inference result if (pred_index == 3) { digitalWrite(LED_BUILT_IN, LOW); //Turn on } else { digitalWrite(LED_BUILT_IN, HIGH); //Turn off } #if EI_CLASSIFIER_HAS_ANOMALY == 1 ei_printf(" anomaly score: "); ei_printf_float(result.anomaly); ei_printf("\n"); #endif } static void audio_inference_callback(uint32_t n_bytes) { for (int i = 0; i < n_bytes >> 1; i++) { inference.buffer[inference.buf_count++] = sampleBuffer[i]; if (inference.buf_count >= inference.n_samples) { inference.buf_count = 0; inference.buf_ready = 1; } } } static void capture_samples(void *arg) { const int32_t i2s_bytes_to_read = (uint32_t)arg; size_t bytes_read = i2s_bytes_to_read; while (record_status) { /* read data at once from i2s - Modified for XIAO ESP2S3 Sense and I2S.h library */ i2s_read(I2S_IN_PORT, (void*)sampleBuffer, i2s_bytes_to_read, &bytes_read, 100); // esp_i2s::i2s_read(esp_i2s::I2S_NUM_0, (void *)sampleBuffer, i2s_bytes_to_read, &bytes_read, 100); if (bytes_read <= 0) { ei_printf("Error in I2S read : %d", bytes_read); } else { if (bytes_read < i2s_bytes_to_read) { ei_printf("Partial I2S read"); } // scale the data (otherwise the sound is too quiet) for (int x = 0; x < i2s_bytes_to_read / 2; x++) { sampleBuffer[x] = (int16_t)(sampleBuffer[x]) * 8; } if (record_status) { audio_inference_callback(i2s_bytes_to_read); } else { break; } } } vTaskDelete(NULL); } /** * @brief Init inferencing struct and setup/start PDM * * @param[in] n_samples The n samples * * @return { description_of_the_return_value } */ static bool microphone_inference_start(uint32_t n_samples) { inference.buffer = (int16_t *)malloc(n_samples * sizeof(int16_t)); if (inference.buffer == NULL) { return false; } inference.buf_count = 0; inference.n_samples = n_samples; inference.buf_ready = 0; // if (i2s_init(EI_CLASSIFIER_FREQUENCY)) { // ei_printf("Failed to start I2S!"); // } ei_sleep(100); record_status = true; xTaskCreate(capture_samples, "CaptureSamples", 1024 * 32, (void *)sample_buffer_size, 10, NULL); return true; } /** * @brief Wait on new data * * @return True when finished */ static bool microphone_inference_record(void) { bool ret = true; while (inference.buf_ready == 0) { delay(10); } inference.buf_ready = 0; return ret; } /** * Get raw audio signal data */ static int microphone_audio_signal_get_data(size_t offset, size_t length, float *out_ptr) { numpy::int16_to_float(&inference.buffer[offset], out_ptr, length); return 0; } /** * @brief Stop PDM and release buffers */ static void microphone_inference_end(void) { free(sampleBuffer); ei_free(inference.buffer); } #if !defined(EI_CLASSIFIER_SENSOR) || EI_CLASSIFIER_SENSOR != EI_CLASSIFIER_SENSOR_MICROPHONE #error "Invalid model for current sensor." #endif ``` 下面进行各模块代码的介绍: 1. 这个是自己训练好的唤醒词模型库的头文件,需要引用到工程中。 ```cpp #include ``` 2. 初始化麦克风NMP441的i2s的配置。 ```cpp // Initialize I2S for audio input i2s_config_t i2s_config_in = { .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX), .sample_rate = SAMPLE_RATE, .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // 注意:INMP441 输出 32 位数据 .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S), .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, .dma_buf_count = 8, .dma_buf_len = 1024, }; i2s_pin_config_t pin_config_in = { .bck_io_num = I2S_IN_BCLK, .ws_io_num = I2S_IN_LRC, .data_out_num = -1, .data_in_num = I2S_IN_DIN }; i2s_driver_install(I2S_IN_PORT, &i2s_config_in, 0, NULL); i2s_set_pin(I2S_IN_PORT, &pin_config_in); ``` 3. 这个是唤醒词识别接口的初始化。 ```cpp // summary of inferencing settings (from model_metadata.h) ei_printf("Inferencing settings:\n"); ei_printf("\tInterval: "); ei_printf_float((float)EI_CLASSIFIER_INTERVAL_MS); ei_printf(" ms.\n"); ei_printf("\tFrame size: %d\n", EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE); ei_printf("\tSample length: %d ms.\n", EI_CLASSIFIER_RAW_SAMPLE_COUNT / 16); ei_printf("\tNo. of classes: %d\n", sizeof(ei_classifier_inferencing_categories) / sizeof(ei_classifier_inferencing_categories[0])); ei_printf("\nStarting continious inference in 2 seconds...\n"); ei_sleep(2000); if (microphone_inference_start(EI_CLASSIFIER_RAW_SAMPLE_COUNT) == false) { ei_printf("ERR: Could not allocate audio buffer (size %d), this could be due to the window length of your model\r\n", EI_CLASSIFIER_RAW_SAMPLE_COUNT); return; } ``` 4. 这个初始化的函数主要就是创建了一个freeRTOS的task,task主要为实时采集音频数据。 ```cpp static bool microphone_inference_start(uint32_t n_samples) { inference.buffer = (int16_t *)malloc(n_samples * sizeof(int16_t)); if (inference.buffer == NULL) { return false; } inference.buf_count = 0; inference.n_samples = n_samples; inference.buf_ready = 0; ei_sleep(100); record_status = true; xTaskCreate(capture_samples, "CaptureSamples", 1024 * 32, (void *)sample_buffer_size, 10, NULL); return true; } ``` 5. 实时采集音频数据的task,将采集到的数据存储到一个全局的数据变量sampleBuffer中去。 ```cpp static void capture_samples(void *arg) { const int32_t i2s_bytes_to_read = (uint32_t)arg; size_t bytes_read = i2s_bytes_to_read; while (record_status) { /* read data at once from i2s - Modified for XIAO ESP2S3 Sense and I2S.h library */ i2s_read(I2S_IN_PORT, (void*)sampleBuffer, i2s_bytes_to_read, &bytes_read, 100); // esp_i2s::i2s_read(esp_i2s::I2S_NUM_0, (void *)sampleBuffer, i2s_bytes_to_read, &bytes_read, 100); if (bytes_read <= 0) { ei_printf("Error in I2S read : %d", bytes_read); } else { if (bytes_read < i2s_bytes_to_read) { ei_printf("Partial I2S read"); } // scale the data (otherwise the sound is too quiet) for (int x = 0; x < i2s_bytes_to_read / 2; x++) { sampleBuffer[x] = (int16_t)(sampleBuffer[x]) * 8; } if (record_status) { audio_inference_callback(i2s_bytes_to_read); } else { break; } } } vTaskDelete(NULL); } ``` 6. 将缓存到sampleBuffer变量中的数据复制到inference数据结构体中去,这个结构体用于后面的分类函数的输入参数。到此,音频输入的数据准备的代码实现已经完成。 ```cpp static void audio_inference_callback(uint32_t n_bytes) { for (int i = 0; i < n_bytes >> 1; i++) { inference.buffer[inference.buf_count++] = sampleBuffer[i]; if (inference.buf_count >= inference.n_samples) { inference.buf_count = 0; inference.buf_ready = 1; } } } ``` 7. 接下来看具体的分类。 ```cpp void loop() { bool m = microphone_inference_record(); if (!m) { ei_printf("ERR: Failed to record audio...\n"); return; } signal_t signal; signal.total_length = EI_CLASSIFIER_RAW_SAMPLE_COUNT; signal.get_data = µphone_audio_signal_get_data; ei_impulse_result_t result = { 0 }; EI_IMPULSE_ERROR r = run_classifier(&signal, &result, debug_nn); if (r != EI_IMPULSE_OK) { ei_printf("ERR: Failed to run classifier (%d)\n", r); return; } int pred_index = 0; // Initialize pred_index float pred_value = 0; // Initialize pred_value // print the predictions ei_printf("Predictions "); ei_printf("(DSP: %d ms., Classification: %d ms., Anomaly: %d ms.)", result.timing.dsp, result.timing.classification, result.timing.anomaly); ei_printf(": \n"); for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) { ei_printf(" %s: ", result.classification[ix].label); ei_printf_float(result.classification[ix].value); ei_printf("\n"); if (result.classification[ix].value > pred_value) { pred_index = ix; pred_value = result.classification[ix].value; } } // Display inference result if (pred_index == 3) { digitalWrite(LED_BUILT_IN, LOW); //Turn on } else { digitalWrite(LED_BUILT_IN, HIGH); //Turn off } #if EI_CLASSIFIER_HAS_ANOMALY == 1 ei_printf(" anomaly score: "); ei_printf_float(result.anomaly); ei_printf("\n"); #endif } ``` 8. 在loop主循环中,主要是对采集到的音频数据进行分类预测。microphone_audio_signal_get_data获取之前存储的音频数据,然后调用run_classifier(&signal, &result, debug_nn),计算出分类的预测值。在模型训练时候,训练有几个标签的数据,这里result就会返回对应几个标签的预测结果。 9. result.classification[ix].value预测值越接近1.0的标签,则表示当前识别的是相应的标签。当说出我们训练的唤醒词时,对应的唤醒词预测值也会接近1.0,从而实现唤醒。 10. 我们可以进行一个阈值来与result.classification[ix].value进行比较来判断是否唤醒成功,控制这个比较的阈值大小,则可以控制识别的灵敏程度。至此,整个唤醒流程的代码实现结束。 ### 百度API访问的access_token获取 在访问百度的语音识别、语音合成、文心一言大模型时,都需要提供access_token。在ESP32-S3中,我们通过创建http请求,根据access_token的api访问格式构建请求包,通过http发送请求,等待响应的数据,然后从响应的数据中解析出access_token。 整体代码实现如下: ```cpp // Get Baidu API access token String getAccessToken(const char* api_key, const char* secret_key) { String access_token = ""; HTTPClient http; // 创建http请求 http.begin("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + String(api_key) + "&client_secret=" + String(secret_key)); int httpCode = http.POST(""); if (httpCode == HTTP_CODE_OK) { String response = http.getString(); DynamicJsonDocument doc(1024); deserializeJson(doc, response); access_token = doc["access_token"].as(); Serial.printf("[HTTP] GET access_token: %s\n", access_token); } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); return access_token; } ``` 在这里,我们需要先在百度智能云网站上申请api_key、secret_key,参考第5章节的操作方式。然后将api_key、secret_key作为输入参数,根据api访问格式,发送http.POST请求,然后从响应数据中解析出access_token。 ### 百度语音识别API访问 在ESP32-S3通过i2s采集INMP441的音频数据后,需要将采集的音频数据流识别为文本模式,因此需要调用语音识别API实现实时的语音识别,这里我们采用了百度的语音识别API访问。 主要代码实现如下: ```cpp String baiduSTT_Send(String access_token, uint8_t* audioData, int audioDataSize) { String recognizedText = ""; if (access_token == "") { Serial.println("access_token is null"); return recognizedText; } // audio数据包许愿哦进行Base64编码,数据量会增大1/3 int audio_data_len = audioDataSize * sizeof(char) * 1.4; unsigned char* audioDataBase64 = (unsigned char*)ps_malloc(audio_data_len); if (!audioDataBase64) { Serial.println("Failed to allocate memory for audioDataBase64"); return recognizedText; } // json包大小,由于需要将audioData数据进行Base64的编码,数据量会增大1/3 int data_json_len = audioDataSize * sizeof(char) * 1.4; char* data_json = (char*)ps_malloc(data_json_len); if (!data_json) { Serial.println("Failed to allocate memory for data_json"); return recognizedText; } // Base64 encode audio data encode_base64(audioData, audioDataSize, audioDataBase64); memset(data_json, '\0', data_json_len); strcat(data_json, "{"); strcat(data_json, "\"format\":\"pcm\","); strcat(data_json, "\"rate\":16000,"); strcat(data_json, "\"dev_pid\":1537,"); strcat(data_json, "\"channel\":1,"); strcat(data_json, "\"cuid\":\"57722200\","); strcat(data_json, "\"token\":\""); strcat(data_json, access_token.c_str()); strcat(data_json, "\","); sprintf(data_json + strlen(data_json), "\"len\":%d,", audioDataSize); strcat(data_json, "\"speech\":\""); strcat(data_json, (const char*)audioDataBase64); strcat(data_json, "\""); strcat(data_json, "}"); // 创建http请求 HTTPClient http_client; http_client.begin("http://vop.baidu.com/server_api"); http_client.addHeader("Content-Type", "application/json"); int httpCode = http_client.POST(data_json); if (httpCode > 0) { if (httpCode == HTTP_CODE_OK) { // 获取返回结果 String response = http_client.getString(); Serial.println(response); // 从json中解析对应的result DynamicJsonDocument responseDoc(2048); deserializeJson(responseDoc, response); recognizedText = responseDoc["result"].as(); } } else { Serial.printf("[HTTP] POST failed, error: %s\n", http_client.errorToString(httpCode).c_str()); } // 释放内存 if (audioDataBase64) { free(audioDataBase64); } if (data_json) { free(data_json); } http_client.end(); return recognizedText; } ``` 下面对上面代码重点地方进行分析说明: 1. 这里json包的buffer创建需要为输入数据的1.4倍左右,因为需要进行base64的编码作为输入。这里分配的内存比较大,因此需要从psram中分配。 ```cpp // audio数据包许愿哦进行Base64编码,数据量会增大1/3 int audio_data_len = audioDataSize * sizeof(char) * 1.4; unsigned char* audioDataBase64 = (unsigned char*)ps_malloc(audio_data_len); if (!audioDataBase64) { Serial.println("Failed to allocate memory for audioDataBase64"); return recognizedText; } // json包大小,由于需要将audioData数据进行Base64的编码,数据量会增大1/3 int data_json_len = audioDataSize * sizeof(char) * 1.4; char* data_json = (char*)ps_malloc(data_json_len); if (!data_json) { Serial.println("Failed to allocate memory for data_json"); return recognizedText; } ``` 2. 这里根据api调用文档的格式进行打包,需要注意的是len为原始的数据大小,不是base64编码后的数据大小。 ```cpp // Base64 encode audio data encode_base64(audioData, audioDataSize, audioDataBase64); memset(data_json, '\0', data_json_len); strcat(data_json, "{"); strcat(data_json, "\"format\":\"pcm\","); strcat(data_json, "\"rate\":16000,"); strcat(data_json, "\"dev_pid\":1537,"); strcat(data_json, "\"channel\":1,"); strcat(data_json, "\"cuid\":\"57722200\","); strcat(data_json, "\"token\":\""); strcat(data_json, access_token.c_str()); strcat(data_json, "\","); sprintf(data_json + strlen(data_json), "\"len\":%d,", audioDataSize); strcat(data_json, "\"speech\":\""); strcat(data_json, (const char*)audioDataBase64); strcat(data_json, "\""); strcat(data_json, "}"); ``` 3. 这里,响应数据的json文档要足够大,够响应的返回数据的大小。 ```cpp // 从json中解析对应的result DynamicJsonDocument responseDoc(2048); deserializeJson(responseDoc, response); recognizedText = responseDoc["result"].as(); ``` ### 百度Agent 角色定义API访问 语音识别会以文本的格式返回识别的结果,然后我们可以用这个作为百度大模型Agent的api的输入。大模型api的调用代码实现如下: ```cpp // Get Baidu API conversation id String getConversation_id(const char* api_key, const char* app_id) { String conversation_id = ""; // 创建http请求 HTTPClient http; http.begin("https://qianfan.baidubce.com/v2/app/conversation"); http.addHeader("Content-Type", "application/json"); http.addHeader("X-Appbuilder-Authorization", "Bearer " + String(api_key)); // 创建一个 JSON 文档 DynamicJsonDocument requestJson(1024); requestJson["app_id"] = app_id; // 将 JSON 数据序列化为字符串 String requestBody; serializeJson(requestJson, requestBody); // 发送http访问请求 int httpCode = http.POST(requestBody); if (httpCode == HTTP_CODE_OK) { String response = http.getString(); DynamicJsonDocument doc(1024); deserializeJson(doc, response); conversation_id = doc["conversation_id"].as(); ei_printf("[HTTP] GET conversation_id: %s\n", conversation_id.c_str()); } else { ei_printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); return conversation_id; } ``` ### 百度语音合成API访问 从百度文心一言api返回的文本数据,我们需要通过扬声器播放出来,因此需要将文本数据转化为音频数据输出。这里我们通过调用百度语音合成api接口,实现文本转音频的功能。主要代码实现如下: ```cpp void baiduTTS_Send(String access_token, String text) { if (access_token == "") { Serial.println("access_token is null"); return; } if (text.length() == 0) { Serial.println("text is null"); return; } const int per = 1; const int spd = 5; const int pit = 5; const int vol = 10; const int aue = 6; // 进行 URL 编码 String encodedText = urlEncode(urlEncode(text)); // URL http请求数据封装 String url = "https://tsn.baidu.com/text2audio"; const char* header[] = { "Content-Type", "Content-Length" }; url += "?tok=" + access_token; url += "&tex=" + encodedText; url += "&per=" + String(per); url += "&spd=" + String(spd); url += "&pit=" + String(pit); url += "&vol=" + String(vol); url += "&aue=" + String(aue); url += "&cuid=esp32s3"; url += "&lan=zh"; url += "&ctp=1"; // http请求创建 HTTPClient http; http.begin(url); http.collectHeaders(header, 2); // http请求 int httpResponseCode = http.GET(); if (httpResponseCode > 0) { if (httpResponseCode == HTTP_CODE_OK) { String contentType = http.header("Content-Type"); Serial.println(contentType); if (contentType.startsWith("audio")) { Serial.println("合成成功"); // 获取返回的音频数据流 Stream* stream = http.getStreamPtr(); uint8_t buffer[512]; size_t bytesRead = 0; // 设置timeout为200ms 避免最后出现杂音 stream->setTimeout(200); while (http.connected() && (bytesRead = stream->readBytes(buffer, sizeof(buffer))) > 0) { // 音频输出 playAudio(buffer, bytesRead); delay(1); } // 清空I2S DMA缓冲区 clearAudio(); } else if (contentType.equals("application/json")) { Serial.println("合成出现错误"); } else { Serial.println("未知的Content-Type"); } } else { Serial.println("Failed to receive audio file"); } } else { Serial.print("Error code: "); Serial.println(httpResponseCode); } http.end(); } // Play audio data using MAX98357A void playAudio(uint8_t* audioData, size_t audioDataSize) { if (audioDataSize > 0) { // 发送 size_t bytes_written = 0; i2s_write(I2S_OUT_PORT, (int16_t*)audioData, audioDataSize, &bytes_written, portMAX_DELAY); } } void clearAudio(void) { // 清空I2S DMA缓冲区 i2s_zero_dma_buffer(I2S_OUT_PORT); Serial.print("clearAudio"); } ``` 下面对上面代码重点地方进行分析说明: 1. 这里是进行两次的url编码,参考的官网api调用文档说明的推荐方式。 ```cpp // 进行 URL 编码 String encodedText = urlEncode(urlEncode(text)); ``` 2. http的请求包封装,根据api调用格式进行参数设置 ```cpp // URL http请求数据封装 String url = "https://tsn.baidu.com/text2audio"; const char* header[] = { "Content-Type", "Content-Length" }; url += "?tok=" + access_token; url += "&tex=" + encodedText; url += "&per=" + String(per); url += "&spd=" + String(spd); url += "&pit=" + String(pit); url += "&vol=" + String(vol); url += "&aue=" + String(aue); url += "&cuid=esp32s3"; url += "&lan=zh"; url += "&ctp=1"; // http请求创建 HTTPClient http; http.begin(url); http.collectHeaders(header, 2); ``` 3. 这里是对http api请求的最大超时时间的设置,系统库默认为1s,但是在喇叭播报的最后会出现颤音现象,因此需要在这里将超时时间减小。 ```cpp // 设置timeout为200ms 避免最后出现杂音 stream->setTimeout(200); ``` 4. 这里是获取http音频流数据,在while中需要加入delay的处理,不然这里会占用系统,其他的task运行不了,比如音频录制、唤醒任务都不能运行,导致在音频输出时唤醒不了,因此这里我们做一个释放cpu的处理。 ```cpp while (http.connected() && (bytesRead = stream->readBytes(buffer, sizeof(buffer))) > 0) { // 音频输出 playAudio(buffer, bytesRead); delay(1); } ``` 5. 这个是清除i2s dma的缓冲区数据,消除杂音的作用。 ```cpp void clearAudio(void) { // 清空I2S DMA缓冲区 i2s_zero_dma_buffer(I2S_OUT_PORT); Serial.print("clearAudio"); } ``` # 训练自己的唤醒词(进阶) ## 音频录制 ### 硬件准备 需要准备以下硬件: + AI集成套件 + microSD卡(不大于32GB) + microSD读卡器 ### microSD卡格式化 1. 将microSD卡装进读卡器中,并连接至电脑,将microSD卡**格式化为FAT32格式**。如下图: ![](./README.assets/1725375587961-5a1740ef-0077-4f66-b2a4-baa0013a459a.png) 2. 格式化完成后,将microSD卡装到AI集成套件的卡槽中去。 ### 开始录制音频数据 我们通过烧写录制音频软件到ESP32-S3中进行录制音频数据,录制的音频数据会保存到microSD卡中,然后我们可以通过电脑去读取出来。 #### 烧写录制音频软件到ESP32-S3 音频录制软件工程在**esp32s3-ai-chat/example/capture_audio_data** 下。打开工程文件,在工程编译前,我们使用到了psram,因此需要**打开psram启动开关**,如下图所示,设置好后编译并且烧录到ESP32-S3中去。 ![](./README.assets/1725261301164-902194fc-6987-4320-8365-07021dea6662-1745300256203-178.png) #### 串口发送标签进行录制音频 1. 程序运行之后,正常运行的串口日志如下图。 ![](./README.assets/1725376824589-674251b5-f8f8-477f-9a9c-885dcb657462.png) 2. 程序运行正常后,我们就可以开始打开串口助手工具,发送相应的控制指令进行音频录制。发送“**hgx**”标签。 ![](./README.assets/1725376992198-7c31f150-a2b8-4879-8775-4ea75e81f83e.png) 3. 发送“**rec**”录制指令,开始录制一次。 ![](./README.assets/1725377031300-0519dfa8-687d-4ed9-bfca-1a2305397fbd.png) 4. 发送**标签(例如:hgx)**后,程序将等待另一个**命令rec**,每次发送命令**rec**时,程序就会开始记录新的样本(持续录制10秒钟后自动结束),文件将保存为hgx.1.wav、hgx.2.wav、hgx.3.wav等。 5. 直到发送一个新标签(例如:noise),在这种情况下,程序开始记录一个新标签样本,当你同样为每个新标签样本发送命令rec时,它将开始录音并被保存为noise.1.wav、noise.2.wav、noise.3.wav等。 6. 最终,我们将得到保存在SD卡上的所有录制的标签样本文件,可以在电脑上通过读卡器读取到SD卡上的所有音频数据。如下图所示: ![](./README.assets/1725377793023-2b3b2011-c7d1-4940-af4a-9f42c7f303f2.png) #### 各标签样本的音频录制方法 至少录制**3个标签**的样本数据,唤醒词标签样本、噪音标签样本、未知标签样本,每个标签样本数据各自**最少录制10组**,数据越多,训练后的模型识别能力越好。 1. 唤醒词标签样本:在录制的10秒钟内,对着麦克风不断重复的说出唤醒词,可以每次说出的语速、音调不同等,样本越丰富,识别的泛化能力越强。 2. 噪音标签样本:直接录制环境声音,可以不对着麦克风讲话。 3. 未知标签样本:在录制的10秒钟内,可以对着麦克风说一些其他的非唤醒词的话。 ## 训练录制的音频数据 ### Edge Impulse上创建工程 登录Edge Impulse,[https://edgeimpulse.com/](https://edgeimpulse.com/),注册账号,并创建工程。 ![](./README.assets/1725378238771-770fe630-0cb1-4a27-8f11-aeb2dacf3ee1.png) ### 上传录制的音频数据 创建项目后,在Data Acquisition部分选择Upload Existing Data工具。选择要上传的文件。 ![](./README.assets/1725264313904-399a59da-d5ee-4dcb-99e7-2fc7cfa1a030.png) 数据上传完成后,系统会自动将它们分为Training、Test集(按照80% 20%的比例)。 ### 分割数据集 系统进行训练的数据集要求所有数据长度都是1秒,但上一节中上传的记录样本长度为10秒,必须将其拆分为1秒的样本才能兼容,因此需要对每个样本数据进行分割的操作。如下图所示: 1. 点击样本名称后的三个点,并选择Split sample。 ![](./README.assets/1725378587970-61f068ea-5be3-4b61-a01c-ce21643c2f74.png) 2. 一旦进入该工具,将数据拆分为1秒的样本数据记录。每个矩形框就是拆分提取的一个子模块,如有必要,可以调整矩形的位置,让其完全覆盖住我们的唤醒词音频区,或者也可以做一些添加或删除片段的操作。 ![](./README.assets/1725378647262-b29f0b48-c437-49fd-8d82-4dcc82ab9655.png) 3. 裁剪后的音频数据如下: ![](./README.assets/1725378700080-fd862eb9-895d-4e36-87b6-c0ac2b7ed0f2.png) 4. 所有样本数据都应重复此操作,直到所有样本数据**(Training、Test)**都为1s长度的音频数据。 ### 创造脉冲信号(预处理/模型定义) 创建脉冲信号,进行数据预处理和模型的选择,如下图所示: ![](./README.assets/1725379246960-81b9d5f2-863e-48f2-97ad-d1fc6d607a0c.png) 操作步骤如下: 1. 点击左侧Create impulse,然后点击Add a processing block添加Audio(MFCC),使用MFCC,它使用梅尔频率倒谱系数从音频信号中提取特征,这对人类声音非常有用。 ![](./README.assets/1725380341093-3cab5bd7-f1b7-44fd-ab41-f64c79054474.png) 2. 然后点击Add a learning block添加Classification模块,它通过使用卷积神经网络进行图像分类从头开始构建我们的模型。 ![](./README.assets/1725380395771-1ea055c4-149e-4e67-af4c-e711a1775bb2.png) 3. 最后点击save impulse,保存配置。 ![](./README.assets/1725380439467-c78952c6-4f9a-4e9d-812e-b353e4b5ae7a.png) ### 预处理(MFCC) 下一步是创建下一阶段要训练的图像。 1. 点击MFCC,我们可以保留默认参数值,直接点击Save parameters。 ![](./README.assets/1725380530318-310228ac-59f9-4955-802c-dbd9b5474b35.png) 2. 点击Generate features,生成3个标签数据的特征。 ![](./README.assets/1725380692504-6cf92efe-a396-474e-9c41-b6f1fa321879.png) ### 模型设计与训练(Classifier) 接着,我们需要对模型的结构进行设计和开始训练,步骤如下: 1. 点击左侧 **Classifier**,整个模型的结构设计已经配置好,然后点击 **save&train**,开始训练模型。 ![](./README.assets/1725380907830-5a5bd3c7-dc35-4b76-ab47-581941300f73.png) 2. 训练完成后,会出现如上图所示的分类结果。 ![](./README.assets/1725381184336-332ba985-1ccb-4ad1-88cc-9cee26f994fd.png) ### 模型测试 在模型训练好后,我们可以用测试集数据测试一下训练好的模型的准确率。 1. 点击左侧的Model testing,然后点击Classify all,开始分类所有的测试集数据。 ![](./README.assets/1725381249767-e76d9bca-0c97-437f-a43b-b108921579cb.png) 2. 等待一会,所有的测试集数据分类结果将会计算出来,如上图。根据这个结果可以基本判断我们训练的唤醒词模型的识别准确率是否满足要求。 ![](./README.assets/1725381469318-c62a28cf-81dd-4f82-8e3e-570d03788d7c.png) ## 部署模型到ESP32-S3 ### 生成模型库文件 模型训练完成后,我们需要生成在arduino esp32平台上运行的库文件。 1. 在生成库文件之前,我们先设置一下我们的硬件平台,点击右侧的 **Target**,选择Target device为**ESP-EYE**,然后点击 **Save**。 ![](./README.assets/1725381865178-5c9b29ce-1950-43c0-9d7f-122898a50967.png) 2. 点击左侧的 **Deployment部署**,依次按照上图的选项配置,点击 **Build**,开始生成库文件。 ![](./README.assets/1725381714732-b6d6fd72-414b-4ecc-8819-39329825c50a.png) 3. 生成完成后,我们将库文件.zip保存到我们的工程目录下。 ![](./README.assets/1725382119121-0544772f-b924-4fc6-95b0-ed9d01fe4fa8.png) ### ESP32-S3唤醒词测试 训练好唤醒词库文件后,我们需要在ESP32-S3上测试一下唤醒词功能。打开工程目录下的 **esp32s3-ai-chat/example/wake_detect**工程,进行唤醒功能测试。 1. 选择项目,导入库,添加.ZIP库文件,选择我们训练好的唤醒词库文件。 ![](./README.assets/1725382485062-1abd4e29-8a4f-425c-beb0-0f2b4260ad3e.png) 2. 导入成功后,我们需要引用该库文件,因此再次点击项目,导入库,点击选择刚才导入的库文件。 ![](./README.assets/1725382640313-35ad96c4-45b2-4a2b-a657-c42cb07b3154.png) 3. 导入后,头文件会被引用进去,这样我们自己训练的唤醒词模型就引用成功了。 ![](./README.assets/1725382732035-fe057853-c6a6-461e-bb68-e1f540684af9.png) 4. 编译工程,烧录程序到ESP32-S3开发板中,程序运行后会实时的进行唤醒词监控,并且给出之前训练的3个标签的分类预测值,标签的预测值越接近1.0,则表示当前识别的可信结果为这个标签。 ![](./README.assets/1725383425750-85ae6b58-7422-4e88-acb9-2cad60bab795.png) 5. 当我说出“houguoxiong”时,“hgx”这个标签的预测值直接快接近1.0了,证明我们的唤醒成功。 ![](./README.assets/1725383488994-40cc2660-89ee-4c57-b82c-83cf1456caca.png) ### 唤醒词模型的替换 当在arduino工程中需要替换新训练的唤醒词模型时,我们可以删除掉之前导入的模型库文件,然后删掉工程中之前引用的头文件,最后重新按照之前**7.3.2章节**导入模型库并引用的方式操作一遍即可。 ![](./README.assets/1725383847477-3474b6a9-e66a-4523-8937-7dcc6e38105c.png) 删除库文件在该目录下直接删除导入的库文件夹,然后关闭arduino软件,重新打开arduino工程就可以。