diff --git a/docs/development/experience-notes.md b/docs/development/experience-notes.md index 91a6a5d..5595903 100644 --- a/docs/development/experience-notes.md +++ b/docs/development/experience-notes.md @@ -38,4 +38,4 @@ See [`../experiences/`](../experiences/), and its [`INDEX.md`](../experiences/INDEX.md) for the archived-entry table, for the stored entries. The index below lists each entry once it is added. -- None recorded yet. +- **Audio Compression Trade-offs on ESP32-C3** (Shinku-Chen) — how a voice-playback codec was chosen on limited flash (IMA-ADPCM vs Opus vs MP3), with measured capacity and decoder cost. See [`../experiences/Shinku-Chen/1787793847_91466b0.md`](../experiences/Shinku-Chen/1787793847_91466b0.md). diff --git a/docs/development/experience-notes.zh_CN.md b/docs/development/experience-notes.zh_CN.md index 31faa18..fd803d5 100644 --- a/docs/development/experience-notes.zh_CN.md +++ b/docs/development/experience-notes.zh_CN.md @@ -29,4 +29,4 @@ fork 上自行创建或变更的 `docs/` 文档。每次采集生成一个 `.md` 见 [`../experiences/`](../experiences/) 目录下已保存的条目,以及它的 [`INDEX.md`](../experiences/INDEX.md) 档案条目表。下面索引在条目新增后列出。 -- 暂无记录。 +- **ESP32-C3 上音频压缩方式的权衡**(Shinku-Chen)— 在有限 Flash 上如何为语音播放应用选编解码(IMA-ADPCM vs Opus vs MP3),含实测容量与解码器成本。见 [`../experiences/Shinku-Chen/1787793847_91466b0.zh_CN.md`](../experiences/Shinku-Chen/1787793847_91466b0.zh_CN.md)。 diff --git a/docs/experiences/INDEX.md b/docs/experiences/INDEX.md index 5d12e31..5c0b83c 100644 --- a/docs/experiences/INDEX.md +++ b/docs/experiences/INDEX.md @@ -15,7 +15,9 @@ For how to add an entry and what belongs here, see the ## Index -No experience entries recorded yet. Each entry is stored under -`docs/experiences//` and linked from the experience notes index. +Each entry is stored under `docs/experiences//` and listed here grouped +by the contributing developer's username. -When an entry is added, list it here grouped by ``. +### Shinku-Chen + +- [Audio Compression Trade-offs on ESP32-C3](Shinku-Chen/1787793847_91466b0.md) — how a voice-playback codec was chosen on limited flash (IMA-ADPCM vs Opus vs MP3), with measured capacity and decoder cost. diff --git a/docs/experiences/INDEX.zh_CN.md b/docs/experiences/INDEX.zh_CN.md index 4ea6340..9a3dd9d 100644 --- a/docs/experiences/INDEX.zh_CN.md +++ b/docs/experiences/INDEX.zh_CN.md @@ -11,6 +11,8 @@ ## 索引 -暂无经验条目记录。每条经验保存在 `docs/experiences//` 下,并从经验索引链接。 +每条经验保存在 `docs/experiences//` 下,并在下面按贡献开发者的 GitHub 用户名分组列出。 -新增条目后,按 `` 在这里列出来。 +### Shinku-Chen + +- [ESP32-C3 上音频压缩方式的权衡](Shinku-Chen/1787793847_91466b0.zh_CN.md) — 在有限 Flash 上如何为语音播放应用选编解码(IMA-ADPCM vs Opus vs MP3),含实测容量与解码器成本。 diff --git a/docs/experiences/Shinku-Chen/1787793847_91466b0.md b/docs/experiences/Shinku-Chen/1787793847_91466b0.md new file mode 100644 index 0000000..ef8ad4a --- /dev/null +++ b/docs/experiences/Shinku-Chen/1787793847_91466b0.md @@ -0,0 +1,116 @@ +

+ 简体中文 · English +

+ +# Audio Compression Trade-offs on ESP32-C3 + +Captured after the **Voice Keychain** release (commit `91466b0`). This entry records +how the audio codec for a voice-playback application was chosen on ESP32-C3 (8 MB +flash, no PSRAM), so the next developer who needs to fit a large voice library into +a bounded flash partition can start from measured numbers instead of estimates. + +## Context and the hard limit + +The application plays a library of Chinese voice clips stored in a dedicated SPIFFS +data partition. The source asset set is large: + +- 38 directories / packs, 1557 clips, ~4598 s of audio. +- Encoded at the original bit rate, the full set is ~35.1 MB. + +The target is a data partition the firmware must also be able to decode. On this +hardware the partition size and the codec together set how much of the library is +actually playable. This is a physics ceiling: no amount of selection recovers the +bulk of the content once a codec is fixed. + +## Methods compared + +| Method | Bytes/second (effective) | Decoder cost on ESP32-C3 (no PSRAM) | +| --- | --- | --- | +| IMA-ADPCM 4-bit | ~7800 B/s (16 kHz mono) | Already present, negligible CPU/RAM | +| MP3 | nominal 4000–8000 B/s | ~30–40 KB flash decoder, moderate CPU | +| Opus | ~1000 B/s (6 kbps), ~1500 B/s (12 kbps) | ~60–80 KB flash + some RAM, moderate CPU | + +Measured capacity of the current set (what actually fits in a 3 MB partition): + +| Method / bit rate | Share of the full set that fits | +| --- | --- | +| IMA-ADPCM 4-bit | ~8.7% (~402 s) | +| Opus 12 kbps | ~46.7% (~2146 s) | +| Opus 6 kbps | ~66.8% (~3072 s) | + +The decisive counterweight is the decoder: an Opus decoder costs ~60–80 KB of flash +and some RAM on a chip with no PSRAM, which must be budgeted against the application +and the LVGL UI. The right choice depends on whether raw capacity or firmware +simplicity matters more. + +## Decision and what was built + +The published app **migrated to Opus 8 kbps** (the current high capacity at low bit +rate) with a `libopus` component decoder, over a raw-packet stream where each packet +is a 2-byte little-endian length plus one Opus frame. The encoder (`encode_opus.py`) +resamples to 16 kHz mono, and the firmware decodes frames in a dedicated task and +writes PCM to the audio output. + +A full tool was not needed in the final build: the previous IMA-ADPCM encoder +(`encode_voice.py`) and its legacy comparison document remain in the fork as the +recorded decision history, and the Opus path is the shipped implementation. + +## Reusable takeaways + +- **Measure, don't estimate.** The effective bytes/second per clip differs from the + nominal bit rate because of per-frame/container overhead. Real encoding of the + actual asset set (not a synthetic sample) is what determines whether the library + fits. +- **Capacity is fixed by the codec, not by selection.** Once the codec and partition + size are set, the playable fraction is a ceiling; trimming the library cannot + recover content that exceeds the partition. +- **Decoder cost is the real trade-off on no-PSRAM.** Raw compression ratio alone is + misleading; the extra flash and RAM for an Opus/MP3 decoder has to be budgeted + against the UI and the application. +- **Keep the partition-mount footnote.** Using SPIFFS (an ESP-IDF built-in + component) avoids pulling an external Managed Component, and `format_if_mount_failed = + true` lets a blank data partition self-format on first boot, simplifying first-run + flashing. + +## Other reusable implementation notes + +These are concrete, useful details from the same release, worth recording for any +developer building a Chinese-voice UI or debugging display/input on this board. + +- **Bump the mic gain when recognition is weak.** If the microphone picks up speech + poorly, raise the input gain at the codec driver rather than adding a software + preamp — `esp_codec_dev_set_in_gain(s_dev, 30.0f)`. +- **Match `BSP_LCD_H` with the UI height.** The screen is an ST7789P3 `240x320` + (`BSP_LCD_W=240`, `BSP_LCD_H=320`). When drawing a full-height canvas, the UI + height (`UI_H`) must equal `BSP_LCD_H`; if a white band appears at the edge, + re-check that the canvas height and the panel height agree. +- **Capture the screen for remote debugging.** Enabling + `CONFIG_LV_USE_SNAPSHOT=y` gives a whole-frame snapshot without extra + dependencies. Alternatively the board can be driven through ADB so an agent can + screenshot and recognize the display. +- **Tune input timing to responsiveness.** The short-press and long-press thresholds + are set in the button component (`config->long_press_time`, `config-> + short_press_time`). A long-press duration around 500 ms and a short-press around + 180 ms feel more responsive than the defaults; the long-press-repeat interval is + set by the component Kconfig. Note the code drives `long_press_time` directly, + bypassing the Kconfig 500 ms lower bound, so tune it in code. +- **Embed a CJK subset font for Chinese UI.** Generate a Noto Sans CJK SC subset + with `lv_font_conv` and wire it via `LV_LVGL_H_INCLUDE_SIMPLE=1` so the font uses + a plain `` include instead of a `lvgl/`-prefixed path. Non-BMP characters + (for example the `🐦`) have no glyph in the subset, so the transcoder replaces + them with readable Chinese before building. +- **Reuse the encoder pipeline.** The Opus encoder (`encode_opus.py`) imports the + IMA-ADPCM encoder (`encode_voice.py`) as `EV` and reuses its display-name + cleaning and C index generation, so the two parallel encode paths share one + index generator instead of drifting. +- **Keep button-count ownership in one place.** The button component reads the + count from the pin table (`BSP_BTN_COUNT` in `bsp_pins.h`) rather than + re-declaring it, avoiding two near-identical `BSP_BTN_COUNT` symbols. The three + physical buttons share one ADC pin and are distinguished by a divider resistor; + after changing dividers, measure the three voltage windows with + `bsp_button_read_mv()` before updating `BSP_BTN_MV_TABLE`. + +## Route + +This is general, upstream-benefiting hardware and design experience, so it is +proposed back to the upstream `FoloToy/ai-passport` as a documentation PR. diff --git a/docs/experiences/Shinku-Chen/1787793847_91466b0.zh_CN.md b/docs/experiences/Shinku-Chen/1787793847_91466b0.zh_CN.md new file mode 100644 index 0000000..f23bfc2 --- /dev/null +++ b/docs/experiences/Shinku-Chen/1787793847_91466b0.zh_CN.md @@ -0,0 +1,63 @@ +

