Skip to content

Commit 4e7baf4

Browse files
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.
1 parent 21311a6 commit 4e7baf4

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
0.5.1
22
=====
3-
- Fri3d Camp 2024 Badge: workaround ADC2+WiFi conflict by temporarily disable WiFi to measure battery level
4-
- Fri3d Camp 2024 Badge: improve battery monitor calibration to fix 0.1V delta
3+
- Fri3d Camp 2024 Board: add startup light and sound
4+
- Fri3d Camp 2024 Board: workaround ADC2+WiFi conflict by temporarily disable WiFi to measure battery level
5+
- Fri3d Camp 2024 Board: improve battery monitor calibration to fix 0.1V delta
56
- API: improve and cleanup animations
67
- API: SharedPreferences: add erase_all() function
78
- API: add defaults handling to SharedPreferences and only save non-defaults

internal_filesystem/lib/mpos/audio/stream_wav.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
import time
88
import sys
99

10-
# Volume scaling function - regular Python version
11-
# Note: Viper optimization removed because @micropython.viper decorator
12-
# causes cross-compiler errors on Unix/macOS builds even inside conditionals
13-
def _scale_audio(buf, num_bytes, scale_fixed):
14-
"""Volume scaling for 16-bit audio samples."""
10+
# Volume scaling function - Viper-optimized for ESP32 performance
11+
# NOTE: The line below is automatically commented out by build_mpos.sh during
12+
# Unix/macOS builds (cross-compiler doesn't support Viper), then uncommented after build.
13+
import micropython
14+
@micropython.viper
15+
def _scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
16+
"""Fast volume scaling for 16-bit audio samples using Viper (ESP32 native code emitter)."""
1517
for i in range(0, num_bytes, 2):
16-
lo = buf[i]
17-
hi = buf[i + 1]
18+
lo = int(buf[i])
19+
hi = int(buf[i + 1])
1820
sample = (hi << 8) | lo
1921
if hi & 128:
2022
sample -= 65536

scripts/build_mpos.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,23 @@ if [ "$target" == "esp32" ]; then
101101
elif [ "$target" == "unix" -o "$target" == "macOS" ]; then
102102
manifest=$(readlink -f "$codebasedir"/manifests/manifest.py)
103103
frozenmanifest="FROZEN_MANIFEST=$manifest"
104+
105+
# Comment out @micropython.viper decorator for Unix/macOS builds
106+
# (cross-compiler doesn't support Viper native code emitter)
107+
echo "Temporarily commenting out @micropython.viper decorator for Unix/macOS build..."
108+
stream_wav_file="$codebasedir"/internal_filesystem/lib/mpos/audio/stream_wav.py
109+
sed -i 's/^@micropython\.viper$/#@micropython.viper/' "$stream_wav_file"
110+
104111
# LV_CFLAGS are passed to USER_C_MODULES
105112
# STRIP= makes it so that debug symbols are kept
106113
pushd "$codebasedir"/lvgl_micropython/
107114
# USER_C_MODULE doesn't seem to work properly so there are symlinks in lvgl_micropython/extmod/
108115
python3 make.py "$target" LV_CFLAGS="-g -O0 -ggdb -ljpeg" STRIP= DISPLAY=sdl_display INDEV=sdl_pointer INDEV=sdl_keyboard "$frozenmanifest"
109116
popd
117+
118+
# Restore @micropython.viper decorator after build
119+
echo "Restoring @micropython.viper decorator..."
120+
sed -i 's/^#@micropython\.viper$/@micropython.viper/' "$stream_wav_file"
110121
else
111122
echo "invalid target $target"
112123
fi

0 commit comments

Comments
 (0)