Added opening of files via command line and added simple controls.
This commit is contained in:
parent
d089a57151
commit
ce254c8b54
6 changed files with 135 additions and 4 deletions
3
wobuzz/player/__init__.py
Normal file
3
wobuzz/player/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
from .player import Player
|
86
wobuzz/player/player.py
Normal file
86
wobuzz/player/player.py
Normal 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
29
wobuzz/player/track.py
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue