From a83ad72041a8dd99205c85aa9ca7816522cbce83 Mon Sep 17 00:00:00 2001
From: Sebastian Krzyszkowiak <dos@dosowisko.net>
Date: Tue, 14 Nov 2023 00:39:57 +0100
Subject: [PATCH] sim: Implement seeking and duration in media fake using pymad

---
 docs/badge/firmware-development.rst |  2 +-
 docs/badge/programming.rst          |  2 +-
 sim/fakes/media.py                  | 16 ++++++++++++++--
 sim/requirements.txt                |  1 +
 sim/run.py                          |  4 ++++
 5 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/docs/badge/firmware-development.rst b/docs/badge/firmware-development.rst
index 65579de0ee..f3338b31e5 100644
--- a/docs/badge/firmware-development.rst
+++ b/docs/badge/firmware-development.rst
@@ -53,7 +53,7 @@ For running the simulator, you'll need Python 3 with pygame and wasmer:
 ::
 
 	$ python3 -m venv venv
-	$ venv/bin/pip install pygame requests
+	$ venv/bin/pip install pygame requests pymad
         $ venv/bin/pip install wasmer wasmer-compiler-cranelift
 
 .. warning::
diff --git a/docs/badge/programming.rst b/docs/badge/programming.rst
index adccc6366f..d504edcf15 100644
--- a/docs/badge/programming.rst
+++ b/docs/badge/programming.rst
@@ -746,7 +746,7 @@ environment with the required packages:
     $ git clone https://git.flow3r.garden/flow3r/flow3r-firmware
     $ cd flow3r-firmware
     $ python3 -m venv venv
-    $ venv/bin/pip install pygame requests
+    $ venv/bin/pip install pygame requests pymad
     $ venv/bin/pip install wasmer wasmer-compiler-cranelift
 
 .. warning::
diff --git a/sim/fakes/media.py b/sim/fakes/media.py
index 60bf2d0827..4e2eb625e9 100644
--- a/sim/fakes/media.py
+++ b/sim/fakes/media.py
@@ -1,7 +1,13 @@
 import pygame
 from _sim import path_replace
 
+try:
+    import mad
+except ImportError:
+    mad = None
+
 _loaded = False
+_duration = 0
 
 
 def stop():
@@ -18,11 +24,13 @@ def load(path, paused=False):
     """
     Load path
     """
-    global _loaded
+    global _loaded, _duration
     if path.startswith(("http://", "https://")):
         return
     pygame.mixer.music.load(path_replace(path))
     pygame.mixer.music.play()
+    if mad:
+        _duration = mad.MadFile(path_replace(path)).total_time()
     if paused:
         pygame.mixer.music.pause()
     _loaded = True
@@ -90,10 +98,14 @@ def seek(pos):
         return
     pygame.mixer.music.play()
     pygame.mixer.music.rewind()
+    if mad:
+        pygame.mixer.music.set_pos(pos * get_duration())
 
 
 def get_duration():
-    return 99999
+    if not mad:
+        return 99999
+    return _duration / 1000
 
 
 def is_visual():
diff --git a/sim/requirements.txt b/sim/requirements.txt
index 42e7cd3c2d..c82ae356d0 100644
--- a/sim/requirements.txt
+++ b/sim/requirements.txt
@@ -1,4 +1,5 @@
 pygame
 requests
+pymad
 wasmer
 wasmer-compiler-cranelift
diff --git a/sim/run.py b/sim/run.py
index b79533699b..6fb50ea142 100755
--- a/sim/run.py
+++ b/sim/run.py
@@ -29,6 +29,10 @@ try:
 except ImportError:
     print("Warning: `requests` is missing so no `urequests` mock will exist")
 
+try:
+    import mad
+except ImportError:
+    print("Warning: `mad` is missing, MP3 support in `media` mock will be limited")
 
 sys_path_orig = sys.path
 
-- 
GitLab