From 653b008ef6c39207f8189abbc31bdff00fb2fdc6 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Wed, 28 Oct 2020 14:29:52 +0100
Subject: [PATCH] Smooth fade-out of scheduled fb. #44

---
 modules/scheduling/fallback.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/modules/scheduling/fallback.py b/modules/scheduling/fallback.py
index 7b9b8657..aa0e2305 100644
--- a/modules/scheduling/fallback.py
+++ b/modules/scheduling/fallback.py
@@ -22,6 +22,7 @@
 import logging
 
 from enum                       import Enum
+from datetime                   import timedelta
 
 from modules.base.config        import AuraConfig
 from modules.base.utils         import SimpleUtil as SU
@@ -219,5 +220,9 @@ class FallbackCommand(EngineExecutor):
             self.logger.info(SU.cyan("=== stop_fallback_playlist() ==="))
             Engine.get_instance().player.stop_fallback_playlist()
 
-        child_timer = EngineExecutor("FALLBACK", None, timeslot.end_unix, do_stop, None)
+        # Start fade-out 50% before the end of the timeslot for a more smooth transition
+        end_time_offset = int(float(AuraConfig.config().get("fade_out_time")) / 2 * 1000 * -1)
+        end_time = SU.timestamp(timeslot.schedule_end + timedelta(milliseconds=end_time_offset))
+        self.logger.info(f"Starting fade-out of scheduled fallback with an offset of {end_time_offset} milliseconds at {end_time}")
+        child_timer = EngineExecutor("FALLBACK", None, end_time, do_stop, None)
         super().__init__("FALLBACK", child_timer, timeslot.start_unix, do_play, entries)
\ No newline at end of file
-- 
GitLab