2024-12-21 16:07:27 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
import pygame.mixer
|
2024-12-21 19:00:06 +01:00
|
|
|
import pygame.event
|
2024-12-21 16:07:27 +01:00
|
|
|
from .track import Track
|
2024-12-21 19:42:26 +01:00
|
|
|
from .track_progress_timer import TrackProgress
|
2024-12-22 16:11:43 +01:00
|
|
|
from .play_state import PlayState
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Player:
|
2024-12-22 16:11:43 +01:00
|
|
|
def __init__(self, app):
|
2024-12-21 19:00:06 +01:00
|
|
|
self.app = app
|
2024-12-21 19:42:26 +01:00
|
|
|
self.track_progress = TrackProgress(self.app)
|
2024-12-22 16:11:43 +01:00
|
|
|
self.play_state = PlayState()
|
2024-12-21 19:00:06 +01:00
|
|
|
|
2024-12-21 16:07:27 +01:00
|
|
|
pygame.mixer.init()
|
2024-12-21 19:00:06 +01:00
|
|
|
self.mixer = pygame.mixer
|
|
|
|
self.music_channel = self.mixer.Channel(0)
|
|
|
|
|
2024-12-22 16:11:43 +01:00
|
|
|
self.current_playlist = []
|
|
|
|
self.playing_track = None
|
|
|
|
self.current_playlist_index = 0
|
|
|
|
self.playing_segment = None
|
|
|
|
self.playing_segment_duration = 0
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-22 16:11:43 +01:00
|
|
|
def load_tracks_from_paths(self, track_paths: list[str]):
|
2024-12-21 16:07:27 +01:00
|
|
|
"""
|
|
|
|
Load tracks from list of paths.
|
|
|
|
"""
|
|
|
|
|
|
|
|
tracks = []
|
|
|
|
|
|
|
|
for track_path in track_paths:
|
|
|
|
if os.path.isfile(track_path):
|
|
|
|
tracks.append(Track(track_path, True))
|
|
|
|
|
2024-12-22 16:11:43 +01:00
|
|
|
self.current_playlist = tracks
|
|
|
|
self.playing_track = self.current_playlist[0]
|
|
|
|
self.current_playlist_index = 0
|
|
|
|
self.playing_segment = self.playing_track.sound
|
|
|
|
self.playing_segment_duration = self.playing_track.duration
|
2024-12-22 16:18:03 +01:00
|
|
|
self.play_state.has_tracks = len(self.current_playlist) > 0
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
def track_finished(self):
|
|
|
|
self.current_playlist_index += 1
|
2024-12-21 20:58:28 +01:00
|
|
|
if self.current_playlist_index < len(self.current_playlist):
|
|
|
|
self.playing_track = self.current_playlist[self.current_playlist_index]
|
|
|
|
self.playing_segment = self.playing_track.sound
|
|
|
|
self.playing_segment_duration = self.playing_track.duration
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-21 20:58:28 +01:00
|
|
|
self.music_channel.play(self.playing_track.sound)
|
|
|
|
self.track_progress.start()
|
|
|
|
self.app.gui_communication.on_track_start()
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.current_playlist_index -= 1
|
|
|
|
self.stop()
|
2024-12-21 19:00:06 +01:00
|
|
|
|
|
|
|
def start_playing(self):
|
2024-12-22 16:18:03 +01:00
|
|
|
if self.play_state.has_tracks:
|
|
|
|
self.music_channel.play(self.playing_track.sound)
|
|
|
|
self.track_progress.start()
|
|
|
|
self.play_state.paused = False
|
|
|
|
self.play_state.playing = True
|
2024-12-21 19:00:06 +01:00
|
|
|
|
|
|
|
def pause(self):
|
|
|
|
self.music_channel.pause()
|
2024-12-21 19:42:26 +01:00
|
|
|
self.track_progress.pause()
|
2024-12-22 16:11:43 +01:00
|
|
|
self.play_state.paused = True
|
2024-12-21 19:00:06 +01:00
|
|
|
|
|
|
|
def unpause(self):
|
|
|
|
self.music_channel.unpause()
|
2024-12-21 19:42:26 +01:00
|
|
|
self.track_progress.unpause()
|
2024-12-22 16:11:43 +01:00
|
|
|
self.play_state.paused = False
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
def skip_current(self):
|
2024-12-22 16:18:03 +01:00
|
|
|
if self.play_state.has_tracks:
|
|
|
|
self.music_channel.stop()
|
|
|
|
self.track_progress.stop()
|
|
|
|
self.track_finished()
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
def previous_track(self):
|
|
|
|
if self.current_playlist_index > 0:
|
2024-12-21 19:00:06 +01:00
|
|
|
self.music_channel.stop()
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
self.current_playlist_index -= 1
|
|
|
|
self.playing_track = self.current_playlist[self.current_playlist_index]
|
2024-12-21 19:42:26 +01:00
|
|
|
self.track_progress.stop()
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-21 20:20:06 +01:00
|
|
|
self.playing_segment = self.playing_track.sound
|
|
|
|
self.playing_segment_duration = self.playing_track.duration
|
|
|
|
|
2024-12-21 19:00:06 +01:00
|
|
|
self.music_channel.play(self.playing_track.sound)
|
2024-12-21 19:42:26 +01:00
|
|
|
self.track_progress.start()
|
|
|
|
self.app.gui_communication.on_track_start()
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
def toggle_playing(self):
|
2024-12-22 16:11:43 +01:00
|
|
|
if self.play_state.playing and self.play_state.paused:
|
2024-12-21 19:00:06 +01:00
|
|
|
self.unpause()
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-22 16:11:43 +01:00
|
|
|
elif self.play_state.playing:
|
2024-12-21 19:00:06 +01:00
|
|
|
self.pause()
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
else:
|
2024-12-21 19:00:06 +01:00
|
|
|
self.start_playing()
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
def stop(self):
|
2024-12-22 16:18:03 +01:00
|
|
|
if self.play_state.has_tracks:
|
|
|
|
self.music_channel.stop()
|
|
|
|
self.track_progress.stop()
|
|
|
|
self.play_state.playing = False
|
|
|
|
self.playing_segment_duration = self.playing_track.duration
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-21 20:20:06 +01:00
|
|
|
def seek(self, position: int):
|
2024-12-22 16:18:03 +01:00
|
|
|
if self.play_state.has_tracks:
|
|
|
|
self.play_state.searched = True
|
|
|
|
self.music_channel.stop()
|
|
|
|
self.track_progress.stop()
|
|
|
|
(self.playing_segment, self.playing_segment_duration) = self.playing_track.remaining(position)
|
|
|
|
self.music_channel.play(self.playing_segment)
|
|
|
|
self.track_progress.start()
|
2024-12-21 20:20:06 +01:00
|
|
|
|