0
1
mirror of https://github.com/radio95-rnt/RadioPlayer.git synced 2026-02-26 21:53:54 +01:00

some changes

This commit is contained in:
2025-09-01 11:40:40 +02:00
parent 4f70a9bd84
commit f77b0ffd17
2 changed files with 113 additions and 143 deletions

View File

@@ -31,15 +31,6 @@ udp_host = ("127.0.0.1", 5000)
logger = log95.log95("radioPlayer") logger = log95.log95("radioPlayer")
# Global variables for state tracking # Global variables for state tracking
current_state = {
"current_file": None,
"start_time": None,
"duration": None,
"playlist_path": None,
"playlist_position": 0
}
state_thread = None
state_lock = threading.Lock()
# Crossfade management # Crossfade management
cross_for_cross_time = 0 cross_for_cross_time = 0
@@ -47,11 +38,70 @@ current_process = None
next_process = None next_process = None
process_lock = threading.Lock() process_lock = threading.Lock()
def get_current_hour(): class Time:
return datetime.now().hour @staticmethod
def get_day_hour():
return datetime.now().strftime('%A').lower(), datetime.now().hour
@staticmethod
def get_playlist_modification_time(playlist_path):
try:
return os.path.getmtime(playlist_path)
except OSError:
return 0
def get_current_day(): class StateManager:
return datetime.now().strftime('%A').lower() def __init__(self) -> None:
self._reset()
self._state_lock = threading.Lock()
def _reset(self):
self._current_state = {
"current_file": None,
"start_time": None,
"duration": None,
"playlist_path": None,
"playlist_position": 0
}
def save_state(self):
try:
with self._state_lock:
with open(state_file_path, 'w') as f:
json.dump(self._current_state, f)
except Exception as e:
logger.error(f"Error saving state: {e}")
def load_state(self):
try:
if os.path.exists(state_file_path):
with open(state_file_path, 'r') as f:
loaded_state = json.load(f)
with self._state_lock:
self._current_state.update(loaded_state)
logger.info(f"Loaded state: {self._current_state}")
return True
except Exception as e:
logger.error(f"Error loading state: {e}")
return False
def clear_state(self):
with self._state_lock:
self._reset()
try:
if os.path.exists(state_file_path):
os.remove(state_file_path)
except Exception as e:
logger.error(f"Error clearing state: {e}")
def update_current_state(self, file_path, playlist_path=None, playlist_position=0):
with self._state_lock:
self._current_state["current_file"] = file_path
self._current_state["start_time"] = time.time()
self._current_state["duration"] = get_audio_duration(file_path)
self._current_state["playlist_path"] = playlist_path
self._current_state["playlist_position"] = playlist_position
self.save_state()
def get_state(self):
with self._state_lock:
return self._current_state.copy()
stateman = StateManager()
def load_dict_from_custom_format(file_path: str) -> dict: def load_dict_from_custom_format(file_path: str) -> dict:
try: try:
@@ -67,90 +117,29 @@ def load_dict_from_custom_format(file_path: str) -> dict:
logger.error(f"{name_table_path} does not exist, or could not be accesed") logger.error(f"{name_table_path} does not exist, or could not be accesed")
return {} return {}
def save_state():
"""Save current state to file"""
try:
with state_lock:
with open(state_file_path, 'w') as f:
json.dump(current_state, f)
except Exception as e:
logger.error(f"Error saving state: {e}")
def load_state():
"""Load state from file"""
global current_state
try:
if os.path.exists(state_file_path):
with open(state_file_path, 'r') as f:
loaded_state = json.load(f)
with state_lock:
current_state.update(loaded_state)
logger.info(f"Loaded state: {current_state}")
return True
except Exception as e:
logger.error(f"Error loading state: {e}")
return False
def clear_state():
"""Clear state file"""
try:
if os.path.exists(state_file_path):
os.remove(state_file_path)
except Exception as e:
logger.error(f"Error clearing state: {e}")
def get_audio_duration(file_path): def get_audio_duration(file_path):
"""Get duration of audio file using ffprobe"""
try: try:
result = subprocess.run([ result = subprocess.run([
'ffprobe', '-v', 'quiet', '-show_entries', 'format=duration', 'ffprobe', '-v', 'quiet', '-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1', file_path '-of', 'default=noprint_wrappers=1:nokey=1', file_path
], capture_output=True, text=True) ], capture_output=True, text=True)
if result.returncode == 0: if result.returncode == 0: return float(result.stdout.strip())
duration = float(result.stdout.strip()) except Exception as e: logger.warning(f"Exception while reading audio duration: {e}")
return duration
except Exception as e:
logger.error(f"Error getting duration for {file_path}: {e}")
return None return None
def update_current_state(file_path, playlist_path=None, playlist_position=0):
"""Update current state with new file"""
with state_lock:
current_state["current_file"] = file_path
current_state["start_time"] = time.time()
current_state["duration"] = get_audio_duration(file_path)
current_state["playlist_path"] = playlist_path
current_state["playlist_position"] = playlist_position
save_state()
def clear_current_state():
"""Clear current playing state"""
with state_lock:
current_state["current_file"] = None
current_state["start_time"] = None
current_state["duration"] = None
save_state()
def should_resume_from_state(tracks, playlist_path): def should_resume_from_state(tracks, playlist_path):
"""Check if we should resume from saved state""" if not stateman.get_state()["current_file"] or not stateman.get_state()["start_time"]: return False, tracks, 0
if not current_state["current_file"] or not current_state["start_time"]:
return False, tracks, 0
# Check if the saved file is still in the current playlist # Check if the saved file is still in the current playlist
if current_state["current_file"] in tracks and current_state["playlist_path"] == playlist_path: if stateman.get_state()["current_file"] in tracks and stateman.get_state()["playlist_path"] == playlist_path:
elapsed = time.time() - current_state["start_time"] elapsed = time.time() - stateman.get_state()["start_time"]
if (stateman.get_state()["duration"] and elapsed < stateman.get_state()["duration"] and elapsed < 600):
# Only resume if less than the track duration and less than 10 minutes have passed
if (current_state["duration"] and elapsed < current_state["duration"] and elapsed < 600):
# Find position of the current file # Find position of the current file
try: try:
resume_index = tracks.index(current_state["current_file"]) logger.info(f"Resuming from {os.path.basename(stateman.get_state()['current_file'])} at {elapsed:.0f}s")
logger.info(f"Resuming from {os.path.basename(current_state['current_file'])} at {elapsed:.0f}s") return True, tracks, tracks.index(stateman.get_state()["current_file"])
return True, tracks, resume_index except ValueError: pass
except ValueError:
pass
return False, tracks, 0 return False, tracks, 0
def update_rds(track_name: str): def update_rds(track_name: str):
@@ -184,11 +173,7 @@ def update_rds(track_name: str):
f = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) f = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
f.settimeout(1.0) f.settimeout(1.0)
try: f.sendto(f"TEXT={prt}\r\n".encode(), udp_host)
f.sendto(f"TEXT={prt}\r\n".encode(), udp_host)
except socket.timeout:
logger.error("Could not send TEXT to RDS, timeout.")
return
logger.info("RT set to", prt) logger.info("RT set to", prt)
rtp = [4] # type 1 rtp = [4] # type 1
@@ -199,33 +184,19 @@ def update_rds(track_name: str):
rtp.append(len(title)) # len 2 rtp.append(len(title)) # len 2
rtp = list(map(str, rtp)) rtp = list(map(str, rtp))
try: f.sendto(f"RTP={','.join(rtp)}\r\n".encode(), udp_host)
f.sendto(f"RTP={','.join(rtp)}\r\n".encode(), udp_host)
except socket.timeout:
logger.error("Could not send TEXT to RDS, timeout.")
return
f.close() f.close()
except Exception as e: except Exception as e: logger.error(f"Error updating RDS: {e}")
logger.error(f"Error updating RDS: {e}")
def get_playlist_modification_time(playlist_path):
try:
return os.path.getmtime(playlist_path)
except OSError:
return 0
def load_playlist(playlist_path): def load_playlist(playlist_path):
try: try:
with open(playlist_path, 'r') as f: with open(playlist_path, 'r') as f: return [line.strip() for line in f.readlines() if line.strip()]
tracks = [line.strip() for line in f.readlines() if line.strip()]
return tracks
except FileNotFoundError: except FileNotFoundError:
logger.error(f"Playlist not found: {playlist_path}") logger.error(f"Playlist not found: {playlist_path}")
return [] return []
def get_newest_track(tracks): def get_newest_track(tracks):
if not tracks: if not tracks: return None
return None
newest_track = None newest_track = None
newest_time = 0 newest_time = 0
@@ -237,8 +208,7 @@ def get_newest_track(tracks):
if mod_time > newest_time: if mod_time > newest_time:
newest_time = mod_time newest_time = mod_time
newest_track = track newest_track = track
except OSError: except OSError: continue
continue
return newest_track return newest_track
@@ -266,8 +236,7 @@ def stop_all_processes():
try: try:
current_process.kill() current_process.kill()
current_process.wait(timeout=2) current_process.wait(timeout=2)
except (subprocess.TimeoutExpired, ProcessLookupError): except (subprocess.TimeoutExpired, ProcessLookupError): pass
pass
current_process = None current_process = None
if next_process and next_process.poll() is None: if next_process and next_process.poll() is None:
@@ -334,7 +303,7 @@ def play_audio_with_crossfade(current_track_path, next_track_path=None, resume_s
def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=False, do_shuffle=True): def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=False, do_shuffle=True):
global current_process, next_process, cross_for_cross_time global current_process, next_process, cross_for_cross_time
last_modified_time = get_playlist_modification_time(playlist_path) last_modified_time = Time.get_playlist_modification_time(playlist_path)
tracks = load_playlist(playlist_path) tracks = load_playlist(playlist_path)
if not tracks: if not tracks:
logger.info(f"No tracks found in {playlist_path}, checking again in 15 seconds...") logger.info(f"No tracks found in {playlist_path}, checking again in 15 seconds...")
@@ -350,8 +319,7 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
if should_resume: if should_resume:
start_index = resume_index start_index = resume_index
with state_lock: resume_seconds = time.time() - stateman.get_state()["start_time"]
resume_seconds = time.time() - current_state["start_time"]
else: else:
# Normal playlist preparation # Normal playlist preparation
if play_newest_first: if play_newest_first:
@@ -370,19 +338,18 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
for i, track in enumerate(tracks[start_index:], start_index): for i, track in enumerate(tracks[start_index:], start_index):
if return_pending: if return_pending:
stop_all_processes() stop_all_processes()
clear_current_state() stateman.clear_state()
return return
track_path = os.path.abspath(os.path.expanduser(track)) track_path = os.path.abspath(os.path.expanduser(track))
track_name = os.path.basename(track_path) track_name = os.path.basename(track_path)
current_modified_time = get_playlist_modification_time(playlist_path) current_modified_time = Time.get_playlist_modification_time(playlist_path)
if current_modified_time > last_modified_time: if current_modified_time > last_modified_time:
logger.info(f"Playlist {playlist_path} has been modified, reloading...") logger.info(f"Playlist {playlist_path} has been modified, reloading...")
return_pending = True return_pending = True
continue continue
current_hour = get_current_hour() current_day, current_hour = Time.get_day_hour()
current_day = get_current_day()
morning_playlist_path = os.path.join(playlist_dir, current_day, 'morning') morning_playlist_path = os.path.join(playlist_dir, current_day, 'morning')
day_playlist_path = os.path.join(playlist_dir, current_day, 'day') day_playlist_path = os.path.join(playlist_dir, current_day, 'day')
night_playlist_path = os.path.join(playlist_dir, current_day, 'night') night_playlist_path = os.path.join(playlist_dir, current_day, 'night')
@@ -414,7 +381,7 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
action = check_control_files() action = check_control_files()
if action == "quit": if action == "quit":
stop_all_processes() stop_all_processes()
clear_state() stateman.clear_state()
exit() exit()
elif action == "reload": elif action == "reload":
logger.info("Reload requested during playback...") logger.info("Reload requested during playback...")
@@ -425,7 +392,7 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
with process_lock: with process_lock:
next_process = create_audio_process(track_path, fade_in=True, fade_out=True) next_process = create_audio_process(track_path, fade_in=True, fade_out=True)
update_rds(track_name) update_rds(track_name)
update_current_state(track_path, playlist_path, i) stateman.update_current_state(track_path, playlist_path, i)
# Wait for crossfade to complete # Wait for crossfade to complete
time.sleep(CROSSFADE_DURATION * 1.5) time.sleep(CROSSFADE_DURATION * 1.5)
@@ -446,7 +413,7 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
current_process = None current_process = None
# Update state before playing # Update state before playing
update_current_state(track_path, playlist_path, i) stateman.update_current_state(track_path, playlist_path, i)
if i == start_index and resume_seconds > 0: if i == start_index and resume_seconds > 0:
logger.info(f"Resuming: {track_name} at {resume_seconds:.0f}s") logger.info(f"Resuming: {track_name} at {resume_seconds:.0f}s")
@@ -491,7 +458,7 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
action = check_control_files() action = check_control_files()
if action == "quit": if action == "quit":
stop_all_processes() stop_all_processes()
clear_state() stateman.clear_state()
exit() exit()
elif action == "reload": elif action == "reload":
logger.info("Reload requested during playback...") logger.info("Reload requested during playback...")
@@ -501,7 +468,7 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
logger.info(f"Starting crossfade to: {os.path.basename(next_track_path)}") logger.info(f"Starting crossfade to: {os.path.basename(next_track_path)}")
with process_lock: with process_lock:
next_process = create_audio_process(next_track_path, fade_in=True, fade_out=True) next_process = create_audio_process(next_track_path, fade_in=True, fade_out=True)
update_rds(next_track_path) update_rds(os.path.basename(next_track_path))
# Wait for crossfade to complete # Wait for crossfade to complete
time.sleep(CROSSFADE_DURATION * 1.5) time.sleep(CROSSFADE_DURATION * 1.5)
@@ -522,20 +489,20 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
current_process = None current_process = None
update_current_state(next_track_path, playlist_path, i+1) stateman.update_current_state(next_track_path, playlist_path, i+1)
# Check control files after each song # Check control files after each song
action = check_control_files() action = check_control_files()
if can_delete_file("/tmp/radioPlayer_onplaylist"): action = None if can_delete_file("/tmp/radioPlayer_onplaylist"): action = None
if action == "quit": if action == "quit":
stop_all_processes() stop_all_processes()
clear_state() stateman.clear_state()
exit() exit()
elif action == "reload": elif action == "reload":
logger.info("Reload requested, restarting with new arguments...") logger.info("Reload requested, restarting with new arguments...")
stop_all_processes() stop_all_processes()
return "reload" return "reload"
clear_state() stateman.clear_state()
def can_delete_file(filepath): def can_delete_file(filepath):
if not os.path.isfile(filepath): if not os.path.isfile(filepath):
@@ -596,7 +563,7 @@ def parse_arguments():
def main(): def main():
# Load state at startup # Load state at startup
state_loaded = load_state() state_loaded = stateman.load_state()
try: try:
while True: # Main reload loop while True: # Main reload loop
@@ -605,40 +572,40 @@ def main():
if pre_track_path: if pre_track_path:
track_name = os.path.basename(pre_track_path) track_name = os.path.basename(pre_track_path)
logger.info(f"Now playing: {track_name}") logger.info(f"Now playing: {track_name}")
update_current_state(pre_track_path) stateman.update_current_state(pre_track_path)
update_rds(track_name) update_rds(track_name)
play_single_track(pre_track_path) play_single_track(pre_track_path)
clear_current_state() stateman.clear_state()
action = check_control_files() action = check_control_files()
if can_delete_file("/tmp/radioPlayer_onplaylist"): action = None if can_delete_file("/tmp/radioPlayer_onplaylist"): action = None
if action == "quit": if action == "quit":
clear_state() stateman.clear_state()
exit() exit()
elif action == "reload": elif action == "reload":
logger.info("Reload requested, restarting with new arguments...") logger.info("Reload requested, restarting with new arguments...")
continue # Restart the main loop continue # Restart the main loop
# Check if we should resume from loaded state first # Check if we should resume from loaded state first
if state_loaded and current_state.get("current_file") and current_state.get("playlist_path"): if state_loaded and stateman.get_state().get("current_file") and stateman.get_state().get("playlist_path"):
# Try to resume from the loaded state # Try to resume from the loaded state
if os.path.exists(current_state["playlist_path"]): if os.path.exists(stateman.get_state()["playlist_path"]):
logger.info(f"Attempting to resume from saved state: {current_state['playlist_path']}") logger.info(f"Attempting to resume from saved state: {stateman.get_state()['playlist_path']}")
# Check if the saved playlist is a time-based playlist (not custom) # Check if the saved playlist is a time-based playlist (not custom)
# A time-based playlist ends with 'morning', 'day', 'night', or 'late_night' # A time-based playlist ends with 'morning', 'day', 'night', or 'late_night'
saved_playlist_name = os.path.basename(current_state["playlist_path"]) saved_playlist_name = os.path.basename(stateman.get_state()["playlist_path"])
is_time_based_playlist = saved_playlist_name in ['morning', 'day', 'night', 'late_night'] is_time_based_playlist = saved_playlist_name in ['morning', 'day', 'night', 'late_night']
result = play_playlist(current_state["playlist_path"], result = play_playlist(stateman.get_state()["playlist_path"],
not is_time_based_playlist, # custom_playlist = NOT time-based not is_time_based_playlist, # custom_playlist = NOT time-based
play_newest_first, do_shuffle) play_newest_first, do_shuffle)
state_loaded = False # Don't try to resume again state_loaded = False # Don't try to resume again
if result == "reload": if result == "reload":
continue continue
else: else:
logger.warning(f"Saved playlist path no longer exists: {current_state['playlist_path']}") logger.warning(f"Saved playlist path no longer exists: {stateman.get_state()['playlist_path']}")
clear_current_state() stateman.clear_state()
state_loaded = False state_loaded = False
playlist_loop_active = True playlist_loop_active = True
@@ -650,8 +617,7 @@ def main():
playlist_loop_active = False # Break out to reload playlist_loop_active = False # Break out to reload
continue continue
current_hour = get_current_hour() current_day, current_hour = Time.get_day_hour()
current_day = get_current_day()
morning_playlist = os.path.join(playlist_dir, current_day, 'morning') morning_playlist = os.path.join(playlist_dir, current_day, 'morning')
day_playlist = os.path.join(playlist_dir, current_day, 'day') day_playlist = os.path.join(playlist_dir, current_day, 'day')
@@ -692,7 +658,7 @@ def main():
if action == "quit": if action == "quit":
if os.path.exists("/tmp/radioPlayer_onplaylist"): if os.path.exists("/tmp/radioPlayer_onplaylist"):
os.remove("/tmp/radioPlayer_onplaylist") os.remove("/tmp/radioPlayer_onplaylist")
clear_state() stateman.clear_state()
exit() exit()
elif action == "reload": elif action == "reload":
if os.path.exists("/tmp/radioPlayer_onplaylist"): if os.path.exists("/tmp/radioPlayer_onplaylist"):

