From 4afcd047829cb47e00042bb3e0e33c25e5b74877 Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Tue, 28 Oct 2025 12:16:02 +0100 Subject: [PATCH] Music Player app: scale volume --- .../assets/audio_player.py | 19 ++++++++++--------- .../assets/music_player.py | 8 ++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/internal_filesystem/apps/com.micropythonos.musicplayer/assets/audio_player.py b/internal_filesystem/apps/com.micropythonos.musicplayer/assets/audio_player.py index 52740730..a11207f3 100644 --- a/internal_filesystem/apps/com.micropythonos.musicplayer/assets/audio_player.py +++ b/internal_filesystem/apps/com.micropythonos.musicplayer/assets/audio_player.py @@ -1,3 +1,4 @@ +import machine import os import time @@ -96,13 +97,12 @@ class AudioPlayer: ibuf=32000 ) except Exception as e: - print("Warning: error initializing I2S audio device, simulating playback...") + print(f"Warning: simulating playback due to error initializing I2S audio device: {e}") print(f"Playing {data_size} bytes (vol {cls._volume}%) …") f.seek(data_start) chunk_size = 4096 # 4 KB → safe on ESP32 - scale = cls._volume / 100.0 # float 0.0-1.0 total = 0 while total < data_size: @@ -112,13 +112,14 @@ class AudioPlayer: break # ---- on-the-fly volume scaling (16-bit little-endian) ---- - if scale < 1.0: - # convert bytes → array of signed ints → scale → back to bytes - import array - samples = array.array('h', raw) # 'h' = signed short - for i in range(len(samples)): - samples[i] = int(samples[i] * scale) - raw = samples.tobytes() + scale = cls._volume / 100.0 # float 0.0-1.0 + if scale < 0.9: + scaled = bytearray(len(raw)) + for i in range(0, len(raw), 2): + sample = int.from_bytes(raw[i:i+2], 'little', True) + sample = int(sample * scale) + scaled[i:i+2] = sample.to_bytes(2, 'little', True) + raw = bytes(scaled) # --------------------------------------------------------- if cls._i2s: diff --git a/internal_filesystem/apps/com.micropythonos.musicplayer/assets/music_player.py b/internal_filesystem/apps/com.micropythonos.musicplayer/assets/music_player.py index 4861efb6..8cbf7123 100644 --- a/internal_filesystem/apps/com.micropythonos.musicplayer/assets/music_player.py +++ b/internal_filesystem/apps/com.micropythonos.musicplayer/assets/music_player.py @@ -59,17 +59,17 @@ class FullscreenPlayer(Activity): self._filename = self.getIntent().extras.get("filename") qr_screen = lv.obj() self._slider_label=lv.label(qr_screen) - self._slider_label.set_text(f"Volume: 100%") + self._slider_label.set_text(f"Volume: {AudioPlayer.get_volume()}%") self._slider_label.align(lv.ALIGN.TOP_MID,0,lv.pct(4)) self._slider=lv.slider(qr_screen) self._slider.set_range(0,100) - self._slider.set_value(100,False) - self._slider.set_width(lv.pct(80)) + self._slider.set_value(AudioPlayer.get_volume(), False) + self._slider.set_width(lv.pct(90)) self._slider.align_to(self._slider_label,lv.ALIGN.OUT_BOTTOM_MID,0,10) def volume_slider_changed(e): volume_int = self._slider.get_value() self._slider_label.set_text(f"Volume: {volume_int}%") - # TODO: set volume using AudioPlayer.set_volume(volume_int) + AudioPlayer.set_volume(volume_int) self._slider.add_event_cb(volume_slider_changed,lv.EVENT.VALUE_CHANGED,None) self._filename_label = lv.label(qr_screen) self._filename_label.align(lv.ALIGN.CENTER,0,0)