From ce254c8b54e10e2fe6d7b3debf339d81d9a4be27 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sat, 21 Dec 2024 16:07:27 +0100 Subject: [PATCH] Added opening of files via command line and added simple controls. --- wobuzz/gui.py | 12 +++++- wobuzz/main.py | 7 +++- wobuzz/player/__init__.py | 3 ++ wobuzz/player/player.py | 86 ++++++++++++++++++++++++++++++++++++++ wobuzz/player/track.py | 29 +++++++++++++ wobuzz/ui/track_control.py | 2 +- 6 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 wobuzz/player/__init__.py create mode 100644 wobuzz/player/player.py create mode 100644 wobuzz/player/track.py diff --git a/wobuzz/gui.py b/wobuzz/gui.py index 3dbf0c3..7bbb122 100644 --- a/wobuzz/gui.py +++ b/wobuzz/gui.py @@ -4,7 +4,17 @@ from ui.main_window import MainWindow class GUI: - def __init__(self): + def __init__(self, app): + self.app = app + self.window = MainWindow() self.window.show() + + self.connect() + + 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/main.py b/wobuzz/main.py index 17edae5..76f7fcc 100644 --- a/wobuzz/main.py +++ b/wobuzz/main.py @@ -3,13 +3,16 @@ import sys from PyQt6.QtWidgets import QApplication from gui import GUI +from wobuzz.player.player import Player class Wobuzz: def __init__(self): - self.qt_app = QApplication(sys.argv) + self.qt_app = QApplication([]) - self.gui = GUI() + self.player = Player(sys.argv[1:]) + + self.gui = GUI(self) if __name__ == "__main__": diff --git a/wobuzz/player/__init__.py b/wobuzz/player/__init__.py new file mode 100644 index 0000000..bb079f1 --- /dev/null +++ b/wobuzz/player/__init__.py @@ -0,0 +1,3 @@ +#!/usr/bin/python3 + +from .player import Player diff --git a/wobuzz/player/player.py b/wobuzz/player/player.py new file mode 100644 index 0000000..ed62a76 --- /dev/null +++ b/wobuzz/player/player.py @@ -0,0 +1,86 @@ +#!/usr/bin/python3 + +import os + +import pygame.mixer +from pydub import AudioSegment +from pydub.effects import normalize +import io +from pygame import mixer +from .track import Track + + +class Player: + def __init__(self, file_paths: list=[]): + pygame.mixer.init() + self.mixer = mixer + self.mixer.music.set_endevent() + + self.playing = False + self.paused = False + + if not file_paths: + pass + # loading of last opened files will be implemented in the future + + else: + self.current_playlist = self.load_tracks(file_paths) + + if self.current_playlist: + self.playing_track = self.current_playlist[0] + self.current_playlist_index = 0 + + else: + self.playing_track = None + self.current_playlist_index = 0 + + def load_tracks(self, track_paths: list[str]): + """ + 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)) + + return tracks + + def track_finished(self): + self.current_playlist_index += 1 + self.playing_track = self.current_playlist[self.current_playlist_index] + + self.playing_track.sound.play() + + def skip_current(self): + self.mixer.stop() + self.track_finished() + + def previous_track(self): + if self.current_playlist_index > 0: + self.mixer.stop() + + self.current_playlist_index -= 1 + self.playing_track = self.current_playlist[self.current_playlist_index] + + self.playing_track.sound.play() + + def toggle_playing(self): + if self.playing and self.paused: + self.mixer.pause() + self.paused = False + + elif self.playing: + self.mixer.unpause() + self.paused = True + + else: + self.playing_track.sound.play() + self.paused = False + self.playing = True + + def stop(self): + self.mixer.stop() + self.playing = False + diff --git a/wobuzz/player/track.py b/wobuzz/player/track.py new file mode 100644 index 0000000..aaf29ec --- /dev/null +++ b/wobuzz/player/track.py @@ -0,0 +1,29 @@ +#!/usr/bin/python3 + +from pydub import AudioSegment +from pydub.effects import normalize +from pygame.mixer import Sound + + +class Track: + """ + Class containing data for a track like file path, raw data... + """ + + def __init__(self, path: str, cache: bool=False): + self.path = path + self.cached = cache + + self.sound = self.cache() if self.cached else None + + def cache(self): + audio = AudioSegment.from_mp3(self.path) + audio = normalize(audio) + + wav = audio.export(format="wav") + + sound = Sound(wav) + + # audio_bytes = io.BytesIO(wav.read()) + + return sound diff --git a/wobuzz/ui/track_control.py b/wobuzz/ui/track_control.py index 4859b20..45e640f 100644 --- a/wobuzz/ui/track_control.py +++ b/wobuzz/ui/track_control.py @@ -27,6 +27,6 @@ class TrackControl(QToolBar): self.play_position_slider = QSlider(Qt.Orientation.Horizontal, self) self.addWidget(self.play_position_slider) - self.track_length_indicator = QLabel("1:00") + self.track_length_indicator = QLabel("0:00") self.addWidget(self.track_length_indicator)