From 2180780f679889c32fac240b67f9d73b9d21a64d Mon Sep 17 00:00:00 2001 From: Shinku <17696928+Shinku-Chen@users.noreply.github.com> Date: Thu, 27 Aug 2026 11:03:15 +0800 Subject: [PATCH] docs(archive): fix voice-keychain guide codec and partition offset - Align the voice-keychain guide with the shipped Opus pipeline: encode_opus.py, 16 kHz mono, Opus 8 kbps (raw packet stream). - Correct the voicefs partition to 0x210000, 0x5F0000 (matches feature/voice-keychain partitions.csv); the guide previously wrote 0x310000, 0x300000. - Fix the flash command offset to 0x210000. - README already correct (0x210000, 16 kHz mono Opus); unchanged. --- .../Shinku-Chen/voice-keychain/voice-guide.md | 20 ++++++++++--------- .../voice-keychain/voice-guide.zh_CN.md | 20 ++++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/plays/Shinku-Chen/voice-keychain/voice-guide.md b/plays/Shinku-Chen/voice-keychain/voice-guide.md index 9f16480..6192c1d 100644 --- a/plays/Shinku-Chen/voice-keychain/voice-guide.md +++ b/plays/Shinku-Chen/voice-keychain/voice-guide.md @@ -15,29 +15,31 @@ Raw audio lives in `assets/project//*.mp3|ogg|wav`. Each directory be ## Transcoding pipeline -Run the transcoder after adding or changing source audio, with a Python environment that has `numpy` and `miniaudio`: +Run the transcoder after adding or changing source audio. It requires `ffmpeg` with the `libopus` encoder and a Python environment with `numpy` and `miniaudio`: ```bash pip install numpy miniaudio # once -python tools/encode_voice.py +python tools/encode_opus.py ``` The script performs, for each clip: -1. Decode mp3/ogg/wav with `miniaudio` and resample to 8 kHz mono int16. +1. Decode mp3/ogg/wav with `miniaudio` and resample to 16 kHz mono. 2. Apply a low-pass filter (voice bandwidth, about 4 kHz) and trim leading/trailing silence. -3. Encode IMA-ADPCM 4-bit (about 32 kbps; ~87.5% smaller than 16-bit PCM, still intelligible speech). -4. Write `assets/audio/dirNN/clipMM.adpcm`, update `assets/audio/voice_index.json`, regenerate `main/voice_index.h` (compile-time path/name/length table the firmware uses), and pack a `voicefs.img` using the ESP-IDF `spiffsgen.py` tool. +3. Encode Opus at 8 kbps with `ffmpeg` (libopus), writing a raw Opus packet stream (each packet is a 2-byte little-endian length plus one Opus frame). +4. Write `assets/audio/dirNN/clipMM.opus`, update `assets/audio/voice_index.json`, regenerate `main/voice_index.h` (compile-time path/name/length table the firmware uses), and pack a `voicefs.img` using the ESP-IDF `spiffsgen.py` tool. + +The firmware decodes the frames in a dedicated task and writes PCM to the audio output. The older IMA-ADPCM encoder (`encode_voice.py`) remains in the repository as recorded decision history; the shipped path is Opus. ## Storage layout `partitions.csv` adds a dedicated SPIFFS data partition: ```csv -voicefs, data, spiffs, 0x310000, 0x300000, +voicefs, data, spiffs, 0x210000, 0x5F0000, ``` -The firmware mounts it via `esp_vfs_spiffs_register` under `/voices` and reads each clip with ordinary `fopen`/`fread`. SPIFFS is an ESP-IDF built-in component, so no external Managed Component is required. The `voicefs.img` is the content of that partition; it is small relative to the 3 MB partition (the coded clips are a few hundred KB). +The firmware mounts it via `esp_vfs_spiffs_register` under `/voices` and reads each clip with ordinary `fopen`/`fread`. SPIFFS is an ESP-IDF built-in component, so no external Managed Component is required. The `voicefs.img` is the content of that partition; it is small relative to the ~5.94 MB (0x5F0000) partition (the coded clips are a few hundred KB). ## Building and flashing @@ -47,7 +49,7 @@ Build the application firmware as usual. The data partition is flashed separatel # app + bootloader + partition table idf.py -p flash # data partition (voice assets) — flash the voicefs.img to the voicefs offset -python -m esptool --chip esp32c3 -p write_flash 0x310000 assets/audio/voicefs.img +python -m esptool --chip esp32c3 -p write_flash 0x210000 assets/audio/voicefs.img ``` On first boot the partition is mounted with `format_if_mount_failed = true`, so a blank data partition is formatted automatically before reading clips. @@ -57,5 +59,5 @@ On first boot the partition is mounted with `format_if_mount_failed = true`, so When you add, rename, or remove source audio: 1. Put the files in `assets/project//`. -2. Run `python tools/encode_voice.py` to regenerate the clips, `voice_index.h`, and `voicefs.img`. +2. Run `python tools/encode_opus.py` to regenerate the clips, `voice_index.h`, and `voicefs.img`. 3. Rebuild the firmware (the `voice_index.h` table changed) and re-flash the data partition. diff --git a/plays/Shinku-Chen/voice-keychain/voice-guide.zh_CN.md b/plays/Shinku-Chen/voice-keychain/voice-guide.zh_CN.md index 29bab14..8f9cad0 100644 --- a/plays/Shinku-Chen/voice-keychain/voice-guide.zh_CN.md +++ b/plays/Shinku-Chen/voice-keychain/voice-guide.zh_CN.md @@ -15,29 +15,31 @@ ## 转码管线 -新增或改动原始音频后运行转码器,需一个装有 `numpy` 与 `miniaudio` 的 Python 环境: +新增或改动原始音频后运行转码器,需一个带 `libopus` 编码器的 `ffmpeg`、以及装有 `numpy` 与 `miniaudio` 的 Python 环境: ```bash pip install numpy miniaudio # 首次 -python tools/encode_voice.py +python tools/encode_opus.py ``` 脚本对每段执行: -1. 用 `miniaudio` 解码 mp3/ogg/wav 并重采样到 8 kHz 单声道 int16。 +1. 用 `miniaudio` 解码 mp3/ogg/wav 并重采样到 16 kHz 单声道。 2. 低通滤波(语音带宽约 4 kHz)并剪除首尾静音。 -3. 编码 IMA-ADPCM 4bit(约 32 kbps;比 16bit PCM 小约 87.5%,语音仍可辨)。 -4. 写入 `assets/audio/dirNN/clipMM.adpcm`,更新 `assets/audio/voice_index.json`,重新生成 `main/voice_index.h`(固件使用的编译期路径/中文名/长度表),并用 ESP-IDF 的 `spiffsgen.py` 打包 `voicefs.img`。 +3. 用 `ffmpeg`(libopus)编码为 Opus 8 kbps,写成裸 Opus 包流(每个包 = 2 字节小端长度 + 一个 Opus 帧)。 +4. 写入 `assets/audio/dirNN/clipMM.opus`,更新 `assets/audio/voice_index.json`,重新生成 `main/voice_index.h`(固件使用的编译期路径/中文名/长度表),并用 ESP-IDF 的 `spiffsgen.py` 打包 `voicefs.img`。 + +固件在独立任务中逐帧解码并写出 PCM 到音频输出。旧的 IMA-ADPCM 编码器(`encode_voice.py`)保留在仓库作为已记录的决策历史;实际交付路径是 Opus。 ## 存储布局 `partitions.csv` 新增一个独立的 SPIFFS 数据分区: ```csv -voicefs, data, spiffs, 0x310000, 0x300000, +voicefs, data, spiffs, 0x210000, 0x5F0000, ``` -固件通过 `esp_vfs_spiffs_register` 将其挂载到 `/voices`,并用普通的 `fopen`/`fread` 读取每段。SPIFFS 是 ESP-IDF 内置组件,因此无需外部 Managed Component。`voicefs.img` 即该分区的内容;相对 3 MB 分区它很小(编码后的音效仅数百 KB)。 +固件通过 `esp_vfs_spiffs_register` 将其挂载到 `/voices`,并用普通的 `fopen`/`fread` 读取每段。SPIFFS 是 ESP-IDF 内置组件,因此无需外部 Managed Component。`voicefs.img` 即该分区的内容;相对约 5.94 MB(0x5F0000)分区它很小(编码后的音效仅数百 KB)。 ## 构建与烧录 @@ -47,7 +49,7 @@ voicefs, data, spiffs, 0x310000, 0x300000, # 应用 + bootloader + 分区表 idf.py -p <端口> flash # 数据分区(语音素材)— 把 voicefs.img 烧到 voicefs 偏移 -python -m esptool --chip esp32c3 -p <端口> write_flash 0x310000 assets/audio/voicefs.img +python -m esptool --chip esp32c3 -p <端口> write_flash 0x210000 assets/audio/voicefs.img ``` 首次启动时以 `format_if_mount_failed = true` 挂载,因此空白数据分区会在读取前自动格式化。 @@ -57,5 +59,5 @@ python -m esptool --chip esp32c3 -p <端口> write_flash 0x310000 assets/audio/v 当新增、重命名或删除原始音频时: 1. 把文件放入 `assets/project/<目录>/`。 -2. 运行 `python tools/encode_voice.py` 重新生成各段、`voice_index.h` 与 `voicefs.img`。 +2. 运行 `python tools/encode_opus.py` 重新生成各段、`voice_index.h` 与 `voicefs.img`。 3. 重新构建固件(`voice_index.h` 表已改变)并重新烧录数据分区。