Wobuzz/wobuzz/player/track.py

62 lines
1.4 KiB
Python

#!/usr/bin/python3
from pydub import AudioSegment
from pydub.effects import normalize
from pygame.mixer import Sound
from tinytag import TinyTag
SUPPORTED_FORMATS = [
"mp3",
"wav",
"ogg"
]
class Track:
"""
Class containing data for a track like file path, raw data...
"""
def __init__(self, app, path: str, property_string: str=None, cache: bool=False):
self.app = app
self.path = path
self.property_string = property_string
self.tags = TinyTag.get(self.path)
self.cached = False
self.audio = None
self.sound = None
self.duration = 0
if cache:
self.cache()
def cache(self):
self.load_audio()
# audio = normalize(audio)
wav = self.audio.export(format="wav")
self.sound = Sound(wav)
self.duration = len(self.audio) # track duration in milliseconds
self.cached = True
def load_audio(self):
type = self.path.split(".")[-1]
if type in SUPPORTED_FORMATS:
self.audio = AudioSegment.from_file(self.path)
def remaining(self, position: int):
remaining_audio = self.audio[position:]
wav = remaining_audio.export(format="wav")
sound = Sound(wav)
# return the remaining part of the track's audio and the duration of the remaining part
return sound, len(remaining_audio)