+ 简体中文 · English +

+ +# ESP32-C3 上音频压缩方式的权衡 + +记录于 **Voice Keychain** 发布之后(commit `91466b0`)。本条目记录在 ESP32-C3(8 MB Flash、无 PSRAM)上一个语音播放应用的音频编解码方案是如何选定的,让下一位需要把大量语音库塞进有限 Flash 分区的开发者,从实测数据出发而不是靠估算。 + +## 背景与硬限制 + +该应用播放一批存储于独立 SPIFFS 数据分区的中文语音片段。源素材集合很大: + +- 38 个目录 / 音源包,1557 条片段,约 4598 秒音频。 +- 按原始码率编码后,完整集合约 35.1 MB。 + +目标是一个固件还必须能解码的数据分区。在这套硬件上,分区大小与编解码方式共同决定了语音库实际上有多少可播放。这是物理上限:一旦编解码方式确定,靠裁剪素材无法挽回超过分区容量的内容。 + +## 对比的方案 + +| 方案 | 有效字节/秒 | ESP32-C3(无 PSRAM)上的解码成本 | +| --- | --- | --- | +| IMA-ADPCM 4-bit | ~7800 B/s(16 kHz 单声道) | 已有,CPU/RAM 几乎可忽略 | +| MP3 | 标称 4000–8000 B/s | ~30–40 KB Flash 解码器,CPU 中等 | +| Opus | ~1000 B/s(6 kbps)、~1500 B/s(12 kbps) | ~60–80 KB Flash + 部分 RAM,CPU 中等 | + +当前素材集合的实测容量(3 MB 分区实际能装多少): + +| 方案 / 码率 | 占完整集合的比例 | +| --- | --- | +| IMA-ADPCM 4-bit | ~8.7%(约 402 秒) | +| Opus 12 kbps | ~46.7%(约 2146 秒) | +| Opus 6 kbps | ~66.8%(约 3072 秒) | + +决定性反作用力在解码器:在无 PSRAM 的芯片上,一个 Opus 解码器要占 ~60–80 KB Flash 和部分 RAM,必须与应用和 LVGL UI 一起权衡预算。最终选什么,取决于更看重原始容量还是固件简洁性。 + +## 决策与实际构建 + +发布的成品**迁移到 Opus 8 kbps**(低码率下高容量),使用 `libopus` 组件解码器,走裸包流:每个包 = 2 字节小端长度 + 一个 Opus 帧。编码器(`encode_opus.py`)重采样到 16 kHz 单声道,固件在独立任务里逐帧解码并写出 PCM 到音频输出。 + +最终构建并不需要完整工具链:旧的 IMA-ADPCM 编码器(`encode_voice.py`)与之前的对比文档保留在 fork 作为已记录的决策历史,Opus 路径则是实际交付的实现。 + +## 可复用的结论 + +- **实测,不要估算。** 每条片段的实际字节/秒与标称码率不同,因为每帧/容器有额外开销。决定语音库能否装下的,是拿真实素材集合(而非合成样本)编码得到的实测值。 +- **容量由编解码方式决定,不由素材取舍决定。** 一旦编解码方式与分区大小定了,可播放比例就是上限;裁剪素材无法恢复超过分区的内容。 +- **无 PSRAM 场景下,解码器成本才是真正的取舍。** 只看原始压缩比会误导;Opus/MP3 解码器多占的 Flash 与 RAM 必须与 UI 和应用一起预算。 +- **记住分区挂载这个细节。** 用 SPIFFS(ESP-IDF 内置组件)可避免引入外部 Managed Component,且 `format_if_mount_failed = true` 让空白数据分区在首次启动时自动格式化,简化首次烧录。 + +## 其它可复用的实现要点 + +以下取自同一次发布,对任何做中文语音 UI 或在这块板上调试显示/输入的人都值得记下。 + +- **识别不清时调麦克风增益。** 如果麦克风拾音不清晰,优先在 codec 驱动里提高输入增益而不是加软件前置放大——`esp_codec_dev_set_in_gain(s_dev, 30.0f)`。 +- **让 `BSP_LCD_H` 与 UI 高度对齐。** 屏幕是 ST7789P3 `240x320`(`BSP_LCD_W=240`、`BSP_LCD_H=320`)。画全屏画布时,UI 高度(`UI_H`)必须等于 `BSP_LCD_H`;若边缘出现白边,先核对画布高度与面板高度是否一致。 +- **为远程调试截屏。** 开启 `CONFIG_LV_USE_SNAPSHOT=y` 可拿整帧快照,无需额外依赖。也可用 ADB 驱动板子,让 AI 截屏并识别画面。 +- **把输入时延调到手感。** 短按与长按阈值在按钮组件里(`config->long_press_time`、`config->short_press_time`)。长按约 500 ms、短按约 180 ms 比默认值更跟手;长按后的连续触发间隔由组件 Kconfig 控制。注意代码是直接设 `long_press_time`、绕开 Kconfig 的 500 ms 下限,所以要在代码里调。 +- **中文 UI 内嵌 CJK 子集字库。** 用 `lv_font_conv` 生成 Noto Sans CJK SC 子集,并通过 `LV_LVGL_H_INCLUDE_SIMPLE=1` 接入,让字体用普通 `` include 而非 `lvgl/` 前缀路径。子集里没有非 BMP 字符(如 `🐦`)的字形,转码器在建文件前会把它替换成可读中文。 +- **复用编码器管线。** Opus 编码器(`encode_opus.py`)把 IMA-ADPCM 编码器(`encode_voice.py`)作为 `EV` 导入,复用其显示名清洗与 C 索引生成,让两条平行编码路径共用同一套索引生成,避免逻辑漂移。 +- **按键数量归属单一。** 按钮组件从引脚表(`bsp_pins.h` 里的 `BSP_BTN_COUNT`)取数量,而不是重复声明,避免出现两个近似的 `BSP_BTN_COUNT` 符号。三个物理按键共用一个 ADC 引脚、靠分压电阻区分;更换分压后,先用 `bsp_button_read_mv()` 测三档电压,再改 `BSP_BTN_MV_TABLE`。 + +## 分流 + +这是通用、上游也受益的硬件与设计经验,因此作为文档 PR 提交回上游 `FoloToy/ai-passport`。 diff --git a/plays/INDEX.md b/plays/INDEX.md index d57c56d..be630cf 100644 --- a/plays/INDEX.md +++ b/plays/INDEX.md @@ -14,12 +14,12 @@ For the archive convention (naming, structure, and the cover image), see the ## Index -No play archived yet. Each archive is stored under -`plays///` and linked from this table once it is added. +Each archive is stored under `plays///` and linked from this +table. | Contributor | Application | Summary | Source | | --- | --- | --- | --- | -| _none recorded yet_ | | | | +| Shinku-Chen | [Voice Keychain](Shinku-Chen/voice-keychain/README.md) | A sound-effects keychain that turns the AI Passport into a pocket audio player; press OK to play one of hundreds of Chinese voice clips from dozens of character packs. | [feature/voice-keychain](https://github.com/Shinku-Chen/ai-passport/tree/feature/voice-keychain) | ## Adding a record diff --git a/plays/INDEX.zh_CN.md b/plays/INDEX.zh_CN.md index d60667e..82c26e5 100644 --- a/plays/INDEX.zh_CN.md +++ b/plays/INDEX.zh_CN.md @@ -11,11 +11,11 @@ ## 索引 -暂无已归档的应用。每个档案保存在 `plays///` 下,新增后从本表链接。 +每个档案保存在 `plays///` 下,并从本表链接。 | 贡献者 | 应用 | 功能说明 | 源码 | | --- | --- | --- | --- | -| _暂无记录_ | | | | +| Shinku-Chen | [音效钥匙扣](Shinku-Chen/voice-keychain/README.zh_CN.md) | 把 AI Passport 变成口袋音频播放器的音效钥匙扣;按 OK 播放来自几十个角色包的数百条中文语音片段。 | [feature/voice-keychain](https://github.com/Shinku-Chen/ai-passport/tree/feature/voice-keychain) | ## 新增记录 diff --git a/plays/Shinku-Chen/voice-keychain/README.md b/plays/Shinku-Chen/voice-keychain/README.md new file mode 100644 index 0000000..3981425 --- /dev/null +++ b/plays/Shinku-Chen/voice-keychain/README.md @@ -0,0 +1,58 @@ +

