自定义分区
官方给出的分区表介绍: esp-idf 编程指南
相关操作:
- 在 VSCODE 的 ESP-IDF 环境中点击底层菜单栏的 menuconfig 图标
- 修改 Serial flasher config 的 Flash size 大小(我的为 4MB)
- 将 Partition Table 选择为 Custom partition table,其余默认,save保存
- 查看->命令面板(Ctrl + Shift + P) 搜索 partition table,选择 Open Partition Table Editor UI
- 通过 add new row 添加分区,并参考下图编辑分区表,编写后 save 保存

音频文件写入自定义分区
音频文件编译为二进制文件
将 wav 形式的音频文件放进 audio 文件夹中

在 spiffsgen.py 所在的文件夹下打开命令行,输入以下命令(示例):python spiffsgen.py 0x200000 audio audio.bin
即可生成 audio.bin
- python spiffsgen.py A B C
- A:系统的空间大小,使用十六进制(预设的 audio 分区为 2M,所以为 0x200000,注意不是 audio 分区的偏移地址)
- B:文件夹名称
- C:生成的bin文件名称
二进制文件烧录到自定义分区
使用 ESP32 FLASH DOWNLOAD TOOL 对 audio.bin 文件进行烧录
按照上面步骤对 audio 分区的设定,将 audio.bin 烧录在 @ 0x110000 ,选择相应 COM 口,点击 START 烧录
ps:如果报错 Permission Denied ,将 ESP32 FLASH DOWNLOAD TOOL 文件夹里的 dl-temp 删掉,重新烧录
音频播放代码
几个需要根据自身情况修改的地方:
- BCK、WS、DATA 引脚根据 ESP32 情况去修改
- esp_vfs_spiffs_conf_t 结构体里的 max_files 的值根据音频文件数量去修改
- fopen(“/audio/what_edit.wav”, “rb”) 根据要播放的文件名称去修改
完整代码如下:
#include <stdio.h>
#include <string.h>
#include "driver/i2s.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_spiffs.h"
typedef struct
{
// RIFF Section
char RIFFSectionID[4]; // Letters "RIFF"
uint32_t Size; // Size of entire file less 8
char RiffFormat[4]; // Letters "WAVE"
// Format Section
char FormatSectionID[4]; // letters "fmt"
uint32_t FormatSize; // Size of format section less 8
uint16_t FormatID; // 1=uncompressed PCM
uint16_t NumChannels; // 1=mono,2=stereo
uint32_t SampleRate; // 44100, 16000, 8000 etc.
uint32_t ByteRate; // =SampleRate * Channels * (BitsPerSample/8)
uint16_t BlockAlign; // =Channels * (BitsPerSample/8)
uint16_t BitsPerSample; // 8,16,24 or 32
// Data Section
char DataSectionID[4]; // The letters "data"
uint32_t DataSize; // Size of the data that follows
}WavHeader_Struct;
WavHeader_Struct WavHeader;
bool ValidWavData(WavHeader_Struct* Wav);
void sound_terminate(void)
1万+



被折叠的 条评论
为什么被折叠?



