31 lines
795 B
Python
31 lines
795 B
Python
#!/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.audio, self.sound, self.duration) = self.cache() if self.cached else (None, None, 0)
|
|
|
|
def cache(self):
|
|
audio = AudioSegment.from_mp3(self.path)
|
|
audio = normalize(audio)
|
|
|
|
wav = audio.export(format="wav")
|
|
|
|
sound = Sound(wav)
|
|
|
|
# return pygame.mixer.Sound object and track duration in milliseconds
|
|
return audio, sound, len(audio)
|
|
|
|
def remaining(self, position: int):
|
|
return self.audio[-position:]
|