You've already forked RadioPlayer
mirror of
https://github.com/radio95-rnt/RadioPlayer.git
synced 2026-02-27 06:03:52 +01:00
crossfade
This commit is contained in:
171
radioPlayer.py
171
radioPlayer.py
@@ -16,6 +16,8 @@ DAY_END = 20
|
||||
LATE_NIGHT_START = 0
|
||||
LATE_NIGHT_END = 6
|
||||
|
||||
CROSSFADE_DURATION = 5 # seconds
|
||||
|
||||
playlist_dir = "/home/user/playlists"
|
||||
name_table_path = "/home/user/mixes/name_table.txt"
|
||||
state_file_path = "/tmp/radioPlayer_state.json"
|
||||
@@ -39,6 +41,11 @@ current_state = {
|
||||
state_thread = None
|
||||
state_lock = threading.Lock()
|
||||
|
||||
# Crossfade management
|
||||
current_process = None
|
||||
next_process = None
|
||||
process_lock = threading.Lock()
|
||||
|
||||
def get_current_hour():
|
||||
return datetime.now().hour
|
||||
|
||||
@@ -220,19 +227,129 @@ def check_control_files():
|
||||
|
||||
return None
|
||||
|
||||
def play_audio_with_resume(track_path, resume_seconds=0):
|
||||
"""Play audio file, optionally resuming from a specific position"""
|
||||
def stop_all_processes():
|
||||
"""Stop all ffplay processes"""
|
||||
global current_process, next_process
|
||||
with process_lock:
|
||||
if current_process and current_process.poll() is None:
|
||||
try:
|
||||
current_process.terminate()
|
||||
current_process.wait(timeout=2)
|
||||
except (subprocess.TimeoutExpired, ProcessLookupError):
|
||||
try:
|
||||
current_process.kill()
|
||||
current_process.wait(timeout=2)
|
||||
except (subprocess.TimeoutExpired, ProcessLookupError):
|
||||
pass
|
||||
current_process = None
|
||||
|
||||
if next_process and next_process.poll() is None:
|
||||
try:
|
||||
next_process.terminate()
|
||||
next_process.wait(timeout=2)
|
||||
except (subprocess.TimeoutExpired, ProcessLookupError):
|
||||
try:
|
||||
next_process.kill()
|
||||
next_process.wait(timeout=2)
|
||||
except (subprocess.TimeoutExpired, ProcessLookupError):
|
||||
pass
|
||||
next_process = None
|
||||
|
||||
def create_audio_process(track_path, resume_seconds=0, fade_in=False, volume=1.0):
|
||||
"""Create ffplay process with optional fade effects and volume control"""
|
||||
cmd = ['ffplay', '-nodisp', '-hide_banner', '-autoexit', '-loglevel', 'quiet']
|
||||
|
||||
resume_seconds -= 5
|
||||
resume_seconds = max(0, resume_seconds)
|
||||
# Build filter chain
|
||||
filters = []
|
||||
|
||||
# Add fade in if requested
|
||||
if fade_in:
|
||||
filters.append(f"afade=t=in:st=0:d={CROSSFADE_DURATION}")
|
||||
|
||||
# Add volume control
|
||||
if volume != 1.0:
|
||||
filters.append(f"volume={volume}")
|
||||
|
||||
# Apply filters if any exist
|
||||
if filters:
|
||||
filter_chain = ",".join(filters)
|
||||
cmd.extend(['-af', filter_chain])
|
||||
|
||||
# Add resume position if specified
|
||||
resume_seconds = max(0, resume_seconds - 5) # Start 5 seconds earlier for better sync
|
||||
if resume_seconds > 0:
|
||||
cmd.extend(['-ss', str(resume_seconds)])
|
||||
|
||||
cmd.append(track_path)
|
||||
|
||||
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
return subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
|
||||
def play_single_track(track_path, resume_seconds=0):
|
||||
"""Play a single track without crossfade (for first track or special cases)"""
|
||||
global current_process
|
||||
|
||||
with process_lock:
|
||||
current_process = create_audio_process(track_path, resume_seconds, fade_in=True)
|
||||
|
||||
# Wait for the process to complete
|
||||
current_process.wait()
|
||||
|
||||
with process_lock:
|
||||
current_process = None
|
||||
|
||||
def play_audio_with_crossfade(current_track_path, next_track_path=None, resume_seconds=0):
|
||||
"""Play audio file with crossfade to next track"""
|
||||
global current_process, next_process
|
||||
|
||||
# Get duration of current track
|
||||
duration = get_audio_duration(current_track_path)
|
||||
if not duration:
|
||||
logger.warning(f"Could not get duration for {current_track_path}, playing without crossfade")
|
||||
play_single_track(current_track_path, resume_seconds)
|
||||
return
|
||||
|
||||
# Calculate when to start the next track (5 seconds before end)
|
||||
crossfade_start_time = max(0, duration - CROSSFADE_DURATION - resume_seconds)
|
||||
|
||||
# Start current track with fade in
|
||||
with process_lock:
|
||||
current_process = create_audio_process(current_track_path, resume_seconds, fade_in=True)
|
||||
|
||||
if next_track_path and crossfade_start_time > 0:
|
||||
# Wait until it's time to start the crossfade
|
||||
time.sleep(crossfade_start_time)
|
||||
|
||||
# Check if we need to stop due to control files
|
||||
action = check_control_files()
|
||||
if action in ["quit", "reload"]:
|
||||
stop_all_processes()
|
||||
return action
|
||||
|
||||
# Start next track with fade in and reduced volume initially
|
||||
logger.info(f"Starting crossfade to: {os.path.basename(next_track_path)}")
|
||||
with process_lock:
|
||||
next_process = create_audio_process(next_track_path, fade_in=True, volume=0.8)
|
||||
|
||||
# Wait for crossfade to complete
|
||||
time.sleep(CROSSFADE_DURATION)
|
||||
|
||||
# Stop current track (it should fade out naturally)
|
||||
with process_lock:
|
||||
if current_process and current_process.poll() is None:
|
||||
try:
|
||||
current_process.terminate()
|
||||
current_process.wait(timeout=2)
|
||||
except (subprocess.TimeoutExpired, ProcessLookupError):
|
||||
pass
|
||||
current_process = next_process
|
||||
next_process = None
|
||||
else:
|
||||
# No crossfade, just wait for current track to finish
|
||||
current_process.wait()
|
||||
with process_lock:
|
||||
current_process = None
|
||||
|
||||
return None
|
||||
|
||||
def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=False, do_shuffle=True):
|
||||
last_modified_time = get_playlist_modification_time(playlist_path)
|
||||
@@ -270,6 +387,7 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
|
||||
current_modified_time = get_playlist_modification_time(playlist_path)
|
||||
if current_modified_time > last_modified_time:
|
||||
logger.info(f"Playlist {playlist_path} has been modified, reloading...")
|
||||
stop_all_processes()
|
||||
clear_current_state()
|
||||
return
|
||||
|
||||
@@ -283,21 +401,25 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
|
||||
if DAY_START <= current_hour < DAY_END and not custom_playlist:
|
||||
if playlist_path != day_playlist_path:
|
||||
logger.info("Time changed to day hours, switching playlist...")
|
||||
stop_all_processes()
|
||||
clear_current_state()
|
||||
return
|
||||
elif MORNING_START <= current_hour < MORNING_END and not custom_playlist:
|
||||
if playlist_path != morning_playlist_path:
|
||||
logger.info("Time changed to morning hours, switching playlist...")
|
||||
stop_all_processes()
|
||||
clear_current_state()
|
||||
return
|
||||
elif LATE_NIGHT_START <= current_hour < LATE_NIGHT_END and not custom_playlist:
|
||||
if playlist_path != late_night_playlist_path:
|
||||
logger.info("Time changed to late night hours, switching playlist...")
|
||||
stop_all_processes()
|
||||
clear_current_state()
|
||||
return
|
||||
else:
|
||||
if playlist_path != night_playlist_path and not custom_playlist:
|
||||
logger.info("Time changed to night hours, switching playlist...")
|
||||
stop_all_processes()
|
||||
clear_current_state()
|
||||
return
|
||||
|
||||
@@ -315,7 +437,27 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
|
||||
|
||||
update_rds(track_name)
|
||||
|
||||
play_audio_with_resume(track_path, resume_seconds)
|
||||
# Determine next track for crossfade
|
||||
next_track_path = None
|
||||
next_index = i + 1
|
||||
if next_index < len(tracks):
|
||||
next_track_path = os.path.abspath(os.path.expanduser(tracks[next_index]))
|
||||
|
||||
# Play with crossfade
|
||||
if i == start_index and resume_seconds > 0:
|
||||
# For resumed tracks, use simpler playback
|
||||
result = play_audio_with_crossfade(track_path, next_track_path, resume_seconds)
|
||||
else:
|
||||
result = play_audio_with_crossfade(track_path, next_track_path)
|
||||
|
||||
if result == "quit":
|
||||
stop_all_processes()
|
||||
clear_state()
|
||||
exit()
|
||||
elif result == "reload":
|
||||
logger.info("Reload requested during playback...")
|
||||
stop_all_processes()
|
||||
return "reload"
|
||||
|
||||
# Clear state after track finishes
|
||||
clear_current_state()
|
||||
@@ -324,10 +466,12 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
|
||||
action = check_control_files()
|
||||
if can_delete_file("/tmp/radioPlayer_onplaylist"): action = None
|
||||
if action == "quit":
|
||||
stop_all_processes()
|
||||
clear_state()
|
||||
exit()
|
||||
elif action == "reload":
|
||||
logger.info("Reload requested, restarting with new arguments...")
|
||||
stop_all_processes()
|
||||
return "reload"
|
||||
|
||||
def can_delete_file(filepath):
|
||||
@@ -357,6 +501,8 @@ def parse_arguments():
|
||||
print(" n - Play newest song first")
|
||||
print(" list:playlist;options - Play custom playlist with options")
|
||||
print(" /path/to/file - Play specific file first")
|
||||
print()
|
||||
print("Crossfade: 5-second crossfade is automatically applied between tracks")
|
||||
exit(0)
|
||||
|
||||
if can_delete_file("/tmp/radioPlayer_arg"):
|
||||
@@ -389,6 +535,7 @@ def main():
|
||||
# Load state at startup
|
||||
state_loaded = load_state()
|
||||
|
||||
try:
|
||||
while True: # Main reload loop
|
||||
arg, play_newest_first, do_shuffle, pre_track_path, selected_list = parse_arguments()
|
||||
|
||||
@@ -397,7 +544,7 @@ def main():
|
||||
logger.info(f"Now playing: {track_name}")
|
||||
update_current_state(pre_track_path)
|
||||
update_rds(track_name)
|
||||
subprocess.run(['ffplay', '-nodisp', '-hide_banner', '-autoexit', '-loglevel', 'quiet', pre_track_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
play_single_track(pre_track_path)
|
||||
clear_current_state()
|
||||
|
||||
action = check_control_files()
|
||||
@@ -490,11 +637,15 @@ def main():
|
||||
if result == "reload":
|
||||
playlist_loop_active = False # Break out to reload
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except KeyboardInterrupt:
|
||||
logger.info("Player stopped by user")
|
||||
stop_all_processes()
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error: {e}")
|
||||
stop_all_processes()
|
||||
raise
|
||||
finally:
|
||||
stop_all_processes()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user