+ 简体中文 · English +

+ +# Voice Keychain + +A sound-effects keychain that turns the AI Passport into a pocket audio player. +Open it and instantly play one of hundreds of Chinese voice clips from dozens of +character packs — jojo, meme cat, Liu Huaqiang, Haji Mi, Nailong, Xiao Ming +Jian Mo, and more. + +## Publish information + +- **Title**: Voice Keychain +- **Description**: a sound-effects keychain that turns the AI Passport into a + pocket audio player; press OK to play one of hundreds of Chinese voice clips + from dozens of character packs. +- **Cover**: `voice-keychain-cover.png` (PNG, 1024×1024). + +## What it does + +- **Character directory**: browse all character packs as a scrollable list. Each + entry is a pack of voice clips (e.g. jojo, MC, meme cat, Liu Huaqiang, + Liu Haizhu, Kaqiu Mixue'er, Luyin, Indian A-san, Ji Yi Kawai, Haji Mi, + Nailong, Bao Bao Duda Leilei, Xiao TuanTuan, Xiao Ming Jian Mo). +- **Clip list**: enter a pack to see its clips by name. +- **One-tap playback**: press OK to play the selected clip; built-in decoding + plays 16 kHz mono Opus audio. +- **Settings** (hold OK): show current battery percentage and voltage, and + adjust the playback volume. + +## Interaction + +Three keys drive the whole app. A top bar shows the title and, on the home +screen, the battery percentage (e.g. `97%`). + +- **UP / DOWN**: move selection. +- **OK**: enter a directory / select a clip / play. +- **OK (hold)**: open settings, or go back. + +Long entries scroll horizontally so the full name is readable; the selected row +is highlighted in blue. + +## Source + +- **Source**: , + entry file `main/voice_app.c` (clip index in `main/voice_index.h`) + +The Opus audio clips live in a `voicefs` SPIFFS data partition +(`esp_vfs_spiffs_register`, mounted at `/voices`, flashed at `0x210000`), read +with the standard POSIX `fopen`/`fread` path; the app flashes the merged firmware +image and that data partition separately. + +## Cover + +`voice-keychain-cover.png` (PNG, 1024×1024) shows the device screen displaying +the character directory, surrounded by the toy keychains and button toys that +the app plays. diff --git a/plays/Shinku-Chen/voice-keychain/README.zh_CN.md b/plays/Shinku-Chen/voice-keychain/README.zh_CN.md new file mode 100644 index 0000000..a001cc3 --- /dev/null +++ b/plays/Shinku-Chen/voice-keychain/README.zh_CN.md @@ -0,0 +1,48 @@ +

