#!/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): remaining_audio = self.audio[position:] wav = remaining_audio.export(format="wav") sound = Sound(wav) return sound, len(remaining_audio)