View File

@@ -184,7 +184,7 @@ class PlaylistManager:
def load_playlists(self, days: List[str]) -> Dict[str, Dict[str, Set[str]]]: def load_playlists(self, days: List[str]) -> Dict[str, Dict[str, Set[str]]]:
"""Load all playlists from disk.""" """Load all playlists from disk."""
if self.config.is_custom_mode: if self.config.is_custom_mode and self.config.custom_playlist_file:
# In custom mode, we only need one "day" entry # In custom mode, we only need one "day" entry
playlists = {"custom": {period: set() for period in self.periods}} playlists = {"custom": {period: set() for period in self.periods}}
# Load existing custom playlist if it exists # Load existing custom playlist if it exists
@@ -228,6 +228,7 @@ class PlaylistManager:
def _update_custom_playlist(self, file_item: FileItem, add: bool): def _update_custom_playlist(self, file_item: FileItem, add: bool):
"""Update the custom playlist file.""" """Update the custom playlist file."""
if not self.config.custom_playlist_file: raise Exception
# Ensure the directory exists # Ensure the directory exists
os.makedirs(os.path.dirname(self.config.custom_playlist_file), exist_ok=True) os.makedirs(os.path.dirname(self.config.custom_playlist_file), exist_ok=True)
@@ -472,8 +473,9 @@ class DisplayManager:
def draw_header(self, playlists: Dict, current_day: str, current_day_idx: int, def draw_header(self, playlists: Dict, current_day: str, current_day_idx: int,
days: List[str], term_width: int, all_file_items: List[FileItem], days: List[str], term_width: int, all_file_items: List[FileItem],
force_redraw: bool = False, state: InterfaceState = None): force_redraw: bool = False, state: InterfaceState | None = None):
"""Draw the header, only if content has changed.""" """Draw the header, only if content has changed."""
if not state: raise Exception
result = self.stats.calculate_category_percentages(playlists, current_day, self.config, all_file_items) result = self.stats.calculate_category_percentages(playlists, current_day, self.config, all_file_items)
percentages, polskie_percentages, total_pl = result or ({}, {}, 0) percentages, polskie_percentages, total_pl = result or ({}, {}, 0)
@@ -514,8 +516,9 @@ class DisplayManager:
return 2 if self.config.is_custom_mode else 3 return 2 if self.config.is_custom_mode else 3
def draw_search_bar(self, search_term: str, term_width: int, force_redraw: bool = False, def draw_search_bar(self, search_term: str, term_width: int, force_redraw: bool = False,
state: InterfaceState = None): state: InterfaceState | None = None):
"""Draw the search bar, only if the search term has changed.""" """Draw the search bar, only if the search term has changed."""
if not state: raise Exception
# Optimization: Only redraw if search term changes # Optimization: Only redraw if search term changes
if force_redraw or state.last_search != search_term: if force_redraw or state.last_search != search_term:
search_row = self.get_header_height() + 3 search_row = self.get_header_height() + 3
@@ -527,8 +530,9 @@ class DisplayManager:
def draw_files_section(self, file_items: List[FileItem], playlists: Dict, selected_idx: int, def draw_files_section(self, file_items: List[FileItem], playlists: Dict, selected_idx: int,
current_day: str, scroll_offset: int, term_width: int, term_height: int, current_day: str, scroll_offset: int, term_width: int, term_height: int,
force_redraw: bool = False, state: InterfaceState = None): force_redraw: bool = False, state: InterfaceState | None = None):
"""Draw the files list, optimized to only redraw when necessary.""" """Draw the files list, optimized to only redraw when necessary."""
if not state: raise Exception
header_height = self.get_header_height() header_height = self.get_header_height()
content_start_row = header_height + 5 content_start_row = header_height + 5
available_lines = term_height - content_start_row available_lines = term_height - content_start_row