+ 简体中文 · English +

+ +# 音效钥匙扣 (Voice Keychain) + +一款音效钥匙扣,把 AI Passport 变成随身音效播放器。开机就能播几十个角色包、 +上百段中文语音——jojo、meme猫、刘华强、哈基米、奶龙、小明剑魔……随手一按就出声音。 + +## 发布信息 + +- **标题**:音效钥匙扣(Voice Keychain) +- **描述**:一款音效钥匙扣,把 AI Passport 变成随身音效播放器;按 OK 即可播放几十个 + 角色包里的上百段中文语音。 +- **封面**:`voice-keychain-cover.png`(PNG,1024×1024)。 + +## 功能 + +- **角色目录**:以可滚动列表浏览全部角色包,每个角色包是一组语音(如 jojo、MC、 + meme猫、刘华强、刘海柱、卡丘米雪儿、卢音、印度阿三、吉伊卡哇、哈基米、奶龙、 + 宝宝肚肚打雷、小团团、小明剑魔)。 +- **片段列表**:进入角色包,按名字浏览其中的语音片段。 +- **一键播放**:按 OK 播放选中片段;内置解码播放 16kHz 单声道 Opus 音频。 +- **设置菜单**(长按 OK):显示当前电量百分比与电压,并调节播放音量。 + +## 交互方式 + +整机由三个按键驱动。顶栏显示标题,首页右侧显示电量百分比(如 `97%`)。 + +- **UP / DOWN**:上下选择。 +- **OK**:进入目录 / 选中片段 / 播放。 +- **OK 长按**:打开设置,或返回。 + +过长条目会横向滚动以完整显示;选中行以蓝色高亮。 + +## 源码 + +- **源码**:, + 入口文件 `main/voice_app.c`(片段索引在 `main/voice_index.h`) + +Opus 音频存放在 `voicefs` SPIFFS 数据分区(`esp_vfs_spiffs_register`,挂载于 +`/voices`,烧录在 `0x210000`),用标准 POSIX `fopen`/`fread` 路径读取;应用 +烧录的是合并固件镜像,数据分区需单独烧录。 + +## 封面 + +`voice-keychain-cover.png`(PNG,1024×1024),展示设备屏幕显示的角色目录, +周围环绕本应用能播放的各种玩具钥匙扣与按键玩具。 diff --git a/plays/Shinku-Chen/voice-keychain/voice-keychain-cover.png b/plays/Shinku-Chen/voice-keychain/voice-keychain-cover.png new file mode 100644 index 0000000..30b670f Binary files /dev/null and b/plays/Shinku-Chen/voice-keychain/voice-keychain-cover.png differ