Got shitty basic features working.

This commit is contained in:
The Wobbler 2024-12-21 19:00:06 +01:00
parent ce254c8b54
commit 45d97f5aeb
6 changed files with 97 additions and 29 deletions

View file

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

View file

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

View file

@ -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__":

View file

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

View file

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

View file

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