2024-12-21 16:07:27 +01:00
|
|
|
#!/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
|
|
|
|
|
2024-12-21 19:00:06 +01:00
|
|
|
(self.audio, self.sound, self.duration) = self.cache() if self.cached else (None, None, 0)
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
def cache(self):
|
|
|
|
audio = AudioSegment.from_mp3(self.path)
|
|
|
|
audio = normalize(audio)
|
|
|
|
|
|
|
|
wav = audio.export(format="wav")
|
|
|
|
|
|
|
|
sound = Sound(wav)
|
|
|
|
|
2024-12-21 19:00:06 +01:00
|
|
|
# return pygame.mixer.Sound object and track duration in milliseconds
|
|
|
|
return audio, sound, len(audio)
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-21 19:00:06 +01:00
|
|
|
def remaining(self, position: int):
|
2024-12-21 20:20:06 +01:00
|
|
|
remaining_audio = self.audio[position:]
|
|
|
|
|
|
|
|
wav = remaining_audio.export(format="wav")
|
|
|
|
|
|
|
|
sound = Sound(wav)
|
|
|
|
|
|
|
|
return sound, len(remaining_audio)
|