Added opening of files via command line and added simple controls.

This commit is contained in:
The Wobbler 2024-12-21 16:07:27 +01:00
parent d089a57151
commit ce254c8b54
6 changed files with 135 additions and 4 deletions

View file

@ -4,7 +4,17 @@ from ui.main_window import MainWindow
class GUI: class GUI:
def __init__(self): def __init__(self, app):
self.app = app
self.window = MainWindow() self.window = MainWindow()
self.window.show() 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)

View file

@ -3,13 +3,16 @@
import sys import sys
from PyQt6.QtWidgets import QApplication from PyQt6.QtWidgets import QApplication
from gui import GUI from gui import GUI
from wobuzz.player.player import Player
class Wobuzz: class Wobuzz:
def __init__(self): 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__": if __name__ == "__main__":

View file

@ -0,0 +1,3 @@
#!/usr/bin/python3
from .player import Player

86
wobuzz/player/player.py Normal file
View file

@ -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

29
wobuzz/player/track.py Normal file
View file

@ -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

View file

@ -27,6 +27,6 @@ class TrackControl(QToolBar):
self.play_position_slider = QSlider(Qt.Orientation.Horizontal, self) self.play_position_slider = QSlider(Qt.Orientation.Horizontal, self)
self.addWidget(self.play_position_slider) 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) self.addWidget(self.track_length_indicator)