From 4e7baf4ec6b18caffbe359ee0e41195c8c870599 Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Wed, 3 Dec 2025 23:11:22 +0100 Subject: [PATCH] AudioFlinger: re-add viper optimizations These make a notable difference when playing audio on ESP32. Without them, each UI action causes a stutter, so it's not fun to listen to audio while doing anything on the device. With them, most UI actions don't cause a stutter. Long maxed out CPU runs and storage access still do, though. --- CHANGELOG.md | 5 +++-- internal_filesystem/lib/mpos/audio/stream_wav.py | 16 +++++++++------- scripts/build_mpos.sh | 11 +++++++++++ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 269851fc..05c98b1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ 0.5.1 ===== -- Fri3d Camp 2024 Badge: workaround ADC2+WiFi conflict by temporarily disable WiFi to measure battery level -- Fri3d Camp 2024 Badge: improve battery monitor calibration to fix 0.1V delta +- Fri3d Camp 2024 Board: add startup light and sound +- Fri3d Camp 2024 Board: workaround ADC2+WiFi conflict by temporarily disable WiFi to measure battery level +- Fri3d Camp 2024 Board: improve battery monitor calibration to fix 0.1V delta - API: improve and cleanup animations - API: SharedPreferences: add erase_all() function - API: add defaults handling to SharedPreferences and only save non-defaults diff --git a/internal_filesystem/lib/mpos/audio/stream_wav.py b/internal_filesystem/lib/mpos/audio/stream_wav.py index 4c527065..884d936f 100644 --- a/internal_filesystem/lib/mpos/audio/stream_wav.py +++ b/internal_filesystem/lib/mpos/audio/stream_wav.py @@ -7,14 +7,16 @@ import os import time import sys -# Volume scaling function - regular Python version -# Note: Viper optimization removed because @micropython.viper decorator -# causes cross-compiler errors on Unix/macOS builds even inside conditionals -def _scale_audio(buf, num_bytes, scale_fixed): - """Volume scaling for 16-bit audio samples.""" +# Volume scaling function - Viper-optimized for ESP32 performance +# NOTE: The line below is automatically commented out by build_mpos.sh during +# Unix/macOS builds (cross-compiler doesn't support Viper), then uncommented after build. +import micropython +@micropython.viper +def _scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int): + """Fast volume scaling for 16-bit audio samples using Viper (ESP32 native code emitter).""" for i in range(0, num_bytes, 2): - lo = buf[i] - hi = buf[i + 1] + lo = int(buf[i]) + hi = int(buf[i + 1]) sample = (hi << 8) | lo if hi & 128: sample -= 65536 diff --git a/scripts/build_mpos.sh b/scripts/build_mpos.sh index 7b77ee46..4ee57487 100755 --- a/scripts/build_mpos.sh +++ b/scripts/build_mpos.sh @@ -101,12 +101,23 @@ if [ "$target" == "esp32" ]; then elif [ "$target" == "unix" -o "$target" == "macOS" ]; then manifest=$(readlink -f "$codebasedir"/manifests/manifest.py) frozenmanifest="FROZEN_MANIFEST=$manifest" + + # Comment out @micropython.viper decorator for Unix/macOS builds + # (cross-compiler doesn't support Viper native code emitter) + echo "Temporarily commenting out @micropython.viper decorator for Unix/macOS build..." + stream_wav_file="$codebasedir"/internal_filesystem/lib/mpos/audio/stream_wav.py + sed -i 's/^@micropython\.viper$/#@micropython.viper/' "$stream_wav_file" + # LV_CFLAGS are passed to USER_C_MODULES # STRIP= makes it so that debug symbols are kept pushd "$codebasedir"/lvgl_micropython/ # USER_C_MODULE doesn't seem to work properly so there are symlinks in lvgl_micropython/extmod/ python3 make.py "$target" LV_CFLAGS="-g -O0 -ggdb -ljpeg" STRIP= DISPLAY=sdl_display INDEV=sdl_pointer INDEV=sdl_keyboard "$frozenmanifest" popd + + # Restore @micropython.viper decorator after build + echo "Restoring @micropython.viper decorator..." + sed -i 's/^#@micropython\.viper$/@micropython.viper/' "$stream_wav_file" else echo "invalid target $target" fi