From 45d97f5aebf00d52505f4e765abb5728337a9e6e Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sat, 21 Dec 2024 19:00:06 +0100 Subject: [PATCH] Got shitty basic features working. --- wobuzz/gui.py | 6 ++-- wobuzz/gui_communication.py | 34 +++++++++++++++++++ wobuzz/main.py | 7 ++-- wobuzz/player/player.py | 67 ++++++++++++++++++++++++++----------- wobuzz/player/track.py | 8 +++-- wobuzz/ui/track_control.py | 4 +-- 6 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 wobuzz/gui_communication.py diff --git a/wobuzz/gui.py b/wobuzz/gui.py index 7bbb122..6bf7485 100644 --- a/wobuzz/gui.py +++ b/wobuzz/gui.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 +from PyQt6.QtCore import QTimer from ui.main_window import MainWindow @@ -9,12 +10,13 @@ class GUI: self.window = MainWindow() - self.window.show() - self.connect() + self.window.show() + def connect(self): self.window.main_container.track_control.previous_button.triggered.connect(self.app.player.previous_track) self.window.main_container.track_control.toggle_play_button.triggered.connect(self.app.player.toggle_playing) self.window.main_container.track_control.stop_button.triggered.connect(self.app.player.stop) self.window.main_container.track_control.next_button.triggered.connect(self.app.player.skip_current) + diff --git a/wobuzz/gui_communication.py b/wobuzz/gui_communication.py new file mode 100644 index 0000000..a5d19a4 --- /dev/null +++ b/wobuzz/gui_communication.py @@ -0,0 +1,34 @@ +#!/usr/bin/python3 + +from PyQt6.QtCore import QTimer + +PROGRESS_UPDATE_RATE = 60 +PROGRESS_UPDATE_INTERVAL = 1000 // PROGRESS_UPDATE_RATE + + +class GUICommunication: + def __init__(self, app): + self.app = app + self.window = self.app.gui.window + + self.progress_update_timer = QTimer() + self.progress_update_timer.timeout.connect(self.update_progress) + self.progress_update_timer.start(PROGRESS_UPDATE_INTERVAL) + self.on_track_start() + + def on_track_start(self): + self.window.main_container.track_control.track_progress_slider.setRange( + 0, + self.app.player.playing_track.duration + ) + + self.update_progress() + + def update_progress(self): + remaining = self.app.player.track_progress_timer.remainingTime() + track_duration = self.app.player.playing_track.duration + + progress = track_duration - remaining + + self.window.main_container.track_control.track_progress_slider.setValue(progress) + diff --git a/wobuzz/main.py b/wobuzz/main.py index 76f7fcc..7a470e2 100644 --- a/wobuzz/main.py +++ b/wobuzz/main.py @@ -2,17 +2,18 @@ import sys from PyQt6.QtWidgets import QApplication -from gui import GUI from wobuzz.player.player import Player +from gui import GUI +from gui_communication import GUICommunication class Wobuzz: def __init__(self): self.qt_app = QApplication([]) - self.player = Player(sys.argv[1:]) - + self.player = Player(self, sys.argv[1:]) self.gui = GUI(self) + self.gui_communication = GUICommunication(self) if __name__ == "__main__": diff --git a/wobuzz/player/player.py b/wobuzz/player/player.py index ed62a76..e717c0e 100644 --- a/wobuzz/player/player.py +++ b/wobuzz/player/player.py @@ -2,19 +2,24 @@ import os +from PyQt6.QtCore import QTimer import pygame.mixer -from pydub import AudioSegment -from pydub.effects import normalize -import io -from pygame import mixer +import pygame.event from .track import Track class Player: - def __init__(self, file_paths: list=[]): + def __init__(self, app, file_paths: list=[]): + self.app = app + pygame.mixer.init() - self.mixer = mixer - self.mixer.music.set_endevent() + self.mixer = pygame.mixer + self.music_channel = self.mixer.Channel(0) + + self.track_progress_timer = QTimer() + self.track_progress_timer.timeout.connect(self.track_finished) + self.track_progress_timer.setSingleShot(True) + self.remaining_time = 0 self.playing = False self.paused = False @@ -51,36 +56,60 @@ class Player: self.current_playlist_index += 1 self.playing_track = self.current_playlist[self.current_playlist_index] - self.playing_track.sound.play() + self.music_channel.play(self.playing_track.sound) + self.start_track_progress_timer() + self.app.gui_communication.on_track_start() + + def start_playing(self): + self.music_channel.play(self.playing_track.sound) + self.start_track_progress_timer() + self.paused = False + self.playing = True + + def pause(self): + self.music_channel.pause() + self.paused = True + self.pause_track_progress_timer() + + def unpause(self): + self.music_channel.unpause() + self.paused = False + self.unpause_track_progress_timer() def skip_current(self): - self.mixer.stop() + self.music_channel.stop() self.track_finished() def previous_track(self): if self.current_playlist_index > 0: - self.mixer.stop() + self.music_channel.stop() self.current_playlist_index -= 1 self.playing_track = self.current_playlist[self.current_playlist_index] - self.playing_track.sound.play() + self.music_channel.play(self.playing_track.sound) def toggle_playing(self): if self.playing and self.paused: - self.mixer.pause() - self.paused = False + self.unpause() elif self.playing: - self.mixer.unpause() - self.paused = True + self.pause() else: - self.playing_track.sound.play() - self.paused = False - self.playing = True + self.start_playing() def stop(self): - self.mixer.stop() + self.music_channel.stop() self.playing = False + def start_track_progress_timer(self): + self.track_progress_timer.start(self.playing_track.duration) + + def pause_track_progress_timer(self): + self.remaining_time = self.track_progress_timer.remainingTime() + self.track_progress_timer.stop() + + def unpause_track_progress_timer(self): + self.track_progress_timer.start(self.playing_track.duration - self.remaining_time) + diff --git a/wobuzz/player/track.py b/wobuzz/player/track.py index aaf29ec..ec63fea 100644 --- a/wobuzz/player/track.py +++ b/wobuzz/player/track.py @@ -14,7 +14,7 @@ class Track: self.path = path self.cached = cache - self.sound = self.cache() if self.cached else None + (self.audio, self.sound, self.duration) = self.cache() if self.cached else (None, None, 0) def cache(self): audio = AudioSegment.from_mp3(self.path) @@ -24,6 +24,8 @@ class Track: sound = Sound(wav) - # audio_bytes = io.BytesIO(wav.read()) + # return pygame.mixer.Sound object and track duration in milliseconds + return audio, sound, len(audio) - return sound + def remaining(self, position: int): + return self.audio[-position:] diff --git a/wobuzz/ui/track_control.py b/wobuzz/ui/track_control.py index 45e640f..8771615 100644 --- a/wobuzz/ui/track_control.py +++ b/wobuzz/ui/track_control.py @@ -24,8 +24,8 @@ class TrackControl(QToolBar): self.current_position_indicator = QLabel("0:00") self.addWidget(self.current_position_indicator) - self.play_position_slider = QSlider(Qt.Orientation.Horizontal, self) - self.addWidget(self.play_position_slider) + self.track_progress_slider = QSlider(Qt.Orientation.Horizontal, self) + self.addWidget(self.track_progress_slider) self.track_length_indicator = QLabel("0:00") self.addWidget(self.track_length_indicator)