From f95edfc6760c835991de822249ac78514b820207 Mon Sep 17 00:00:00 2001
From: moon2 <moon2protonmail@protonmail.com>
Date: Sun, 3 Sep 2023 21:45:08 +0000
Subject: [PATCH] py-st3m: make use of audio backend

c-st3m: bugfix: fake mute triggered real mute
---
 components/st3m/st3m_audio.c                |  3 ++-
 python_payload/st3m/processors.py           | 24 +++------------------
 python_payload/st3m/run.py                  |  6 +++++-
 python_payload/st3m/ui/elements/overlays.py | 17 +++------------
 sim/fakes/audio.py                          | 12 +++++++++++
 5 files changed, 25 insertions(+), 37 deletions(-)

diff --git a/components/st3m/st3m_audio.c b/components/st3m/st3m_audio.c
index 1ea7111aec..2c296747c5 100644
--- a/components/st3m/st3m_audio.c
+++ b/components/st3m/st3m_audio.c
@@ -64,7 +64,6 @@ static float _output_set_volume(st3m_audio_output_t *out, float vol_dB) {
     }
     if (vol_dB < out->volume_min) {
         vol_dB = SILLY_LOW_VOLUME_DB;
-        out->mute = true;
     }
     out->volume = vol_dB;
     _output_apply(out);
@@ -131,6 +130,7 @@ static float _output_get_volume_relative(st3m_audio_output_t *out) {
 static void _audio_headphones_apply(st3m_audio_output_t *out) {
     bool mute = out->mute;
     float vol_dB = out->volume;
+    if (out->volume < (SILLY_LOW_VOLUME_DB + 1)) mute = true;
 
     bool headphones = _headphones_connected();
     if (!headphones) {
@@ -153,6 +153,7 @@ static void _audio_headphones_apply(st3m_audio_output_t *out) {
 static void _audio_speaker_apply(st3m_audio_output_t *out) {
     bool mute = out->mute;
     float vol_dB = out->volume;
+    if (out->volume < (SILLY_LOW_VOLUME_DB + 1)) mute = true;
 
     bool headphones = _headphones_connected();
     if (headphones) {
diff --git a/python_payload/st3m/processors.py b/python_payload/st3m/processors.py
index 6f7e78b40f..9ff75199ca 100644
--- a/python_payload/st3m/processors.py
+++ b/python_payload/st3m/processors.py
@@ -30,32 +30,14 @@ class AudioProcessor(Processor):
     def __init__(self) -> None:
         super().__init__()
         self.input = InputController()
+        self.volume_step = 1.5
 
     def think(self, ins: InputState, delta_ms: int) -> None:
         self.input.think(ins, delta_ms)
-        # Whether the volume was adjusted. Used to do a second pass.
-        adjusted = False
-        # Whether the volume is so low that we should enable mute.
-        should_mute = False
         if self.input.buttons.os.left.pressed:
-            started_at = audio.get_volume_dB()
-            if started_at <= -20:
-                should_mute = True
-            audio.adjust_volume_dB(-5)
-            adjusted = True
+            audio.adjust_volume_dB(-self.volume_step)
         if self.input.buttons.os.right.pressed:
-            if not audio.get_mute():
-                audio.adjust_volume_dB(5)
-            adjusted = True
-        if adjusted:
-            # Clamp lower level to -20dB.
-            if audio.get_volume_dB() < -20:
-                audio.set_volume_dB(-20)
-            # Set audio mute
-            if should_mute:
-                audio.set_mute(True)
-            else:
-                audio.set_mute(False)
+            audio.adjust_volume_dB(self.volume_step)
 
 
 class ProcessorMidldeware(Responder):
diff --git a/python_payload/st3m/run.py b/python_payload/st3m/run.py
index 1f22ab7f2f..bde74b88c3 100644
--- a/python_payload/st3m/run.py
+++ b/python_payload/st3m/run.py
@@ -145,7 +145,11 @@ def run_main() -> None:
     log.info(f"free memory: {gc.mem_free()}")
 
     captouch.calibration_request()
-    audio.set_volume_dB(-10)  # slightly less loud startup volume
+    # defaults, maybe expose in a config file someday
+    audio.set_volume_dB(-10)
+    audio.headphones_set_minimum_volume_dB(-30)
+    audio.speaker_set_minimum_volume_dB(-30)
+
     leds.set_rgb(0, 255, 0, 0)
     leds.update()
     bundles = BundleManager()
diff --git a/python_payload/st3m/ui/elements/overlays.py b/python_payload/st3m/ui/elements/overlays.py
index c3ae78c7d4..88b61784b3 100644
--- a/python_payload/st3m/ui/elements/overlays.py
+++ b/python_payload/st3m/ui/elements/overlays.py
@@ -302,17 +302,9 @@ class OverlayVolume(Overlay):
         return False
 
     def think(self, ins: InputState, delta_ms: int) -> None:
-        self._volume = audio.get_volume_dB()
+        self._volume = audio.get_volume_relative()
         self._headphones = audio.headphones_are_connected()
-        self._muted = audio.get_mute()
-        if self._headphones:
-            # -20 ... +3dB
-            self._volume += 20
-            self._volume /= 20 + 3
-        else:
-            # -20 ... +14dB
-            self._volume += 20
-            self._volume /= 20 + 14
+        self._muted = (self._volume == 0) or audio.get_mute()
 
         if self._started:
             if self.changed():
@@ -380,10 +372,7 @@ class OverlayVolume(Overlay):
         ctx.line_width = 2
         ctx.stroke()
 
-        v = self._volume
-        v = min(max(v, 0), 1)
-
-        width = 60 * v
+        width = 60 * self._volume
         ctx.round_rectangle(-30, 20, width, 10, 3)
         ctx.fill()
 
diff --git a/sim/fakes/audio.py b/sim/fakes/audio.py
index 1f4e885b9a..0633298f34 100644
--- a/sim/fakes/audio.py
+++ b/sim/fakes/audio.py
@@ -12,6 +12,18 @@ def get_volume_dB() -> float:
     return _volume
 
 
+def get_volume_relative() -> float:
+    return 0
+
+
+def headphones_set_minimum_volume_dB(v: float) -> None:
+    pass
+
+
+def speaker_set_minimum_volume_dB(v: float) -> None:
+    pass
+
+
 def adjust_volume_dB(v) -> float:
     global _volume
     _volume += v
-- 
GitLab