From 24d589b17286a7c930163bace3625c2fdbe2b029 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sat, 21 Dec 2024 20:20:06 +0100 Subject: [PATCH] Implemented seeking. --- wobuzz/gui_communication.py | 32 +++++++++++++++++++++------ wobuzz/player/player.py | 18 +++++++++++++++ wobuzz/player/track.py | 8 ++++++- wobuzz/player/track_progress_timer.py | 3 +-- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/wobuzz/gui_communication.py b/wobuzz/gui_communication.py index 3fa5a9e..3ad616e 100644 --- a/wobuzz/gui_communication.py +++ b/wobuzz/gui_communication.py @@ -9,15 +9,32 @@ PROGRESS_UPDATE_INTERVAL = 1000 // PROGRESS_UPDATE_RATE class GUICommunication: def __init__(self, app): self.app = app + self.window = self.app.gui.window + self.track_control = self.window.main_container.track_control + + self.track_progress_slider_dragged = False + + self.connect() 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 connect(self): + self.track_control.track_progress_slider.sliderPressed.connect(self.on_progress_slider_press) + self.track_control.track_progress_slider.sliderReleased.connect(self.on_progress_slider_release) + + def on_progress_slider_press(self): + self.track_progress_slider_dragged = True + + def on_progress_slider_release(self): + self.track_progress_slider_dragged = False + self.app.player.seek(self.track_control.track_progress_slider.value()) + def on_track_start(self): - self.window.main_container.track_control.track_progress_slider.setRange( + self.track_control.track_progress_slider.setRange( 0, self.app.player.playing_track.duration ) @@ -25,14 +42,15 @@ class GUICommunication: self.update_progress() def update_progress(self): - remaining = self.app.player.track_progress.timer.remainingTime() + if not self.track_progress_slider_dragged: + remaining = self.app.player.track_progress.timer.remainingTime() - if remaining == -1: - remaining = self.app.player.track_progress.remaining_time + if remaining == -1: + remaining = self.app.player.track_progress.remaining_time - track_duration = self.app.player.playing_track.duration + track_duration = self.app.player.playing_track.duration - progress = track_duration - remaining + progress = track_duration - remaining - self.window.main_container.track_control.track_progress_slider.setValue(progress) + self.track_control.track_progress_slider.setValue(progress) diff --git a/wobuzz/player/player.py b/wobuzz/player/player.py index 5939e08..824f95a 100644 --- a/wobuzz/player/player.py +++ b/wobuzz/player/player.py @@ -20,6 +20,7 @@ class Player: self.playing = False self.paused = False + self.searched = False if not file_paths: pass @@ -31,10 +32,14 @@ class Player: if self.current_playlist: 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 else: self.playing_track = None self.current_playlist_index = 0 + self.playing_segment = None + self.playing_segment_duration = 0 def load_tracks(self, track_paths: list[str]): """ @@ -52,6 +57,8 @@ class Player: def track_finished(self): self.current_playlist_index += 1 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 self.music_channel.play(self.playing_track.sound) self.track_progress.start() @@ -86,6 +93,9 @@ class Player: self.playing_track = self.current_playlist[self.current_playlist_index] self.track_progress.stop() + self.playing_segment = self.playing_track.sound + self.playing_segment_duration = self.playing_track.duration + self.music_channel.play(self.playing_track.sound) self.track_progress.start() self.app.gui_communication.on_track_start() @@ -105,3 +115,11 @@ class Player: self.track_progress.stop() self.playing = False + def seek(self, position: int): + self.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() + diff --git a/wobuzz/player/track.py b/wobuzz/player/track.py index ec63fea..d4d706d 100644 --- a/wobuzz/player/track.py +++ b/wobuzz/player/track.py @@ -28,4 +28,10 @@ class Track: return audio, sound, len(audio) def remaining(self, position: int): - return self.audio[-position:] + remaining_audio = self.audio[position:] + + wav = remaining_audio.export(format="wav") + + sound = Sound(wav) + + return sound, len(remaining_audio) diff --git a/wobuzz/player/track_progress_timer.py b/wobuzz/player/track_progress_timer.py index 284d8ec..bae790a 100644 --- a/wobuzz/player/track_progress_timer.py +++ b/wobuzz/player/track_progress_timer.py @@ -17,7 +17,7 @@ class TrackProgress: self.app.player.track_finished() def start(self): - self.timer.start(self.app.player.playing_track.duration) + self.timer.start(self.app.player.playing_segment_duration) def pause(self): self.remaining_time = self.timer.remainingTime() @@ -27,6 +27,5 @@ class TrackProgress: self.timer.start(self.remaining_time) def stop(self): - print(self.app.player.playing_track.path) self.timer.stop() self.remaining_time = self.app.player.playing_track.duration